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

121 lines
2.7 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-04-13 15:12:43 +02:00
* @return string
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';
return $this->executeProbe($cmd);
}
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-04-13 15:12:43 +02:00
* @return string
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) {
if (in_array($line, array('[STREAM]', '[/STREAM]'))) {
$n ++;
$ret[$n] = array();
continue;
}
$ret[$n][] = $line;
}
return $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
}
}