diff --git a/src/FFMpeg/FFMpeg.php b/src/FFMpeg/FFMpeg.php index 7fa0301..17dbc60 100644 --- a/src/FFMpeg/FFMpeg.php +++ b/src/FFMpeg/FFMpeg.php @@ -186,7 +186,7 @@ class FFMpeg extends Binary if ($format instanceof Audio\Transcodable) { $cmd .= ' -acodec ' . $format->getAudioCodec(); } - + if ($format instanceof Audio\Resamplable) { $cmd .= ' -ac 2 -ar ' . $format->getAudioSampleRate(); } @@ -234,17 +234,17 @@ class FFMpeg extends Binary throw new LogicException('You must set a valid prober if you use RESIZEMODE_INSET'); } - $result = $this->prober->probeStreams($this->pathfile); + $result = json_decode($this->prober->probeStreams($this->pathfile), true); $originalWidth = $originalHeight = null; foreach ($result as $stream) { - foreach ($stream as $info) { - if (strpos($info, 'width=') === 0) { + foreach ($stream as $name => $value) { + if ($name == 'width') { $originalWidth = substr($info, 6); continue; } - if (strpos($info, 'height=') === 0) { + if ($name == 'value') { $originalHeight = substr($info, 7); continue; } diff --git a/src/FFMpeg/FFProbe.php b/src/FFMpeg/FFProbe.php index fb90b9b..b2484e4 100644 --- a/src/FFMpeg/FFProbe.php +++ b/src/FFMpeg/FFProbe.php @@ -27,7 +27,8 @@ class FFProbe extends Binary * Probe the format of a given file * * @param string $pathfile - * @return string + * @return string A Json object containing the key/values of the probe output + * * @throws InvalidArgumentException * @throws RuntimeException */ @@ -39,14 +40,41 @@ class FFProbe extends Binary $cmd = $this->binary . ' ' . $pathfile . ' -show_format'; - return $this->executeProbe($cmd); + $output = $this->executeProbe($cmd); + + $ret = array(); + + foreach (explode("\n", $output) as $line) { + + if (in_array($line, array('[FORMAT]', '[/FORMAT]'))) { + continue; + } + + $chunks = explode('=', $line); + $key = array_shift($chunks); + + if ('' === trim($key)) { + continue; + } + + $value = trim(implode('=', $chunks)); + + if (ctype_digit($value)) { + $value = (int) $value; + } + + $ret[$key] = $value; + } + + return json_encode($ret); } /** * Probe the streams contained in a given file * * @param string $pathfile - * @return string + * @return array An array of streams array + * * @throws InvalidArgumentException * @throws RuntimeException */ @@ -65,16 +93,32 @@ class FFProbe extends Binary foreach ($output as $line) { - if (in_array($line, array('[STREAM]', '[/STREAM]'))) { + if ($line == '[STREAM]') { $n ++; $ret[$n] = array(); continue; } + if ($line == '[/STREAM]') { + continue; + } - $ret[$n][] = $line; + $chunks = explode('=', $line); + $key = array_shift($chunks); + + if ('' === trim($key)) { + continue; + } + + $value = trim(implode('=', $chunks)); + + if (ctype_digit($value)) { + $value = (int) $value; + } + + $ret[$n][$key] = $value; } - return $ret; + return json_encode(array_values($ret)); } /**