ffmpeg-mappable-media/src/FFMpeg/FFProbe.php

165 lines
3.8 KiB
PHP
Raw Normal View History

2012-04-13 10:20:54 +02:00
<?php
2012-04-13 14:34:53 +02:00
/*
* This file is part of PHP-FFmpeg.
*
* (c) Alchemy <info@alchemy.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2012-04-13 10:20:54 +02:00
namespace FFMpeg;
2012-05-25 16:21:16 +02:00
use FFMpeg\Exception\InvalidArgumentException;
use FFMpeg\Exception\RuntimeException;
use Symfony\Component\Process\Process;
2012-04-13 15:12:43 +02:00
/**
* FFProbe driver
*
* @author Romain Neutron imprec@gmail.com
*/
2012-04-13 10:20:54 +02:00
class FFProbe extends Binary
{
2012-04-13 15:12:43 +02:00
/**
* Probe the format of a given file
*
2012-05-25 16:22:16 +02:00
* @param string $pathfile
2012-05-31 15:54:28 +02:00
* @return string A Json object containing the key/values of the probe output
*
2012-05-25 16:21:16 +02:00
* @throws InvalidArgumentException
* @throws RuntimeException
2012-04-13 15:12:43 +02:00
*/
2012-04-13 10:20:54 +02:00
public function probeFormat($pathfile)
{
2012-05-11 00:30:02 +02:00
if ( ! is_file($pathfile)) {
2012-05-25 16:21:16 +02:00
throw new InvalidArgumentException($pathfile);
2012-04-13 10:20:54 +02:00
}
$cmd = $this->binary . ' ' . $pathfile . ' -show_format';
2012-05-31 15:54:28 +02:00
$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);
2012-04-13 10:20:54 +02:00
}
2012-04-13 15:12:43 +02:00
/**
* Probe the streams contained in a given file
*
2012-05-25 16:22:16 +02:00
* @param string $pathfile
2012-05-31 15:54:28 +02:00
* @return array An array of streams array
*
2012-05-25 16:21:16 +02:00
* @throws InvalidArgumentException
* @throws RuntimeException
2012-04-13 15:12:43 +02:00
*/
2012-04-13 10:20:54 +02:00
public function probeStreams($pathfile)
{
2012-05-11 00:30:02 +02:00
if ( ! is_file($pathfile)) {
2012-05-25 16:21:16 +02:00
throw new InvalidArgumentException($pathfile);
2012-04-13 10:20:54 +02:00
}
$cmd = $this->binary . ' ' . $pathfile . ' -show_streams';
$output = explode("\n", $this->executeProbe($cmd));
$ret = array();
$n = 0;
foreach ($output as $line) {
2012-05-31 15:54:28 +02:00
if ($line == '[STREAM]') {
$n ++;
$ret[$n] = array();
continue;
}
2012-05-31 15:54:28 +02:00
if ($line == '[/STREAM]') {
continue;
}
$chunks = explode('=', $line);
$key = array_shift($chunks);
if ('' === trim($key)) {
continue;
}
$value = trim(implode('=', $chunks));
if (ctype_digit($value)) {
$value = (int) $value;
}
2012-05-31 15:54:28 +02:00
$ret[$n][$key] = $value;
}
2012-05-31 15:54:28 +02:00
return json_encode(array_values($ret));
2012-04-13 10:20:54 +02:00
}
2012-04-13 15:12:43 +02:00
/**
*
2012-05-25 16:22:16 +02:00
* @param string $command
2012-04-13 15:12:43 +02:00
* @return string
2012-05-25 16:21:16 +02:00
* @throws RuntimeException
2012-04-13 15:12:43 +02:00
*/
2012-04-13 10:20:54 +02:00
protected function executeProbe($command)
{
2012-05-25 16:21:16 +02:00
$this->logger->addInfo(sprintf('FFprobe executes command %s', $command));
2012-05-11 00:30:02 +02:00
try {
2012-04-13 15:12:43 +02:00
$process = new Process($command);
2012-04-13 10:20:54 +02:00
2012-04-13 15:12:43 +02:00
$process->run();
2012-05-11 00:30:02 +02:00
} catch (\RuntimeException $e) {
2012-05-25 16:21:16 +02:00
$this->logger->addInfo('FFprobe command failed');
throw new RuntimeException(sprintf('Failed to run the given command %s', $command));
2012-04-13 15:12:43 +02:00
}
2012-04-13 10:20:54 +02:00
2012-05-11 00:30:02 +02:00
if ( ! $process->isSuccessful()) {
2012-05-25 16:21:16 +02:00
$this->logger->addInfo('FFprobe command failed');
throw new RuntimeException(sprintf('Failed to probe %s', $command));
2012-04-13 10:20:54 +02:00
}
2012-05-25 16:21:16 +02:00
$this->logger->addInfo('FFprobe command successful');
2012-04-13 10:20:54 +02:00
return $process->getOutput();
}
2012-04-13 15:12:43 +02:00
/**
2012-05-25 16:21:16 +02:00
* {@inheritdoc}
2012-04-13 15:12:43 +02:00
*
* @return string
*/
2012-04-13 10:20:54 +02:00
protected static function getBinaryName()
{
2012-05-11 00:30:02 +02:00
return array('avprobe', 'ffprobe');
2012-04-13 10:20:54 +02:00
}
}