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-04-13 15:12:43 +02:00
|
|
|
use \Symfony\Component\Process\Process;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* FFMpeg driver
|
|
|
|
|
*
|
|
|
|
|
* @author Romain Neutron imprec@gmail.com
|
|
|
|
|
*/
|
2012-04-13 10:20:54 +02:00
|
|
|
class FFMpeg extends Binary
|
|
|
|
|
{
|
|
|
|
|
protected $pathfile;
|
|
|
|
|
|
2012-04-13 15:12:43 +02:00
|
|
|
/**
|
|
|
|
|
* Opens a file in order to be processed
|
|
|
|
|
*
|
|
|
|
|
* @param string $pathfile
|
2012-04-17 16:33:36 +02:00
|
|
|
* @return \FFMpeg\FFMpeg
|
2012-04-13 15:12:43 +02:00
|
|
|
* @throws Exception\InvalidFileArgumentException
|
|
|
|
|
*/
|
2012-04-13 10:20:54 +02:00
|
|
|
public function open($pathfile)
|
|
|
|
|
{
|
2012-05-11 00:30:02 +02:00
|
|
|
if ( ! file_exists($pathfile)) {
|
2012-04-13 10:20:54 +02:00
|
|
|
$this->logger->addError(sprintf('Request to open %s failed', $pathfile));
|
2012-04-13 15:12:43 +02:00
|
|
|
|
|
|
|
|
throw new Exception\InvalidFileArgumentException(sprintf('File %s does not exists', $pathfile));
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->logger->addInfo(sprintf('FFmpeg opens %s', $pathfile));
|
|
|
|
|
|
|
|
|
|
$this->pathfile = $pathfile;
|
2012-04-17 16:33:36 +02:00
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Close a file
|
|
|
|
|
*
|
|
|
|
|
* @return \FFMpeg\FFMpeg
|
|
|
|
|
*/
|
|
|
|
|
public function close()
|
|
|
|
|
{
|
|
|
|
|
$this->pathfile = null;
|
|
|
|
|
|
|
|
|
|
return $this;
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:12:43 +02:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param int $time The time in second where to take the snapshot
|
|
|
|
|
* @param string $output The pathfile where to write
|
2012-04-17 16:33:36 +02:00
|
|
|
* @return \FFMpeg\FFMpeg
|
2012-04-13 15:12:43 +02:00
|
|
|
* @throws Exception\RuntimeException
|
2012-04-13 15:21:52 +02:00
|
|
|
* @throws Exception\LogicException
|
2012-04-13 15:12:43 +02:00
|
|
|
*/
|
2012-04-18 10:36:29 +02:00
|
|
|
public function extractImage($time, $output)
|
2012-04-13 10:20:54 +02:00
|
|
|
{
|
2012-05-11 00:30:02 +02:00
|
|
|
if ( ! $this->pathfile) {
|
2012-04-13 15:21:52 +02:00
|
|
|
throw new Exception\LogicException('No file open');
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cmd = $this->binary
|
2012-05-11 00:30:02 +02:00
|
|
|
. ' -i ' . escapeshellarg($this->pathfile)
|
|
|
|
|
. ' -vframes 1 -ss ' . $time
|
|
|
|
|
. ' -f image2 ' . escapeshellarg($output);
|
2012-04-13 10:20:54 +02:00
|
|
|
|
|
|
|
|
$this->logger->addInfo(sprintf('Executing command %s', $cmd));
|
|
|
|
|
|
2012-04-13 15:12:43 +02:00
|
|
|
$process = new Process($cmd);
|
|
|
|
|
|
2012-05-11 00:30:02 +02:00
|
|
|
try {
|
2012-04-13 15:12:43 +02:00
|
|
|
$process->run();
|
2012-05-11 00:30:02 +02:00
|
|
|
} catch (\RuntimeException $e) {
|
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-04-13 10:20:54 +02:00
|
|
|
$this->logger->addError(sprintf('Command failed :: %s', $process->getErrorOutput()));
|
|
|
|
|
|
2012-04-13 15:12:43 +02:00
|
|
|
$this->cleanupTemporaryFile($output);
|
2012-04-13 10:20:54 +02:00
|
|
|
|
2012-04-13 15:12:43 +02:00
|
|
|
throw new Exception\RuntimeException('Failed to extract image');
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->logger->addInfo('Command run with success');
|
|
|
|
|
|
2012-04-17 16:33:36 +02:00
|
|
|
return $this;
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:12:43 +02:00
|
|
|
/**
|
|
|
|
|
* Encode the file to the specified format
|
|
|
|
|
*
|
|
|
|
|
* @param Format\AudioFormat $format The output format
|
|
|
|
|
* @param string $outputPathfile The pathfile where to write
|
|
|
|
|
* @param int $threads The number of threads to use
|
2012-04-17 16:33:36 +02:00
|
|
|
* @return \FFMpeg\FFMpeg
|
2012-04-13 15:12:43 +02:00
|
|
|
* @throws Exception\RuntimeException
|
2012-04-13 15:21:52 +02:00
|
|
|
* @throws Exception\LogicException
|
2012-04-13 15:12:43 +02:00
|
|
|
*/
|
2012-04-13 12:45:41 +02:00
|
|
|
public function encode(Format\AudioFormat $format, $outputPathfile, $threads = 1)
|
2012-04-13 10:20:54 +02:00
|
|
|
{
|
2012-05-11 00:30:02 +02:00
|
|
|
if ( ! $this->pathfile) {
|
2012-04-13 15:21:52 +02:00
|
|
|
throw new Exception\LogicException('No file open');
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$threads = max(min($threads, 64), 1);
|
|
|
|
|
|
2012-05-11 00:30:02 +02:00
|
|
|
switch (true) {
|
2012-04-13 14:15:56 +02:00
|
|
|
case $format instanceof Format\VideoFormat:
|
2012-04-17 16:33:36 +02:00
|
|
|
$this->encodeVideo($format, $outputPathfile, $threads);
|
2012-04-13 14:15:56 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
case $format instanceof Format\AudioFormat:
|
2012-04-17 16:33:36 +02:00
|
|
|
$this->encodeAudio($format, $outputPathfile, $threads);
|
2012-04-13 14:15:56 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-17 16:33:36 +02:00
|
|
|
return $this;
|
2012-04-13 14:15:56 +02:00
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:12:43 +02:00
|
|
|
/**
|
|
|
|
|
* Encode to audio
|
|
|
|
|
*
|
|
|
|
|
* @param Format\AudioFormat $format The output format
|
|
|
|
|
* @param string $outputPathfile The pathfile where to write
|
|
|
|
|
* @param int $threads The number of threads to use
|
2012-04-17 16:33:36 +02:00
|
|
|
* @return \FFMpeg\FFMpeg
|
2012-04-13 15:12:43 +02:00
|
|
|
* @throws Exception\RuntimeException
|
|
|
|
|
*/
|
2012-04-13 14:15:56 +02:00
|
|
|
protected function encodeAudio(Format\AudioFormat $format, $outputPathfile, $threads)
|
|
|
|
|
{
|
|
|
|
|
$cmd = $this->binary
|
2012-05-11 00:30:02 +02:00
|
|
|
. ' -y -i '
|
|
|
|
|
. escapeshellarg($this->pathfile)
|
|
|
|
|
. ' ' . $format->getExtraParams()
|
|
|
|
|
. ' -threads ' . $threads
|
|
|
|
|
. ' -acodec ' . $format->getAudioCodec()
|
|
|
|
|
. ' -ab ' . $format->getKiloBitrate() . 'k '
|
|
|
|
|
. ' -ac 2 -ar ' . $format->getAudioSampleRate()
|
|
|
|
|
. ' ' . escapeshellarg($outputPathfile);
|
2012-04-13 14:15:56 +02:00
|
|
|
|
2012-04-13 15:12:43 +02:00
|
|
|
$process = new Process($cmd);
|
|
|
|
|
|
2012-05-11 00:30:02 +02:00
|
|
|
try {
|
2012-04-13 15:12:43 +02:00
|
|
|
$process->run();
|
2012-05-11 00:30:02 +02:00
|
|
|
} catch (\RuntimeException $e) {
|
2012-04-13 15:12:43 +02:00
|
|
|
|
|
|
|
|
}
|
2012-04-13 14:15:56 +02:00
|
|
|
|
2012-05-11 00:30:02 +02:00
|
|
|
if ( ! $process->isSuccessful()) {
|
2012-04-13 15:12:43 +02:00
|
|
|
throw new Exception\RuntimeException(sprintf('Encoding failed : %s', $process->getErrorOutput()));
|
2012-04-13 14:15:56 +02:00
|
|
|
}
|
|
|
|
|
|
2012-04-17 16:33:36 +02:00
|
|
|
return $this;
|
2012-04-13 14:15:56 +02:00
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:12:43 +02:00
|
|
|
/**
|
|
|
|
|
* Encode to video
|
|
|
|
|
*
|
|
|
|
|
* @param Format\VideoFormat $format The output format
|
|
|
|
|
* @param string $outputPathfile The pathfile where to write
|
|
|
|
|
* @param int $threads The number of threads to use
|
2012-04-18 10:36:29 +02:00
|
|
|
* @return \FFMpeg\FFMpeg
|
2012-04-13 15:12:43 +02:00
|
|
|
* @throws Exception\RuntimeException
|
|
|
|
|
*/
|
2012-05-11 00:34:19 +02:00
|
|
|
protected function encodeVideo(Format\VideoFormat $format, $outputPathfile, $threads)
|
2012-04-13 14:15:56 +02:00
|
|
|
{
|
2012-04-13 10:20:54 +02:00
|
|
|
$cmd_part1 = $this->binary
|
2012-05-11 00:30:02 +02:00
|
|
|
. ' -y -i '
|
|
|
|
|
. escapeshellarg($this->pathfile) . ' '
|
|
|
|
|
. $format->getExtraParams() . ' ';
|
2012-04-13 10:20:54 +02:00
|
|
|
|
|
|
|
|
$cmd_part2 = ' -s ' . $format->getWidth() . 'x' . $format->getHeight()
|
2012-05-11 00:30:02 +02:00
|
|
|
. ' -r ' . $format->getFrameRate()
|
|
|
|
|
. ' -vcodec ' . $format->getVideoCodec()
|
|
|
|
|
. ' -b ' . $format->getKiloBitrate() . 'k -g 25 -bf 3'
|
|
|
|
|
. ' -threads ' . $threads
|
|
|
|
|
. ' -refs 6 -b_strategy 1 -coder 1 -qmin 10 -qmax 51 '
|
|
|
|
|
. ' -sc_threshold 40 -flags +loop -cmp +chroma'
|
|
|
|
|
. ' -me_range 16 -subq 7 -i_qfactor 0.71 -qcomp 0.6 -qdiff 4 '
|
|
|
|
|
. ' -trellis 1 -qscale 1 '
|
|
|
|
|
. '-acodec ' . $format->getAudioCodec() . ' -ab 92k ';
|
2012-04-13 10:20:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
$tmpFile = new \SplFileInfo(tempnam(sys_get_temp_dir(), 'temp') . '.' . pathinfo($outputPathfile, PATHINFO_EXTENSION));
|
|
|
|
|
|
|
|
|
|
$passes = array();
|
|
|
|
|
|
|
|
|
|
$passes[] = $cmd_part1 . ' -pass 1 ' . $cmd_part2
|
2012-05-11 00:30:02 +02:00
|
|
|
. ' -an ' . escapeshellarg($tmpFile->getPathname());
|
2012-04-13 10:20:54 +02:00
|
|
|
|
|
|
|
|
$passes[] = $cmd_part1 . ' -pass 2 ' . $cmd_part2
|
2012-05-11 00:30:02 +02:00
|
|
|
. ' -ac 2 -ar 44100 ' . escapeshellarg($outputPathfile);
|
2012-04-13 10:20:54 +02:00
|
|
|
|
2012-04-13 15:12:43 +02:00
|
|
|
$process = null;
|
|
|
|
|
|
2012-05-11 00:30:02 +02:00
|
|
|
foreach ($passes as $pass) {
|
2012-04-13 15:12:43 +02:00
|
|
|
$process = new Process($pass);
|
2012-04-13 10:20:54 +02:00
|
|
|
|
2012-05-11 00:30:02 +02:00
|
|
|
try {
|
2012-05-11 00:34:19 +02:00
|
|
|
$process->run();
|
2012-05-11 00:30:02 +02:00
|
|
|
} catch (\RuntimeException $e) {
|
2012-04-13 10:20:54 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->cleanupTemporaryFile($tmpFile->getPathname());
|
|
|
|
|
$this->cleanupTemporaryFile(getcwd() . '/ffmpeg2pass-0.log');
|
|
|
|
|
$this->cleanupTemporaryFile(getcwd() . '/ffmpeg2pass-0.log.mbtree');
|
|
|
|
|
|
2012-05-11 00:30:02 +02:00
|
|
|
if ( ! $process->isSuccessful()) {
|
2012-04-13 15:12:43 +02:00
|
|
|
throw new Exception\RuntimeException(sprintf('Encoding failed : %s', $process->getErrorOutput()));
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
2012-04-17 16:33:36 +02:00
|
|
|
return $this;
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:12:43 +02:00
|
|
|
/**
|
|
|
|
|
* Removes unnecessary file
|
|
|
|
|
*
|
|
|
|
|
* @param string $pathfile
|
|
|
|
|
*/
|
2012-04-13 10:20:54 +02:00
|
|
|
protected function cleanupTemporaryFile($pathfile)
|
|
|
|
|
{
|
2012-05-11 00:30:02 +02:00
|
|
|
if (file_exists($pathfile) && is_writable($pathfile)) {
|
2012-04-13 10:20:54 +02:00
|
|
|
unlink($pathfile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:12:43 +02:00
|
|
|
/**
|
|
|
|
|
* Return the binary name
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2012-04-13 10:20:54 +02:00
|
|
|
protected static function getBinaryName()
|
|
|
|
|
{
|
2012-05-11 00:30:02 +02:00
|
|
|
return array('avconv', 'ffmpeg');
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
}
|