Skip to content

Commit 6ba011d

Browse files
authored
Methods getAdditionalParams & setAdditionalParams which allow the user to pass additional parameters to the encoding request (PHP-FFMpeg#284)
* Modification of the format Video to add additional parameters based on user's desire * Update of the README * Working version of this feature. Still needs tests * Fixing the tests of FFMPeg\Media\Video * Setting up tests for the additionalParams feature * Correction des tests * Modifying tests. They work locally but not on Travis. * Still trying to understand why Travis is throwing errors when PHPUnit is not. * Add the additional params at the end of the command * Fixed the tests and the way we add the parameters * We remove log files
1 parent 2b5d18f commit 6ba011d

File tree

5 files changed

+136
-7
lines changed

5 files changed

+136
-7
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,18 @@ $video->save($format, 'video.avi');
441441

442442
The callback provided for the event can be any callable.
443443

444+
##### Add additional parameters
445+
446+
You can add additional parameters to your encoding requests based on your video format.
447+
448+
The argument of the setAdditionalParameters method is an array.
449+
450+
```php
451+
$format = new Format\Video\X264();
452+
$format->setAdditionalParameters(array('foo', 'bar'));
453+
$video->save($format, 'video.avi');
454+
```
455+
444456
##### Create your own format
445457

446458
The easiest way to create a format is to extend the abstract

src/FFMpeg/Format/Video/DefaultVideo.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ abstract class DefaultVideo extends DefaultAudio implements VideoInterface
3232
/** @var Integer */
3333
protected $modulus = 16;
3434

35+
/** @var Array */
36+
protected $additionalParamaters;
37+
3538
/**
3639
* {@inheritdoc}
3740
*/
@@ -94,6 +97,31 @@ public function getModulus()
9497
return $this->modulus;
9598
}
9699

100+
/**
101+
* {@inheritdoc}
102+
*/
103+
public function getAdditionalParameters()
104+
{
105+
return $this->additionalParamaters;
106+
}
107+
108+
/**
109+
* Sets additional parameters.
110+
*
111+
* @param array $additionalParamaters
112+
* @throws InvalidArgumentException
113+
*/
114+
public function setAdditionalParameters($additionalParamaters)
115+
{
116+
if (!is_array($additionalParamaters)) {
117+
throw new InvalidArgumentException('Wrong additionalParamaters value');
118+
}
119+
120+
$this->additionalParamaters = $additionalParamaters;
121+
122+
return $this;
123+
}
124+
97125
/**
98126
* {@inheritdoc}
99127
*/

src/FFMpeg/Format/VideoInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,11 @@ public function supportBFrames();
5454
* @return array
5555
*/
5656
public function getAvailableVideoCodecs();
57+
58+
/**
59+
* Returns the list of available video codecs for this format.
60+
*
61+
* @return array
62+
*/
63+
public function getAdditionalParameters();
5764
}

src/FFMpeg/Media/Video.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ public function save(FormatInterface $format, $outputPathfile)
120120
}
121121
}
122122

123+
// If the user passed some additional parameters
124+
if ($format instanceof VideoInterface) {
125+
if (null !== $format->getAdditionalParameters()) {
126+
foreach ($format->getAdditionalParameters() as $additionalParameter) {
127+
$commands[] = $additionalParameter;
128+
}
129+
}
130+
}
131+
123132
$fs = FsManager::create();
124133
$fsId = uniqid('ffmpeg-passes');
125134
$passPrefix = $fs->createTemporaryDirectory(0777, 50, $fsId) . '/' . uniqid('pass-');

tests/Unit/Media/VideoTest.php

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,29 @@ public function provideSaveData()
249249
$format->expects($this->any())
250250
->method('getPasses')
251251
->will($this->returnValue(2));
252+
$format->expects($this->any())
253+
->method('getAdditionalParameters')
254+
->will($this->returnValue(array('foo', 'bar')));
255+
256+
$format2 = $this->getMock('FFMpeg\Format\VideoInterface');
257+
$format2->expects($this->any())
258+
->method('getExtraParams')
259+
->will($this->returnValue(array()));
260+
$format2->expects($this->any())
261+
->method('getKiloBitrate')
262+
->will($this->returnValue(663));
263+
$format2->expects($this->any())
264+
->method('getAudioKiloBitrate')
265+
->will($this->returnValue(92));
266+
$format2->expects($this->any())
267+
->method('getAudioChannels')
268+
->will($this->returnValue(2));
269+
$format2->expects($this->any())
270+
->method('getPasses')
271+
->will($this->returnValue(2));
272+
$format2->expects($this->any())
273+
->method('getAdditionalParameters')
274+
->will($this->returnValue(array('foo', 'bar')));
252275

253276
$audioFormat = $this->getMock('FFMpeg\Format\AudioInterface');
254277
$audioFormat->expects($this->any())
@@ -289,6 +312,9 @@ public function provideSaveData()
289312
$audioVideoFormat->expects($this->any())
290313
->method('getPasses')
291314
->will($this->returnValue(2));
315+
$audioVideoFormat->expects($this->any())
316+
->method('getAdditionalParameters')
317+
->will($this->returnValue(array()));
292318

293319
$audioVideoFormatSinglePass = $this->getMock('FFMpeg\Format\VideoInterface');
294320
$audioVideoFormatSinglePass->expects($this->any())
@@ -312,6 +338,9 @@ public function provideSaveData()
312338
$audioVideoFormatSinglePass->expects($this->any())
313339
->method('getPasses')
314340
->will($this->returnValue(1));
341+
$audioVideoFormatSinglePass->expects($this->any())
342+
->method('getAdditionalParameters')
343+
->will($this->returnValue(array()));
315344

