From 73fdec8290c4db17ebfbf3f4bf856c0757388c6e Mon Sep 17 00:00:00 2001 From: Ilja Lukin Date: Mon, 24 Feb 2020 21:42:38 +0100 Subject: [PATCH] Added initial parameters --- README.md | 12 ++++++++ src/FFMpeg/Format/Video/DefaultVideo.php | 28 +++++++++++++++++++ src/FFMpeg/Format/VideoInterface.php | 9 +++++- src/FFMpeg/Media/AbstractVideo.php | 21 ++++++++++++-- src/FFMpeg/Media/Clip.php | 4 ++- .../Format/Video/InitialParametersTest.php | 16 +++++++++++ 6 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 tests/Unit/Format/Video/InitialParametersTest.php diff --git a/README.md b/README.md index 6d2beb89..9d6bcd54 100644 --- a/README.md +++ b/README.md @@ -554,6 +554,18 @@ $format->setAdditionalParameters(array('foo', 'bar')); $video->save($format, 'video.avi'); ``` +##### Add initial parameters + +You can also add initial parameters to your encoding requests based on your video format. This can be expecially handy in overriding a default input codec in FFMpeg. + +The argument of the setInitialParameters method is an array. + +```php +$format = new FFMpeg\Format\Video\X264(); +$format->setInitialParameters(array('-acodec', 'libopus')); +$video->save($format, 'video.avi'); +``` + ##### Create your own format The easiest way to create a format is to extend the abstract diff --git a/src/FFMpeg/Format/Video/DefaultVideo.php b/src/FFMpeg/Format/Video/DefaultVideo.php index 3b1eef8e..d45eaf88 100644 --- a/src/FFMpeg/Format/Video/DefaultVideo.php +++ b/src/FFMpeg/Format/Video/DefaultVideo.php @@ -35,6 +35,9 @@ abstract class DefaultVideo extends DefaultAudio implements VideoInterface /** @var Array */ protected $additionalParamaters; + /** @var Array */ + protected $initialParamaters; + /** * {@inheritdoc} */ @@ -122,6 +125,31 @@ public function setAdditionalParameters($additionalParamaters) return $this; } + /** + * {@inheritdoc} + */ + public function getInitialParameters() + { + return $this->initialParamaters; + } + + /** + * Sets initial parameters. + * + * @param array $initialParamaters + * @throws InvalidArgumentException + */ + public function setInitialParameters($initialParamaters) + { + if (!is_array($initialParamaters)) { + throw new InvalidArgumentException('Wrong initialParamaters value'); + } + + $this->initialParamaters = $initialParamaters; + + return $this; + } + /** * {@inheritdoc} */ diff --git a/src/FFMpeg/Format/VideoInterface.php b/src/FFMpeg/Format/VideoInterface.php index 329bff29..26204567 100644 --- a/src/FFMpeg/Format/VideoInterface.php +++ b/src/FFMpeg/Format/VideoInterface.php @@ -56,9 +56,16 @@ public function supportBFrames(); public function getAvailableVideoCodecs(); /** - * Returns the list of available video codecs for this format. + * Returns the list of additional parameters for this format. * * @return array */ public function getAdditionalParameters(); + + /** + * Returns the list of initial parameters for this format + * + * @return array + */ + public function getInitialParameters(); } diff --git a/src/FFMpeg/Media/AbstractVideo.php b/src/FFMpeg/Media/AbstractVideo.php index e02bbe93..339e4dac 100644 --- a/src/FFMpeg/Media/AbstractVideo.php +++ b/src/FFMpeg/Media/AbstractVideo.php @@ -134,7 +134,7 @@ public function getFinalCommand(FormatInterface $format, $outputPathfile) */ protected function buildCommand(FormatInterface $format, $outputPathfile) { - $commands = $this->basePartOfCommand(); + $commands = $this->basePartOfCommand($format); $filters = clone $this->filters; $filters->add(new SimpleFilter($format->getExtraParams(), 10)); @@ -283,10 +283,25 @@ protected function buildCommand(FormatInterface $format, $outputPathfile) /** * Return base part of command. * + * @param FormatInterface $format * @return array */ - protected function basePartOfCommand() + protected function basePartOfCommand(FormatInterface $format) { - return array('-y', '-i', $this->pathfile); + $commands = array('-y'); + + // If the user passed some initial parameters + if ($format instanceof VideoInterface) { + if (null !== $format->getInitialParameters()) { + foreach ($format->getInitialParameters() as $initialParameter) { + $commands[] = $initialParameter; + } + } + } + + $commands[] = '-i'; + $commands[] = $this->pathfile; + + return $commands; } } diff --git a/src/FFMpeg/Media/Clip.php b/src/FFMpeg/Media/Clip.php index 4435e562..095794cb 100644 --- a/src/FFMpeg/Media/Clip.php +++ b/src/FFMpeg/Media/Clip.php @@ -4,6 +4,7 @@ use FFMpeg\Driver\FFMpegDriver; use FFMpeg\FFProbe; use FFMpeg\Coordinate\TimeCode; +use FFMpeg\Format\FormatInterface; /** * Video clip. @@ -34,6 +35,7 @@ public function __construct(Video $video, FFMpegDriver $driver, FFProbe $ffprobe /** * Returns the video related to the frame. * + * @param FormatInterface $format * @return Video */ public function getVideo() @@ -46,7 +48,7 @@ public function getVideo() * * @return array */ - protected function basePartOfCommand() + protected function basePartOfCommand(FormatInterface $format) { $arr = array('-y', '-ss', (string) $this->start, '-i', $this->pathfile); diff --git a/tests/Unit/Format/Video/InitialParametersTest.php b/tests/Unit/Format/Video/InitialParametersTest.php new file mode 100644 index 00000000..932be079 --- /dev/null +++ b/tests/Unit/Format/Video/InitialParametersTest.php @@ -0,0 +1,16 @@ +setInitialParameters(array('-acodec', 'libopus')); + $this->assertEquals(array('-acodec', 'libopus'), $format->getInitialParameters()); + } +}