2012-04-13 10:20:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace FFMpeg;
|
|
|
|
|
|
|
|
|
|
abstract class Binary implements AdapterInterface
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
protected $binary;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @var \Monolog\Logger
|
|
|
|
|
*/
|
|
|
|
|
protected $logger;
|
|
|
|
|
|
|
|
|
|
public function __construct($binary, $logger = null)
|
|
|
|
|
{
|
|
|
|
|
$this->binary = $binary;
|
|
|
|
|
|
|
|
|
|
if ( ! $logger)
|
|
|
|
|
{
|
|
|
|
|
$logger = new \Monolog\Logger('default');
|
|
|
|
|
$logger->pushHandler(new \Monolog\Handler\NullHandler());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->logger = $logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function load(\Monolog\Logger $logger = null)
|
|
|
|
|
{
|
|
|
|
|
if ('' === $binary = self::autodetect(static::getBinaryName()))
|
|
|
|
|
{
|
2012-04-13 11:23:04 +02:00
|
|
|
throw new Exception\BinaryNotFoundException('Binary not found');
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new static($binary, $logger);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static function run($command, $bypass_errors = false)
|
|
|
|
|
{
|
|
|
|
|
$process = new \Symfony\Component\Process\Process($command);
|
|
|
|
|
$process->run();
|
|
|
|
|
|
|
|
|
|
if ( ! $process->isSuccessful() && ! $bypass_errors)
|
|
|
|
|
{
|
2012-04-13 11:23:04 +02:00
|
|
|
throw new \RuntimeException('Failed to execute ' . $command);
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result = $process->getOutput();
|
|
|
|
|
unset($process);
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Autodetect the presence of a binary
|
|
|
|
|
*
|
|
|
|
|
* @param string $binaryName
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected static function autodetect($binaryName)
|
|
|
|
|
{
|
|
|
|
|
return trim(self::run(sprintf('which %s', escapeshellarg($binaryName)), true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static function getBinaryName()
|
|
|
|
|
{
|
|
|
|
|
throw new Exception('Should be implemented');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|