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

382 lines
10 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\LogicException;
use FFMpeg\Exception\RuntimeException;
2012-05-25 20:54:54 +02:00
use FFMpeg\Format\Audio;
use FFMpeg\Format\Video;
2012-05-25 16:21:16 +02:00
use Symfony\Component\Process\Process;
2012-04-13 15:12:43 +02:00
/**
* FFMpeg driver
*
* @author Romain Neutron imprec@gmail.com
*/
2012-04-13 10:20:54 +02:00
class FFMpeg extends Binary
{
protected $pathfile;
/**
*
* @var FFProbe
*/
protected $prober;
2012-05-30 18:44:09 +02:00
/**
* Destructor
*/
public function __destruct()
{
$this->prober = null;
parent::__destruct();
}
2012-04-13 15:12:43 +02:00
/**
* Opens a file in order to be processed
*
2012-05-25 16:22:16 +02:00
* @param string $pathfile A pathfile
* @return \FFMpeg\FFMpeg
2012-05-25 16:21:16 +02:00
* @throws InvalidArgumentException
2012-04-13 15:12:43 +02:00
*/
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-05-25 16:21:16 +02:00
$this->logger->addError(sprintf('FFmpeg failed to open %s', $pathfile));
2012-04-13 15:12:43 +02:00
2012-05-25 16:21:16 +02:00
throw new InvalidArgumentException(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;
return $this;
}
/**
* Set a prober
*
* @return \FFMpeg\FFMpeg
*/
public function setProber(FFProbe $prober)
{
$this->prober = $prober;
return $this;
}
/**
* Close a file
*
* @return \FFMpeg\FFMpeg
*/
public function close()
{
2012-05-25 16:21:16 +02:00
$this->logger->addInfo(sprintf('FFmpeg closes %s', $this->pathfile));
$this->pathfile = null;
return $this;
2012-04-13 10:20:54 +02:00
}
2012-04-13 15:12:43 +02:00
/**
2012-05-30 18:44:09 +02:00
* Extract an image from a media file
2012-04-13 15:12:43 +02:00
*
2012-05-25 16:22:16 +02:00
* @param integer $time The time in second where to take the snapshot
* @param string $output The pathfile where to write
* @return \FFMpeg\FFMpeg
2012-05-25 16:21:16 +02:00
* @throws RuntimeException
* @throws LogicException
2012-04-13 15:12:43 +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-05-25 16:21:16 +02:00
throw new 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
2012-05-25 16:21:16 +02:00
$this->logger->addInfo(sprintf('FFmpeg executes command %s', $cmd));
2012-04-13 10:20:54 +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-05-31 16:16:35 +02:00
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->addError(sprintf('FFmpeg command failed : %s', $process->getErrorOutput()));
2012-04-13 10:20:54 +02:00
2012-04-13 15:12:43 +02:00
$this->cleanupTemporaryFile($output);
2012-04-13 10:20:54 +02:00
2012-05-25 16:21:16 +02:00
throw new RuntimeException('Failed to extract image');
2012-04-13 10:20:54 +02:00
}
2012-05-25 16:21:16 +02:00
$this->logger->addInfo(sprintf('FFmpeg command successful'));
2012-04-13 10:20:54 +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
*
2012-05-25 20:54:54 +02:00
* @param Audio $format The output format
2012-05-25 16:22:16 +02:00
* @param string $outputPathfile The pathfile where to write
* @param integer $threads The number of threads to use
* @return \FFMpeg\FFMpeg
2012-05-25 16:21:16 +02:00
* @throws RuntimeException
* @throws LogicException
2012-04-13 15:12:43 +02:00
*/
2012-05-25 20:54:54 +02:00
public function encode(Audio $format, $outputPathfile, $threads = 1)
2012-04-13 10:20:54 +02:00
{
2012-05-11 00:30:02 +02:00
if ( ! $this->pathfile) {
2012-05-25 16:21:16 +02:00
throw new 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-05-25 20:54:54 +02:00
case $format instanceof Video:
$this->encodeVideo($format, $outputPathfile, $threads);
2012-04-13 14:15:56 +02:00
break;
default:
2012-05-25 20:54:54 +02:00
case $format instanceof Audio:
$this->encodeAudio($format, $outputPathfile, $threads);
2012-04-13 14:15:56 +02:00
break;
}
return $this;
2012-04-13 14:15:56 +02:00
}
2012-04-13 15:12:43 +02:00
/**
* Encode to audio
*
2012-05-25 20:54:54 +02:00
* @param Audio $format The output format
2012-05-25 16:22:16 +02:00
* @param string $outputPathfile The pathfile where to write
* @param integer $threads The number of threads to use
* @return \FFMpeg\FFMpeg
2012-05-25 16:21:16 +02:00
* @throws RuntimeException
2012-04-13 15:12:43 +02:00
*/
2012-05-25 20:54:54 +02:00
protected function encodeAudio(Audio $format, $outputPathfile, $threads)
2012-04-13 14:15:56 +02:00
{
$cmd = $this->binary
2012-05-11 00:30:02 +02:00
. ' -y -i '
. escapeshellarg($this->pathfile)
. ' ' . $format->getExtraParams()
. ' -threads ' . $threads
. ' -ab ' . $format->getKiloBitrate() . 'k '
. ' ' . escapeshellarg($outputPathfile);
2012-04-13 14:15:56 +02:00
2012-05-30 15:06:53 +02:00
if ($format instanceof Audio\Transcodable) {
$cmd .= ' -acodec ' . $format->getAudioCodec();
}
2012-05-31 15:54:28 +02:00
2012-05-30 15:06:53 +02:00
if ($format instanceof Audio\Resamplable) {
$cmd .= ' -ac 2 -ar ' . $format->getAudioSampleRate();
}
2012-04-13 15:12:43 +02:00
$process = new Process($cmd);
2012-05-25 16:21:16 +02:00
$this->logger->addInfo(sprintf('FFmpeg executes command %s', $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-05-31 16:16:35 +02:00
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-05-25 16:21:16 +02:00
$this->logger->addInfo(sprintf('FFmpeg command failed'));
throw new RuntimeException(sprintf('Encoding failed : %s', $process->getErrorOutput()));
2012-04-13 14:15:56 +02:00
}
2012-05-25 16:21:16 +02:00
$this->logger->addInfo(sprintf('FFmpeg command successful'));
return $this;
2012-04-13 14:15:56 +02:00
}
2012-04-13 15:12:43 +02:00
/**
* Encode to video
*
2012-05-25 20:54:54 +02:00
* @param Video $format The output format
2012-05-25 16:22:16 +02:00
* @param string $outputPathfile The pathfile where to write
* @param integer $threads The number of threads to use
* @return \FFMpeg\FFMpeg
2012-05-25 16:21:16 +02:00
* @throws RuntimeException
2012-04-13 15:12:43 +02:00
*/
2012-05-25 20:54:54 +02:00
protected function encodeVideo(Video $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
2012-05-25 18:24:37 +02:00
$cmd_part2 = '';
2012-05-30 16:53:36 +02:00
if ($format instanceof Video\Resizable) {
if ( ! $this->prober) {
throw new LogicException('You must set a valid prober if you use RESIZEMODE_INSET');
}
2012-05-31 15:54:28 +02:00
$result = json_decode($this->prober->probeStreams($this->pathfile), true);
2012-05-30 15:06:53 +02:00
$originalWidth = $originalHeight = null;
foreach ($result as $stream) {
2012-05-31 15:54:28 +02:00
foreach ($stream as $name => $value) {
if ($name == 'width') {
2012-05-31 16:12:51 +02:00
$originalWidth = $value;
continue;
}
2012-05-31 15:54:28 +02:00
if ($name == 'value') {
2012-05-31 16:12:51 +02:00
$originalHeight = $value;
continue;
2012-05-29 10:32:37 +02:00
}
}
}
2012-05-30 15:06:53 +02:00
if ($originalHeight !== null && $originalWidth !== null) {
2012-05-30 16:53:36 +02:00
$dimensions = $format->getComputedDimensions($originalWidth, $originalHeight);
2012-05-30 15:06:53 +02:00
2012-05-30 16:53:36 +02:00
$width = $this->getMultiple($dimensions->getWidth(), 16);
$height = $this->getMultiple($dimensions->getHeight(), 16);
2012-05-30 15:06:53 +02:00
$cmd_part2 .= ' -s ' . $width . 'x' . $height;
}
}
if ($format instanceof Video\Resamplable) {
$cmd_part2 .= ' -r ' . $format->getFrameRate();
}
2012-05-30 15:06:53 +02:00
if ($format instanceof Video\Transcodable) {
$cmd_part2 .= ' -vcodec ' . $format->getVideoCodec();
2012-05-25 18:24:37 +02:00
}
2012-05-30 15:06:53 +02:00
$cmd_part2 .= ' -b ' . $format->getKiloBitrate() . 'k -g 25 -bf 3'
2012-05-11 00:30:02 +02:00
. ' -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 '
2012-05-30 15:06:53 +02:00
. ' -ab 92k ';
if ($format instanceof Audio\Transcodable) {
$cmd_part2 .= '-acodec ' . $format->getAudioCodec();
}
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-05-25 16:21:16 +02:00
$this->logger->addInfo(sprintf('FFmpeg executes command %s', $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');
2012-05-30 18:46:11 +02:00
$this->cleanupTemporaryFile(getcwd() . '/av2pass-0.log');
2012-04-13 10:20:54 +02:00
$this->cleanupTemporaryFile(getcwd() . '/ffmpeg2pass-0.log.mbtree');
2012-05-11 00:30:02 +02:00
if ( ! $process->isSuccessful()) {
2012-05-25 16:21:16 +02:00
$this->logger->addInfo(sprintf('FFmpeg command failed'));
throw new RuntimeException(sprintf('Encoding failed : %s', $process->getErrorOutput()));
2012-04-13 10:20:54 +02:00
}
2012-05-25 16:21:16 +02:00
$this->logger->addInfo(sprintf('FFmpeg command successful'));
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);
}
}
/**
* Returns the nearest multiple for a value
*
* @param integer $value
* @param integer $multiple
* @return integer
*/
protected function getMultiple($value, $multiple)
{
$modulo = $value % $multiple;
$ret = (int) $multiple;
$halfDistance = $multiple / 2;
if ($modulo <= $halfDistance)
$bound = 'bottom';
else
$bound = 'top';
switch ($bound) {
default:
case 'top':
$ret = $value + $multiple - $modulo;
break;
case 'bottom':
$ret = $value - $modulo;
break;
}
if ($ret < $multiple) {
$ret = (int) $multiple;
}
return (int) $ret;
}
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('avconv', 'ffmpeg');
2012-04-13 10:20:54 +02:00
}
}