Version 0.3
This commit is contained in:
parent
0d69145ec3
commit
ad3a5af623
130 changed files with 7283 additions and 2627 deletions
|
|
@ -11,466 +11,126 @@
|
|||
|
||||
namespace FFMpeg;
|
||||
|
||||
use Alchemy\BinaryDriver\ConfigurationInterface;
|
||||
use FFMpeg\Driver\FFMpegDriver;
|
||||
use FFMpeg\Exception\InvalidArgumentException;
|
||||
use FFMpeg\Exception\LogicException;
|
||||
use FFMpeg\Exception\RuntimeException;
|
||||
use FFMpeg\Format\AudioInterface;
|
||||
use FFMpeg\Format\VideoInterface;
|
||||
use FFMpeg\Format\Video\Resamplable as VideoResamplable;
|
||||
use FFMpeg\Format\Video\Resizable as VideoResizable;
|
||||
use FFMpeg\Format\Video\Transcodable as VideoTranscodable;
|
||||
use FFMpeg\Format\Audio\Resamplable as AudioResamplable;
|
||||
use FFMpeg\Format\Audio\Transcodable as AudioTranscodable;
|
||||
use FFMpeg\Helper\HelperInterface;
|
||||
use Symfony\Component\Process\Process;
|
||||
use Symfony\Component\Process\ProcessBuilder;
|
||||
use FFMpeg\Media\Audio;
|
||||
use FFMpeg\Media\Video;
|
||||
use Alchemy\BinaryDriver\Configuration;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* FFMpeg driver
|
||||
*
|
||||
* @author Romain Neutron imprec@gmail.com
|
||||
*/
|
||||
class FFMpeg extends Binary
|
||||
class FFMpeg
|
||||
{
|
||||
protected $pathfile;
|
||||
/** @var FFMpegDriver */
|
||||
private $driver;
|
||||
/** @var FFProbe */
|
||||
private $ffprobe;
|
||||
|
||||
public function __construct(FFMpegDriver $ffmpeg, FFProbe $ffprobe)
|
||||
{
|
||||
$this->driver = $ffmpeg;
|
||||
$this->ffprobe = $ffprobe;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets ffprobe
|
||||
*
|
||||
* @var FFProbe
|
||||
* @param FFProbe
|
||||
*
|
||||
* @return FFMpeg
|
||||
*/
|
||||
protected $prober;
|
||||
protected $threads = 1;
|
||||
|
||||
/**
|
||||
* @var HelperInterface[]
|
||||
*/
|
||||
protected $helpers = array();
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*/
|
||||
public function __destruct()
|
||||
public function setFFProbe(FFProbe $ffprobe)
|
||||
{
|
||||
$this->prober = null;
|
||||
parent::__destruct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param HelperInterface $helper
|
||||
* @return \FFMpeg\FFMpeg
|
||||
*/
|
||||
public function attachHelper(HelperInterface $helper)
|
||||
{
|
||||
$this->helpers[] = $helper;
|
||||
$helper->setProber($this->prober);
|
||||
|
||||
// ensure the helpers have the path to the file in case
|
||||
// they need to probe for format information
|
||||
if ($this->pathfile !== null) {
|
||||
$helper->open($this->pathfile);
|
||||
}
|
||||
$this->ffprobe = $ffprobe;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setThreads($threads)
|
||||
/**
|
||||
* Gets FFProbe
|
||||
*
|
||||
* @return FFProbe
|
||||
*/
|
||||
public function getFFProbe()
|
||||
{
|
||||
if ($threads > 64 || $threads < 1) {
|
||||
throw new InvalidArgumentException('Invalid `threads` value ; threads must fit in range 1 - 64');
|
||||
}
|
||||
return $this->ffprobe;
|
||||
}
|
||||
|
||||
$this->threads = (int) $threads;
|
||||
/**
|
||||
* Sets ffmpeg driver
|
||||
*
|
||||
* @return FFMpeg
|
||||
*/
|
||||
public function setFFMpegDriver(FFMpegDriver $ffmpeg)
|
||||
{
|
||||
$this->driver = $ffmpeg;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getThreads()
|
||||
/**
|
||||
* Gets the ffmpeg driver
|
||||
*
|
||||
* @return FFMpegDriver
|
||||
*/
|
||||
public function getFFMpegDriver()
|
||||
{
|
||||
return $this->threads;
|
||||
return $this->driver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a file in order to be processed
|
||||
*
|
||||
* @param string $pathfile A pathfile
|
||||
* @return \FFMpeg\FFMpeg
|
||||
* @param string $pathfile A pathfile
|
||||
*
|
||||
* @return Audio|Video
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function open($pathfile)
|
||||
{
|
||||
if (!file_exists($pathfile)) {
|
||||
$this->logger->addError(sprintf('FFmpeg failed to open %s', $pathfile));
|
||||
|
||||
throw new InvalidArgumentException(sprintf('File %s does not exist', $pathfile));
|
||||
throw new InvalidArgumentException(sprintf('File %s does not exists', $pathfile));
|
||||
}
|
||||
|
||||
$this->logger->addInfo(sprintf('FFmpeg opens %s', $pathfile));
|
||||
$this->pathfile = $pathfile;
|
||||
$streams = $this->ffprobe->streams($pathfile);
|
||||
|
||||
foreach ($this->helpers as $helper) {
|
||||
$helper->open($pathfile);
|
||||
if (0 < count($streams->videos())) {
|
||||
return new Video($pathfile, $this->driver, $this->ffprobe);
|
||||
} elseif (0 < count($streams->audios())) {
|
||||
return new Audio($pathfile, $this->driver, $this->ffprobe);
|
||||
}
|
||||
|
||||
return $this;
|
||||
throw new InvalidArgumentException('Unable to detect file format, only audio and video supported');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a prober
|
||||
* Creates a new FFMpeg instance
|
||||
*
|
||||
* @return \FFMpeg\FFMpeg
|
||||
* @param array|ConfigurationInterface $configuration
|
||||
* @param LoggerInterface $logger
|
||||
* @param FFProbe $probe
|
||||
*
|
||||
* @return FFMpeg
|
||||
*/
|
||||
public function setProber(FFProbe $prober)
|
||||
public static function create($configuration = array(), LoggerInterface $logger = null, FFProbe $probe = null)
|
||||
{
|
||||
$this->prober = $prober;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close a file
|
||||
*
|
||||
* @return \FFMpeg\FFMpeg
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
$this->logger->addInfo(sprintf('FFmpeg closes %s', $this->pathfile));
|
||||
|
||||
$this->pathfile = null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract an image from a media file
|
||||
*
|
||||
* @param integer|string $time The time where to take the snapshot, time could either be in second or in hh:mm:ss[.xxx] form.
|
||||
* @param string $output The pathfile where to write
|
||||
* @param Boolean $accurate Whether to decode the whole video until position or seek and extract. See -ss option in FFMpeg manual (http://ffmpeg.org/ffmpeg.html#Main-options)
|
||||
*
|
||||
* @return \FFMpeg\FFMpeg
|
||||
*
|
||||
* @throws RuntimeException
|
||||
* @throws LogicException
|
||||
*/
|
||||
public function extractImage($time, $output, $accurate = false)
|
||||
{
|
||||
if (!$this->pathfile) {
|
||||
throw new LogicException('No file open');
|
||||
if (!$configuration instanceof ConfigurationInterface) {
|
||||
$configuration = new Configuration($configuration);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see http://ffmpeg.org/ffmpeg.html#Main-options
|
||||
*/
|
||||
if (!$accurate) {
|
||||
$options = array(
|
||||
$this->binary, '-ss', $time,
|
||||
'-i', $this->pathfile,
|
||||
'-vframes', '1',
|
||||
'-f', 'image2', $output
|
||||
);
|
||||
} else {
|
||||
$options = array(
|
||||
$this->binary,
|
||||
'-i', $this->pathfile,
|
||||
'-vframes', '1', '-ss', $time,
|
||||
'-f', 'image2', $output
|
||||
);
|
||||
$binaries = $configuration->get('ffmpeg.binaries', array('avconv', 'ffmpeg'));
|
||||
|
||||
if (!$configuration->has('timeout')) {
|
||||
$configuration->set('timeout', 300);
|
||||
}
|
||||
|
||||
$builder = ProcessBuilder::create($options);
|
||||
$process = $builder->getProcess();
|
||||
$process->setTimeout($this->timeout);
|
||||
|
||||
$this->logger->addInfo(sprintf('FFmpeg executes command %s', $process->getCommandline()));
|
||||
|
||||
try {
|
||||
$process->run(array($this, 'transcodeCallback'));
|
||||
} catch (\RuntimeException $e) {
|
||||
$driver = FFMpegDriver::load($binaries, $logger, $configuration);
|
||||
|
||||
if (null === $probe) {
|
||||
$probe = FFProbe::create($configuration, $logger, null);
|
||||
}
|
||||
|
||||
if (!$process->isSuccessful()) {
|
||||
$this->logger->addError(sprintf('FFmpeg command failed: %s', $process->getErrorOutput()));
|
||||
|
||||
$this->cleanupTemporaryFile($output);
|
||||
|
||||
throw new RuntimeException('Failed to extract image');
|
||||
}
|
||||
|
||||
$this->logger->addInfo(sprintf('FFmpeg command successful'));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the file to the specified format
|
||||
*
|
||||
* @param AudioInterface $format The output format
|
||||
* @param string $outputPathfile The pathfile where to write
|
||||
* @return \FFMpeg\FFMpeg
|
||||
* @throws RuntimeException
|
||||
* @throws LogicException
|
||||
*/
|
||||
public function encode(AudioInterface $format, $outputPathfile)
|
||||
{
|
||||
if (!$this->pathfile) {
|
||||
throw new LogicException('No file open');
|
||||
}
|
||||
|
||||
switch (true) {
|
||||
case $format instanceof VideoInterface:
|
||||
$this->encodeVideo($format, $outputPathfile);
|
||||
break;
|
||||
default:
|
||||
case $format instanceof AudioInterface:
|
||||
$this->encodeAudio($format, $outputPathfile);
|
||||
break;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode to audio
|
||||
*
|
||||
* @param Audio $format The output format
|
||||
* @param string $outputPathfile The pathfile where to write
|
||||
* @return \FFMpeg\FFMpeg
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
protected function encodeAudio(AudioInterface $format, $outputPathfile)
|
||||
{
|
||||
$builder = ProcessBuilder::create(array(
|
||||
$this->binary,
|
||||
'-y', '-i',
|
||||
$this->pathfile,
|
||||
'-threads', $this->threads,
|
||||
'-ab', $format->getKiloBitrate() . 'k ',
|
||||
));
|
||||
|
||||
foreach ($format->getExtraParams() as $parameter) {
|
||||
$builder->add($parameter);
|
||||
}
|
||||
|
||||
if ($format instanceof AudioTranscodable) {
|
||||
$builder->add('-acodec')->add($format->getAudioCodec());
|
||||
}
|
||||
|
||||
if ($format instanceof AudioResamplable) {
|
||||
$builder->add('-ac')->add(2)->add('-ar')->add($format->getAudioSampleRate());
|
||||
}
|
||||
|
||||
$builder->add($outputPathfile);
|
||||
|
||||
$process = $builder->getProcess();
|
||||
|
||||
$process->setTimeout($this->timeout);
|
||||
|
||||
$this->logger->addInfo(sprintf('FFmpeg executes command %s', $process->getCommandLine()));
|
||||
|
||||
try {
|
||||
$process->run(array($this, 'transcodeCallback'));
|
||||
} catch (\RuntimeException $e) {
|
||||
|
||||
}
|
||||
|
||||
if (!$process->isSuccessful()) {
|
||||
$this->logger->addInfo(sprintf('FFmpeg command failed'));
|
||||
throw new RuntimeException(sprintf('Encoding failed: %s', $process->getErrorOutput()));
|
||||
}
|
||||
|
||||
$this->logger->addInfo(sprintf('FFmpeg command successful'));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode to video
|
||||
*
|
||||
* @param VideoInterface $format The output format
|
||||
* @param string $outputPathfile The pathfile where to write
|
||||
* @return \FFMpeg\FFMpeg
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
protected function encodeVideo(VideoInterface $format, $outputPathfile)
|
||||
{
|
||||
$builder = ProcessBuilder::create(array(
|
||||
$this->binary, '-y', '-i',
|
||||
$this->pathfile
|
||||
));
|
||||
|
||||
foreach ($format->getExtraParams() as $parameter) {
|
||||
$builder->add($parameter);
|
||||
}
|
||||
|
||||
if ($format instanceof VideoResizable) {
|
||||
if (!$this->prober) {
|
||||
throw new LogicException('You must set a valid prober if you use a resizable format');
|
||||
}
|
||||
|
||||
$result = json_decode($this->prober->probeStreams($this->pathfile), true);
|
||||
|
||||
$originalWidth = $originalHeight = null;
|
||||
|
||||
foreach ($result as $stream) {
|
||||
foreach ($stream as $name => $value) {
|
||||
if ($name == 'width') {
|
||||
$originalWidth = $value;
|
||||
continue;
|
||||
}
|
||||
if ($name == 'height') {
|
||||
$originalHeight = $value;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($originalHeight !== null && $originalWidth !== null) {
|
||||
$this->logger->addInfo(sprintf('Read dimension for resizing succesful : %s x %s', $originalWidth, $originalHeight));
|
||||
} else {
|
||||
$this->logger->addInfo(sprintf('Read dimension for resizing failed !'));
|
||||
}
|
||||
|
||||
if ($originalHeight !== null && $originalWidth !== null) {
|
||||
$dimensions = $format->getComputedDimensions($originalWidth, $originalHeight);
|
||||
$width = $this->getMultiple($dimensions->getWidth(), $format->getModulus());
|
||||
$height = $this->getMultiple($dimensions->getHeight(), $format->getModulus());
|
||||
|
||||
$builder->add('-s')->add($width . 'x' . $height);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ($format instanceof VideoResamplable) {
|
||||
$builder->add('-r')->add($format->getFrameRate());
|
||||
|
||||
/**
|
||||
* @see http://sites.google.com/site/linuxencoding/x264-ffmpeg-mapping
|
||||
*/
|
||||
if ($format->supportBFrames()) {
|
||||
$builder->add('-b_strategy')
|
||||
->add('1')
|
||||
->add('-bf')
|
||||
->add('3')
|
||||
->add('-g')
|
||||
->add($format->getGOPSize());
|
||||
}
|
||||
}
|
||||
|
||||
if ($format instanceof VideoTranscodable) {
|
||||
$builder->add('-vcodec')->add($format->getVideoCodec());
|
||||
}
|
||||
|
||||
$builder->add('-b:v')->add($format->getKiloBitrate() . 'k')
|
||||
->add('-threads')->add($this->threads)
|
||||
->add('-refs')->add('6')
|
||||
->add('-coder')->add('1')
|
||||
->add('-sc_threshold')->add('40')
|
||||
->add('-flags')->add('+loop')
|
||||
->add('-me_range')->add('16')
|
||||
->add('-subq')->add('7')
|
||||
->add('-i_qfactor')->add('0.71')
|
||||
->add('-qcomp')->add('0.6')
|
||||
->add('-qdiff')->add('4')
|
||||
->add('-trellis')->add('1')
|
||||
->add('-b:a')->add('92k');
|
||||
|
||||
if ($format instanceof AudioTranscodable) {
|
||||
$builder->add('-acodec')->add($format->getAudioCodec());
|
||||
}
|
||||
|
||||
$passPrefix = uniqid('pass-');
|
||||
|
||||
$pass1 = $builder;
|
||||
$pass2 = clone $builder;
|
||||
|
||||
$passes[] = $pass1
|
||||
->add('-pass')->add('1')
|
||||
->add('-passlogfile')->add($passPrefix)
|
||||
->add('-an')->add($outputPathfile)
|
||||
->getProcess();
|
||||
$passes[] = $pass2
|
||||
->add('-pass')->add('2')
|
||||
->add('-passlogfile')->add($passPrefix)
|
||||
->add('-ac')->add('2')
|
||||
->add('-ar')->add('44100')->add($outputPathfile)
|
||||
->getProcess();
|
||||
|
||||
foreach ($passes as $process) {
|
||||
|
||||
$process->setTimeout($this->timeout);
|
||||
|
||||
$this->logger->addInfo(sprintf('FFmpeg executes command %s', $process->getCommandline()));
|
||||
|
||||
try {
|
||||
$process->run(array($this, 'transcodeCallback'));
|
||||
} catch (\RuntimeException $e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$this->cleanupTemporaryFile(getcwd() . '/' . $passPrefix . '-0.log');
|
||||
$this->cleanupTemporaryFile(getcwd() . '/' . $passPrefix . '-0.log');
|
||||
$this->cleanupTemporaryFile(getcwd() . '/' . $passPrefix . '-0.log.mbtree');
|
||||
|
||||
if (!$process->isSuccessful()) {
|
||||
$this->logger->addInfo(sprintf('FFmpeg command failed'));
|
||||
throw new RuntimeException(sprintf('Encoding failed : %s', $process->getErrorOutput()));
|
||||
}
|
||||
|
||||
$this->logger->addInfo(sprintf('FFmpeg command successful'));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The main transcoding callback, delegates the content to the helpers.
|
||||
*
|
||||
* @param string $channel (stdio|stderr)
|
||||
* @param string $content the current line of the ffmpeg output
|
||||
*/
|
||||
public function transcodeCallback($channel, $content)
|
||||
{
|
||||
foreach ($this->helpers as $helper) {
|
||||
$helper->transcodeCallback($channel, $content);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes unnecessary file
|
||||
*
|
||||
* @param string $pathfile
|
||||
*/
|
||||
protected function cleanupTemporaryFile($pathfile)
|
||||
{
|
||||
if (file_exists($pathfile) && is_writable($pathfile)) {
|
||||
unlink($pathfile);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the nearest multiple for a value
|
||||
*
|
||||
* @param integer $value
|
||||
* @param integer $multiple
|
||||
* @return integer
|
||||
*/
|
||||
protected function getMultiple($value, $multiple)
|
||||
{
|
||||
while (0 !== $value % $multiple) {
|
||||
$value++;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function getBinaryName()
|
||||
{
|
||||
return array('avconv', 'ffmpeg');
|
||||
return new static($driver, $probe);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue