Can now use avconv and avprobe

This commit is contained in:
Romain Neutron 2012-05-11 00:30:02 +02:00
commit 7f3d76c5fe
3 changed files with 65 additions and 82 deletions

View file

@ -20,7 +20,6 @@ use \Symfony\Component\Process\ExecutableFinder;
*/ */
abstract class Binary implements AdapterInterface abstract class Binary implements AdapterInterface
{ {
protected $binary; protected $binary;
/** /**
@ -39,8 +38,7 @@ abstract class Binary implements AdapterInterface
{ {
$this->binary = $binary; $this->binary = $binary;
if ( ! $logger) if ( ! $logger) {
{
$logger = new \Monolog\Logger('default'); $logger = new \Monolog\Logger('default');
$logger->pushHandler(new \Monolog\Handler\NullHandler()); $logger->pushHandler(new \Monolog\Handler\NullHandler());
} }
@ -58,9 +56,15 @@ abstract class Binary implements AdapterInterface
public static function load(\Monolog\Logger $logger = null) public static function load(\Monolog\Logger $logger = null)
{ {
$finder = new ExecutableFinder(); $finder = new ExecutableFinder();
$binary = null;
if (null === $binary = $finder->find(static::getBinaryName())) foreach (static::getBinaryName() as $candidate) {
{ if (null !== $binary = $finder->find(static::getBinaryName())) {
break;
}
}
if (null === $binary) {
throw new Exception\BinaryNotFoundException('Binary not found'); throw new Exception\BinaryNotFoundException('Binary not found');
} }
@ -76,5 +80,4 @@ abstract class Binary implements AdapterInterface
{ {
throw new \Exception('Should be implemented'); throw new \Exception('Should be implemented');
} }
} }

View file

@ -20,7 +20,6 @@ use \Symfony\Component\Process\Process;
*/ */
class FFMpeg extends Binary class FFMpeg extends Binary
{ {
protected $pathfile; protected $pathfile;
/** /**
@ -32,8 +31,7 @@ class FFMpeg extends Binary
*/ */
public function open($pathfile) public function open($pathfile)
{ {
if ( ! file_exists($pathfile)) if ( ! file_exists($pathfile)) {
{
$this->logger->addError(sprintf('Request to open %s failed', $pathfile)); $this->logger->addError(sprintf('Request to open %s failed', $pathfile));
throw new Exception\InvalidFileArgumentException(sprintf('File %s does not exists', $pathfile)); throw new Exception\InvalidFileArgumentException(sprintf('File %s does not exists', $pathfile));
@ -68,31 +66,26 @@ class FFMpeg extends Binary
*/ */
public function extractImage($time, $output) public function extractImage($time, $output)
{ {
if ( ! $this->pathfile) if ( ! $this->pathfile) {
{
throw new Exception\LogicException('No file open'); throw new Exception\LogicException('No file open');
} }
$cmd = $this->binary $cmd = $this->binary
. ' -i ' . escapeshellarg($this->pathfile) . ' -i ' . escapeshellarg($this->pathfile)
. ' -vframes 1 -ss ' . $time . ' -vframes 1 -ss ' . $time
. ' -f image2 ' . escapeshellarg($output); . ' -f image2 ' . escapeshellarg($output);
$this->logger->addInfo(sprintf('Executing command %s', $cmd)); $this->logger->addInfo(sprintf('Executing command %s', $cmd));
$process = new Process($cmd); $process = new Process($cmd);
try try {
{
$process->run(); $process->run();
} } catch (\RuntimeException $e) {
catch (\RuntimeException $e)
{
} }
if ( ! $process->isSuccessful()) if ( ! $process->isSuccessful()) {
{
$this->logger->addError(sprintf('Command failed :: %s', $process->getErrorOutput())); $this->logger->addError(sprintf('Command failed :: %s', $process->getErrorOutput()));
$this->cleanupTemporaryFile($output); $this->cleanupTemporaryFile($output);
@ -117,15 +110,13 @@ class FFMpeg extends Binary
*/ */
public function encode(Format\AudioFormat $format, $outputPathfile, $threads = 1) public function encode(Format\AudioFormat $format, $outputPathfile, $threads = 1)
{ {
if ( ! $this->pathfile) if ( ! $this->pathfile) {
{
throw new Exception\LogicException('No file open'); throw new Exception\LogicException('No file open');
} }
$threads = max(min($threads, 64), 1); $threads = max(min($threads, 64), 1);
switch (true) switch (true) {
{
case $format instanceof Format\VideoFormat: case $format instanceof Format\VideoFormat:
$this->encodeVideo($format, $outputPathfile, $threads); $this->encodeVideo($format, $outputPathfile, $threads);
break; break;
@ -150,28 +141,24 @@ class FFMpeg extends Binary
protected function encodeAudio(Format\AudioFormat $format, $outputPathfile, $threads) protected function encodeAudio(Format\AudioFormat $format, $outputPathfile, $threads)
{ {
$cmd = $this->binary $cmd = $this->binary
. ' -y -i ' . ' -y -i '
. escapeshellarg($this->pathfile) . escapeshellarg($this->pathfile)
. ' ' . $format->getExtraParams() . ' ' . $format->getExtraParams()
. ' -threads ' . $threads . ' -threads ' . $threads
. ' -acodec ' . $format->getAudioCodec() . ' -acodec ' . $format->getAudioCodec()
. ' -ab ' . $format->getKiloBitrate() . 'k ' . ' -ab ' . $format->getKiloBitrate() . 'k '
. ' -ac 2 -ar ' . $format->getAudioSampleRate() . ' -ac 2 -ar ' . $format->getAudioSampleRate()
. ' ' . escapeshellarg($outputPathfile); . ' ' . escapeshellarg($outputPathfile);
$process = new Process($cmd); $process = new Process($cmd);
try try {
{
$process->run(); $process->run();
} } catch (\RuntimeException $e) {
catch (\RuntimeException $e)
{
} }
if ( ! $process->isSuccessful()) if ( ! $process->isSuccessful()) {
{
throw new Exception\RuntimeException(sprintf('Encoding failed : %s', $process->getErrorOutput())); throw new Exception\RuntimeException(sprintf('Encoding failed : %s', $process->getErrorOutput()));
} }
@ -187,23 +174,23 @@ class FFMpeg extends Binary
* @return \FFMpeg\FFMpeg * @return \FFMpeg\FFMpeg
* @throws Exception\RuntimeException * @throws Exception\RuntimeException
*/ */
protected function encodeVideo(Format\VideoFormat $format, $outputPathfile, $threads) protected function encodeVideo(Format\VideoFormat $format, $outputPathfile, $threads, $control)
{ {
$cmd_part1 = $this->binary $cmd_part1 = $this->binary
. ' -y -i ' . ' -y -i '
. escapeshellarg($this->pathfile) . ' ' . escapeshellarg($this->pathfile) . ' '
. $format->getExtraParams() . ' '; . $format->getExtraParams() . ' ';
$cmd_part2 = ' -s ' . $format->getWidth() . 'x' . $format->getHeight() $cmd_part2 = ' -s ' . $format->getWidth() . 'x' . $format->getHeight()
. ' -r ' . $format->getFrameRate() . ' -r ' . $format->getFrameRate()
. ' -vcodec ' . $format->getVideoCodec() . ' -vcodec ' . $format->getVideoCodec()
. ' -b ' . $format->getKiloBitrate() . 'k -g 25 -bf 3' . ' -b ' . $format->getKiloBitrate() . 'k -g 25 -bf 3'
. ' -threads ' . $threads . ' -threads ' . $threads
. ' -refs 6 -b_strategy 1 -coder 1 -qmin 10 -qmax 51 ' . ' -refs 6 -b_strategy 1 -coder 1 -qmin 10 -qmax 51 '
. ' -sc_threshold 40 -flags +loop -cmp +chroma' . ' -sc_threshold 40 -flags +loop -cmp +chroma'
. ' -me_range 16 -subq 7 -i_qfactor 0.71 -qcomp 0.6 -qdiff 4 ' . ' -me_range 16 -subq 7 -i_qfactor 0.71 -qcomp 0.6 -qdiff 4 '
. ' -trellis 1 -qscale 1 ' . ' -trellis 1 -qscale 1 '
. '-acodec ' . $format->getAudioCodec() . ' -ab 92k '; . '-acodec ' . $format->getAudioCodec() . ' -ab 92k ';
$tmpFile = new \SplFileInfo(tempnam(sys_get_temp_dir(), 'temp') . '.' . pathinfo($outputPathfile, PATHINFO_EXTENSION)); $tmpFile = new \SplFileInfo(tempnam(sys_get_temp_dir(), 'temp') . '.' . pathinfo($outputPathfile, PATHINFO_EXTENSION));
@ -211,23 +198,26 @@ class FFMpeg extends Binary
$passes = array(); $passes = array();
$passes[] = $cmd_part1 . ' -pass 1 ' . $cmd_part2 $passes[] = $cmd_part1 . ' -pass 1 ' . $cmd_part2
. ' -an ' . escapeshellarg($tmpFile->getPathname()); . ' -an ' . escapeshellarg($tmpFile->getPathname());
$passes[] = $cmd_part1 . ' -pass 2 ' . $cmd_part2 $passes[] = $cmd_part1 . ' -pass 2 ' . $cmd_part2
. ' -ac 2 -ar 44100 ' . escapeshellarg($outputPathfile); . ' -ac 2 -ar 44100 ' . escapeshellarg($outputPathfile);
$process = null; $process = null;
foreach ($passes as $pass) foreach ($passes as $pass) {
{
$process = new Process($pass); $process = new Process($pass);
try try {
{ // $process->run();
$process->run(); $process->run(function($data, $dodo) {
} // echo $data.$dodo."\nend chunk\n";
catch (\RuntimeException $e) $matches = array();
{ preg_match('/time=([0-9:\.]+)/', $dodo, $matches);
if ($matches[1])
var_dump($matches);
});
} catch (\RuntimeException $e) {
break; break;
} }
} }
@ -236,8 +226,7 @@ class FFMpeg extends Binary
$this->cleanupTemporaryFile(getcwd() . '/ffmpeg2pass-0.log'); $this->cleanupTemporaryFile(getcwd() . '/ffmpeg2pass-0.log');
$this->cleanupTemporaryFile(getcwd() . '/ffmpeg2pass-0.log.mbtree'); $this->cleanupTemporaryFile(getcwd() . '/ffmpeg2pass-0.log.mbtree');
if ( ! $process->isSuccessful()) if ( ! $process->isSuccessful()) {
{
throw new Exception\RuntimeException(sprintf('Encoding failed : %s', $process->getErrorOutput())); throw new Exception\RuntimeException(sprintf('Encoding failed : %s', $process->getErrorOutput()));
} }
@ -251,8 +240,7 @@ class FFMpeg extends Binary
*/ */
protected function cleanupTemporaryFile($pathfile) protected function cleanupTemporaryFile($pathfile)
{ {
if (file_exists($pathfile) && is_writable($pathfile)) if (file_exists($pathfile) && is_writable($pathfile)) {
{
unlink($pathfile); unlink($pathfile);
} }
} }
@ -264,7 +252,6 @@ class FFMpeg extends Binary
*/ */
protected static function getBinaryName() protected static function getBinaryName()
{ {
return 'ffmpeg'; return array('avconv', 'ffmpeg');
} }
} }

View file

@ -31,8 +31,7 @@ class FFProbe extends Binary
*/ */
public function probeFormat($pathfile) public function probeFormat($pathfile)
{ {
if ( ! is_file($pathfile)) if ( ! is_file($pathfile)) {
{
throw new Exception\InvalidFileArgumentException($pathfile); throw new Exception\InvalidFileArgumentException($pathfile);
} }
@ -51,8 +50,7 @@ class FFProbe extends Binary
*/ */
public function probeStreams($pathfile) public function probeStreams($pathfile)
{ {
if ( ! is_file($pathfile)) if ( ! is_file($pathfile)) {
{
throw new Exception\InvalidFileArgumentException($pathfile); throw new Exception\InvalidFileArgumentException($pathfile);
} }
@ -69,19 +67,15 @@ class FFProbe extends Binary
*/ */
protected function executeProbe($command) protected function executeProbe($command)
{ {
try try {
{
$process = new Process($command); $process = new Process($command);
$process->run(); $process->run();
} } catch (\RuntimeException $e) {
catch (\RuntimeException $e)
{
throw new Exception\RuntimeException(sprintf('Failed to run the given command %s', $command)); throw new Exception\RuntimeException(sprintf('Failed to run the given command %s', $command));
} }
if ( ! $process->isSuccessful()) if ( ! $process->isSuccessful()) {
{
throw new Exception\RuntimeException(sprintf('Failed to probe %s', $command)); throw new Exception\RuntimeException(sprintf('Failed to probe %s', $command));
} }
@ -95,7 +89,6 @@ class FFProbe extends Binary
*/ */
protected static function getBinaryName() protected static function getBinaryName()
{ {
return 'ffprobe'; return array('avprobe', 'ffprobe');
} }
} }