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;
|
|
|
|
|
|
2013-06-25 10:03:20 +02:00
|
|
|
use Alchemy\BinaryDriver\ConfigurationInterface;
|
|
|
|
|
use FFMpeg\Driver\FFMpegDriver;
|
2012-05-25 16:21:16 +02:00
|
|
|
use FFMpeg\Exception\InvalidArgumentException;
|
2013-06-25 10:03:20 +02:00
|
|
|
use FFMpeg\Media\Audio;
|
|
|
|
|
use FFMpeg\Media\Video;
|
|
|
|
|
use Alchemy\BinaryDriver\Configuration;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
2012-04-13 15:12:43 +02:00
|
|
|
|
2013-06-25 10:03:20 +02:00
|
|
|
class FFMpeg
|
2012-04-13 10:20:54 +02:00
|
|
|
{
|
2013-06-25 10:03:20 +02:00
|
|
|
/** @var FFMpegDriver */
|
|
|
|
|
private $driver;
|
|
|
|
|
/** @var FFProbe */
|
|
|
|
|
private $ffprobe;
|
2012-11-25 15:40:20 +01:00
|
|
|
|
2013-06-25 10:03:20 +02:00
|
|
|
public function __construct(FFMpegDriver $ffmpeg, FFProbe $ffprobe)
|
2012-05-28 19:46:49 +02:00
|
|
|
{
|
2013-06-25 10:03:20 +02:00
|
|
|
$this->driver = $ffmpeg;
|
|
|
|
|
$this->ffprobe = $ffprobe;
|
2012-05-28 19:46:49 +02:00
|
|
|
}
|
2012-05-30 12:23:55 +02:00
|
|
|
|
2012-11-25 15:40:20 +01:00
|
|
|
/**
|
2013-06-25 10:03:20 +02:00
|
|
|
* Sets ffprobe
|
|
|
|
|
*
|
|
|
|
|
* @param FFProbe
|
|
|
|
|
*
|
|
|
|
|
* @return FFMpeg
|
2012-11-25 15:40:20 +01:00
|
|
|
*/
|
2013-06-25 10:03:20 +02:00
|
|
|
public function setFFProbe(FFProbe $ffprobe)
|
2012-11-25 15:40:20 +01:00
|
|
|
{
|
2013-06-25 10:03:20 +02:00
|
|
|
$this->ffprobe = $ffprobe;
|
2012-11-25 15:40:20 +01:00
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:12:43 +02:00
|
|
|
/**
|
2013-06-25 10:03:20 +02:00
|
|
|
* Gets FFProbe
|
2012-04-13 15:12:43 +02:00
|
|
|
*
|
2013-06-25 10:03:20 +02:00
|
|
|
* @return FFProbe
|
2012-04-13 15:12:43 +02:00
|
|
|
*/
|
2013-06-25 10:03:20 +02:00
|
|
|
public function getFFProbe()
|
2012-04-13 10:20:54 +02:00
|
|
|
{
|
2013-06-25 10:03:20 +02:00
|
|
|
return $this->ffprobe;
|
2012-04-17 16:33:36 +02:00
|
|
|
}
|
|
|
|
|
|
2012-05-28 19:46:49 +02:00
|
|
|
/**
|
2013-06-25 10:03:20 +02:00
|
|
|
* Sets ffmpeg driver
|
2012-05-30 12:23:55 +02:00
|
|
|
*
|
2013-06-25 10:03:20 +02:00
|
|
|
* @return FFMpeg
|
2012-05-28 19:46:49 +02:00
|
|
|
*/
|
2013-06-25 10:03:20 +02:00
|
|
|
public function setFFMpegDriver(FFMpegDriver $ffmpeg)
|
2012-05-28 19:46:49 +02:00
|
|
|
{
|
2013-06-25 10:03:20 +02:00
|
|
|
$this->driver = $ffmpeg;
|
2012-05-30 12:23:55 +02:00
|
|
|
|
2012-05-28 19:46:49 +02:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-17 16:33:36 +02:00
|
|
|
/**
|
2013-06-25 10:03:20 +02:00
|
|
|
* Gets the ffmpeg driver
|
2012-04-17 16:33:36 +02:00
|
|
|
*
|
2013-06-25 10:03:20 +02:00
|
|
|
* @return FFMpegDriver
|
2012-04-17 16:33:36 +02:00
|
|
|
*/
|
2013-06-25 10:03:20 +02:00
|
|
|
public function getFFMpegDriver()
|
2012-04-17 16:33:36 +02:00
|
|
|
{
|
2013-06-25 10:03:20 +02:00
|
|
|
return $this->driver;
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:12:43 +02:00
|
|
|
/**
|
2013-06-25 10:03:20 +02:00
|
|
|
* Opens a file in order to be processed
|
2012-04-13 15:12:43 +02:00
|
|
|
*
|
2013-06-25 10:03:20 +02:00
|
|
|
* @param string $pathfile A pathfile
|
2012-12-13 13:03:55 +01:00
|
|
|
*
|
2013-06-25 10:03:20 +02:00
|
|
|
* @return Audio|Video
|
2012-12-13 13:03:55 +01:00
|
|
|
*
|
2013-06-25 10:03:20 +02:00
|
|
|
* @throws InvalidArgumentException
|
2012-04-13 15:12:43 +02:00
|
|
|
*/
|
2013-06-25 10:03:20 +02:00
|
|
|
public function open($pathfile)
|
2012-04-13 10:20:54 +02:00
|
|
|
{
|
2013-06-25 10:03:20 +02:00
|
|
|
if (!file_exists($pathfile)) {
|
|
|
|
|
throw new InvalidArgumentException(sprintf('File %s does not exists', $pathfile));
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-25 10:03:20 +02:00
|
|
|
$streams = $this->ffprobe->streams($pathfile);
|
2012-04-13 10:20:54 +02:00
|
|
|
|
2013-06-25 10:03:20 +02:00
|
|
|
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);
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-25 10:03:20 +02:00
|
|
|
throw new InvalidArgumentException('Unable to detect file format, only audio and video supported');
|
2012-04-13 14:15:56 +02:00
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:12:43 +02:00
|
|
|
/**
|
2013-06-25 10:03:20 +02:00
|
|
|
* Creates a new FFMpeg instance
|
2012-04-13 15:12:43 +02:00
|
|
|
*
|
2013-06-25 10:03:20 +02:00
|
|
|
* @param array|ConfigurationInterface $configuration
|
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
|
* @param FFProbe $probe
|
2012-04-13 15:12:43 +02:00
|
|
|
*
|
2013-06-25 10:03:20 +02:00
|
|
|
* @return FFMpeg
|
2012-04-13 15:12:43 +02:00
|
|
|
*/
|
2013-06-25 10:03:20 +02:00
|
|
|
public static function create($configuration = array(), LoggerInterface $logger = null, FFProbe $probe = null)
|
2012-04-13 14:15:56 +02:00
|
|
|
{
|
2013-06-25 10:03:20 +02:00
|
|
|
if (!$configuration instanceof ConfigurationInterface) {
|
|
|
|
|
$configuration = new Configuration($configuration);
|
2012-05-30 15:06:53 +02:00
|
|
|
}
|
2012-04-13 10:20:54 +02:00
|
|
|
|
2013-06-25 10:03:20 +02:00
|
|
|
$binaries = $configuration->get('ffmpeg.binaries', array('avconv', 'ffmpeg'));
|
2013-05-04 22:18:47 +02:00
|
|
|
|
2013-06-25 10:03:20 +02:00
|
|
|
if (!$configuration->has('timeout')) {
|
|
|
|
|
$configuration->set('timeout', 300);
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-25 10:03:20 +02:00
|
|
|
$driver = FFMpegDriver::load($binaries, $logger, $configuration);
|
2012-04-13 10:20:54 +02:00
|
|
|
|
2013-06-25 10:03:20 +02:00
|
|
|
if (null === $probe) {
|
|
|
|
|
$probe = FFProbe::create($configuration, $logger, null);
|
2012-11-25 15:40:20 +01:00
|
|
|
}
|
|
|
|
|
|
2013-06-25 10:03:20 +02:00
|
|
|
return new static($driver, $probe);
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
}
|