Skip to content

Commit

Permalink
Merge pull request #703 from iljalukin/master
Browse files Browse the repository at this point in the history
Added initial parameters
  • Loading branch information
jens1o committed Mar 23, 2020
2 parents afc0e40 + 73fdec8 commit 984dbd0
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 5 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions src/FFMpeg/Format/Video/DefaultVideo.php
Expand Up @@ -35,6 +35,9 @@ abstract class DefaultVideo extends DefaultAudio implements VideoInterface
/** @var Array */
protected $additionalParamaters;

/** @var Array */
protected $initialParamaters;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -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}
*/
Expand Down
9 changes: 8 additions & 1 deletion src/FFMpeg/Format/VideoInterface.php
Expand Up @@ -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();
}
21 changes: 18 additions & 3 deletions src/FFMpeg/Media/AbstractVideo.php
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
}
4 changes: 3 additions & 1 deletion src/FFMpeg/Media/Clip.php
Expand Up @@ -4,6 +4,7 @@
use FFMpeg\Driver\FFMpegDriver;
use FFMpeg\FFProbe;
use FFMpeg\Coordinate\TimeCode;
use FFMpeg\Format\FormatInterface;

/**
* Video clip.
Expand Down Expand Up @@ -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()
Expand All @@ -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);

Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/Format/Video/InitialParametersTest.php
@@ -0,0 +1,16 @@
<?php

namespace Tests\FFMpeg\Unit\Format\Video;

use FFMpeg\Format\Video\X264;
use Tests\FFMpeg\Unit\TestCase;

class InitialParametersTest extends TestCase
{
public function testApplyInitialParameters()
{
$format = new X264();
$format->setInitialParameters(array('-acodec', 'libopus'));
$this->assertEquals(array('-acodec', 'libopus'), $format->getInitialParameters());
}
}

0 comments on commit 984dbd0

Please sign in to comment.