316345
$formatExtra = $this->getMock('FFMpeg\Format\VideoInterface');
317346
$formatExtra->expects($this->any())
@@ -329,6 +358,29 @@ public function provideSaveData()
329358
$formatExtra->expects($this->any())
330359
->method('getPasses')
331360
->will($this->returnValue(2));
361+
$formatExtra->expects($this->any())
362+
->method('getAdditionalParameters')
363+
->will($this->returnValue(array()));
364+
365+
$formatExtra2 = $this->getMock('FFMpeg\Format\VideoInterface');
366+
$formatExtra2->expects($this->any())
367+
->method('getExtraParams')
368+
->will($this->returnValue(array('extra', 'param')));
369+
$formatExtra2->expects($this->any())
370+
->method('getKiloBitrate')
371+
->will($this->returnValue(665));
372+
$formatExtra2->expects($this->any())
373+
->method('getAudioKiloBitrate')
374+
->will($this->returnValue(92));
375+
$formatExtra2->expects($this->any())
376+
->method('getAudioChannels')
377+
->will($this->returnValue(2));
378+
$formatExtra2->expects($this->any())
379+
->method('getPasses')
380+
->will($this->returnValue(2));
381+
$formatExtra2->expects($this->any())
382+
->method('getAdditionalParameters')
383+
->will($this->returnValue(array()));
332384

333385
$listeners = array($this->getMock('Alchemy\BinaryDriver\Listeners\ListenerInterface'));
334386

@@ -353,6 +405,27 @@ public function provideSaveData()
353405
->method('getPasses')
354406
->will($this->returnValue(2));
355407

408+
$progressableFormat2 = $this->getMockBuilder('Tests\FFMpeg\Unit\Media\Prog')
409+
->disableOriginalConstructor()->getMock();
410+
$progressableFormat2->expects($this->any())
411+
->method('getExtraParams')
412+
->will($this->returnValue(array()));
413+
$progressableFormat2->expects($this->any())
414+
->method('createProgressListener')
415+
->will($this->returnValue($listeners));
416+
$progressableFormat2->expects($this->any())
417+
->method('getKiloBitrate')
418+
->will($this->returnValue(666));
419+
$progressableFormat2->expects($this->any())
420+
->method('getAudioKiloBitrate')
421+
->will($this->returnValue(92));
422+
$progressableFormat2->expects($this->any())
423+
->method('getAudioChannels')
424+
->will($this->returnValue(2));
425+
$progressableFormat2->expects($this->any())
426+
->method('getPasses')
427+
->will($this->returnValue(2));
428+
356429
$progressableAudioFormat = $this->getMockBuilder('Tests\FFMpeg\Unit\Media\AudioProg')
357430
->disableOriginalConstructor()->getMock();
358431
$progressableAudioFormat->expects($this->any())
@@ -379,14 +452,14 @@ public function provideSaveData()
379452
'-y', '-i', __FILE__, '-b:v', '663k',
380453
'-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop',
381454
'-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6',
382-
'-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-ac', '2', '-pass', '1', '-passlogfile',
455+
'-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-ac', 2, 'foo', 'bar', '-pass', 1, '-passlogfile',
383456
'/target/file',
384457
), array(
385458
'-y', '-i', __FILE__,
386459
'-b:v', '663k',
387460
'-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop',
388461
'-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6',
389-
'-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-ac', '2', '-pass', '2', '-passlogfile',
462+
'-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-ac', 2, 'foo', 'bar', '-pass', 2, '-passlogfile',
390463
'/target/file',
391464
)), null, $format),
392465
array(false, array(array(
@@ -436,17 +509,17 @@ public function provideSaveData()
436509
'-threads', 24, '-b:v', '663k',
437510
'-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop',
438511
'-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6',
439-
'-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-ac', '2', '-pass', '1', '-passlogfile',
512+
'-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-ac', 2, 'foo', 'bar', '-pass', 1, '-passlogfile',
440513
'/target/file',
441514
), array(
442515
'-y', '-i', __FILE__,
443516
'-threads', 24,
444517
'-b:v', '663k',
445518
'-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop',
446519
'-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6',
447-
'-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-ac', '2', '-pass', '2', '-passlogfile',
520+
'-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-ac', 2, 'foo', 'bar', '-pass', 2, '-passlogfile',
448521
'/target/file',
449-
)), null, $format),
522+
)), null, $format2),
450523
array(true, array(array(
451524
'-y', '-i', __FILE__,
452525
'extra', 'param', '-threads', 24, '-b:v', '665k',
@@ -461,7 +534,7 @@ public function provideSaveData()
461534
'-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6',
462535
'-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-ac', '2', '-pass', '2', '-passlogfile',
463536
'/target/file',
464-
)), null, $formatExtra),
537+
)), null, $formatExtra2),
465538
array(false, array(array(
466539
'-y', '-i', __FILE__, '-b:v', '666k',
467540
'-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop',
@@ -475,7 +548,7 @@ public function provideSaveData()
475548
'-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6',
476549
'-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-ac', '2', '-pass', '2', '-passlogfile',
477550
'/target/file',
478-
)), $listeners, $progressableFormat),
551+
)), $listeners, $progressableFormat2),
479552
array(true, array(array(
480553
'-y', '-i', __FILE__,
481554
'-threads', 24, '-b:v', '666k',

0 commit comments

Comments
 (0)