From d7cf9231af471b7c5057d4b93b13334a926a51cd Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Tue, 25 Jun 2013 21:43:01 +0200 Subject: [PATCH] Add ability to share audio filters with video --- src/FFMpeg/Media/Audio.php | 8 ++++- src/FFMpeg/Media/Video.php | 15 +++----- tests/FFMpeg/Tests/Media/AudioTest.php | 21 +++++++++++ tests/FFMpeg/Tests/Media/VideoTest.php | 49 ++++++++++++++++++-------- 4 files changed, 68 insertions(+), 25 deletions(-) diff --git a/src/FFMpeg/Media/Audio.php b/src/FFMpeg/Media/Audio.php index b20187b..961c6b0 100644 --- a/src/FFMpeg/Media/Audio.php +++ b/src/FFMpeg/Media/Audio.php @@ -15,7 +15,9 @@ use Alchemy\BinaryDriver\Exception\ExecutionFailureException; use FFMpeg\Filters\Audio\AudioFilters; use FFMpeg\Format\FormatInterface; use FFMpeg\Exception\RuntimeException; +use FFMpeg\Exception\InvalidArgumentException; use FFMpeg\Filters\Audio\AudioFilterInterface; +use FFMpeg\Filters\FilterInterface; use FFMpeg\Format\ProgressableInterface; class Audio extends AbstractStreamableMedia @@ -35,8 +37,12 @@ class Audio extends AbstractStreamableMedia * * @return Audio */ - public function addFilter(AudioFilterInterface $filter) + public function addFilter(FilterInterface $filter) { + if (!$filter instanceof AudioFilterInterface) { + throw new InvalidArgumentException('Audio only accepts AudioFilterInterface filters'); + } + $this->filters->add($filter); return $this; diff --git a/src/FFMpeg/Media/Video.php b/src/FFMpeg/Media/Video.php index e34e939..f98d0f2 100644 --- a/src/FFMpeg/Media/Video.php +++ b/src/FFMpeg/Media/Video.php @@ -15,12 +15,12 @@ use Alchemy\BinaryDriver\Exception\ExecutionFailureException; use FFMpeg\Coordinate\TimeCode; use FFMpeg\Exception\RuntimeException; use FFMpeg\Filters\Video\VideoFilters; -use FFMpeg\Filters\Video\VideoFilterInterface; -use FFMpeg\Format\VideoInterface; +use FFMpeg\Filters\FilterInterface; +use FFMpeg\Format\FormatInterface; use FFMpeg\Format\ProgressableInterface; use FFMpeg\Media\Frame; -class Video extends AbstractStreamableMedia +class Video extends Audio { /** * {@inheritdoc} @@ -37,7 +37,7 @@ class Video extends AbstractStreamableMedia * * @return Video */ - public function addFilter(VideoFilterInterface $filter) + public function addFilter(FilterInterface $filter) { $this->filters->add($filter); @@ -54,7 +54,7 @@ class Video extends AbstractStreamableMedia * * @throws RuntimeException */ - public function save(VideoInterface $format, $outputPathfile) + public function save(FormatInterface $format, $outputPathfile) { $commands = array_merge(array('-y', '-i', $this->pathfile), $format->getExtraParams()); @@ -110,17 +110,12 @@ class Video extends AbstractStreamableMedia $pass1[] = '1'; $pass1[] = '-passlogfile'; $pass1[] = $passPrefix; - $pass1[] = '-an'; $pass1[] = $outputPathfile; $pass2[] = '-pass'; $pass2[] = '2'; $pass2[] = '-passlogfile'; $pass2[] = $passPrefix; - $pass2[] = '-ac'; - $pass2[] = '2'; - $pass2[] = '-ar'; - $pass2[] = '44100'; $pass2[] = $outputPathfile; $failure = null; diff --git a/tests/FFMpeg/Tests/Media/AudioTest.php b/tests/FFMpeg/Tests/Media/AudioTest.php index 79f5653..85066ff 100644 --- a/tests/FFMpeg/Tests/Media/AudioTest.php +++ b/tests/FFMpeg/Tests/Media/AudioTest.php @@ -50,6 +50,27 @@ class AudioTest extends AbstractStreamableTestCase $audio->addFilter($filter); } + public function testAddAVideoFilterThrowsException() + { + $driver = $this->getFFMpegDriverMock(); + $ffprobe = $this->getFFProbeMock(); + + $filters = $this->getMockBuilder('FFMpeg\Filters\FiltersCollection') + ->disableOriginalConstructor() + ->getMock(); + + $audio = new Audio(__FILE__, $driver, $ffprobe); + $audio->setFiltersCollection($filters); + + $filter = $this->getMock('FFMpeg\Filters\Video\VideoFilterInterface'); + + $filters->expects($this->never()) + ->method('add'); + + $this->setExpectedException('FFMpeg\Exception\InvalidArgumentException'); + $audio->addFilter($filter); + } + public function testSaveWithFailure() { $driver = $this->getFFMpegDriverMock(); diff --git a/tests/FFMpeg/Tests/Media/VideoTest.php b/tests/FFMpeg/Tests/Media/VideoTest.php index 14f290d..00dbf12 100644 --- a/tests/FFMpeg/Tests/Media/VideoTest.php +++ b/tests/FFMpeg/Tests/Media/VideoTest.php @@ -50,6 +50,27 @@ class VideoTest extends AbstractStreamableTestCase $video->addFilter($filter); } + public function testAddAudioFilterAddsAFilter() + { + $driver = $this->getFFMpegDriverMock(); + $ffprobe = $this->getFFProbeMock(); + + $filters = $this->getMockBuilder('FFMpeg\Filters\FiltersCollection') + ->disableOriginalConstructor() + ->getMock(); + + $video = new Video(__FILE__, $driver, $ffprobe); + $video->setFiltersCollection($filters); + + $filter = $this->getMock('FFMpeg\Filters\Audio\AudioFilterInterface'); + + $filters->expects($this->once()) + ->method('add') + ->with($filter); + + $video->addFilter($filter); + } + public function testFrameShouldReturnAFrame() { $driver = $this->getFFMpegDriverMock(); @@ -277,14 +298,14 @@ class VideoTest extends AbstractStreamableTestCase '-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop', '-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6', '-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-pass', '1', '-passlogfile', - '-an', '/target/file', + '/target/file', ), array( '-y', '-i', __FILE__, '-b:v', '663k', '-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop', '-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6', '-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-pass', '2', '-passlogfile', - '-ac', '2', '-ar', '44100', '/target/file', + '/target/file', )), null, $format), array(false, array(array( '-y', '-i', __FILE__, @@ -293,7 +314,7 @@ class VideoTest extends AbstractStreamableTestCase '-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop', '-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6', '-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-pass', '1', '-passlogfile', - '-an', '/target/file', + '/target/file', ), array( '-y', '-i', __FILE__, '-vcodec', 'gloubi-boulga-video', @@ -302,7 +323,7 @@ class VideoTest extends AbstractStreamableTestCase '-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop', '-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6', '-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-pass', '2', '-passlogfile', - '-ac', '2', '-ar', '44100', '/target/file', + '/target/file', )), null, $audioVideoFormat), array(false, array(array( '-y', '-i', __FILE__, @@ -310,14 +331,14 @@ class VideoTest extends AbstractStreamableTestCase '-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop', '-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6', '-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-pass', '1', '-passlogfile', - '-an', '/target/file', + '/target/file', ), array( '-y', '-i', __FILE__, 'extra', 'param', '-b:v', '665k', '-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop', '-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6', '-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-pass', '2', '-passlogfile', - '-ac', '2', '-ar', '44100', '/target/file', + '/target/file', )), null, $formatExtra), array(true, array(array( '-y', '-i', __FILE__, @@ -325,7 +346,7 @@ class VideoTest extends AbstractStreamableTestCase '-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop', '-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6', '-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-pass', '1', '-passlogfile', - '-an', '/target/file', + '/target/file', ), array( '-y', '-i', __FILE__, '-threads', 24, @@ -333,7 +354,7 @@ class VideoTest extends AbstractStreamableTestCase '-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop', '-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6', '-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-pass', '2', '-passlogfile', - '-ac', '2', '-ar', '44100', '/target/file', + '/target/file', )), null, $format), array(true, array(array( '-y', '-i', __FILE__, @@ -341,28 +362,28 @@ class VideoTest extends AbstractStreamableTestCase '-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop', '-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6', '-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-pass', '1', '-passlogfile', - '-an', '/target/file', + '/target/file', ), array( '-y', '-i', __FILE__, 'extra', 'param', '-threads', 24, '-b:v', '665k', '-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop', '-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6', '-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-pass', '2', '-passlogfile', - '-ac', '2', '-ar', '44100', '/target/file', + '/target/file', )), null, $formatExtra), array(false, array(array( '-y', '-i', __FILE__, '-b:v', '666k', '-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop', '-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6', '-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-pass', '1', '-passlogfile', - '-an', '/target/file', + '/target/file', ), array( '-y', '-i', __FILE__, '-b:v', '666k', '-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop', '-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6', '-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-pass', '2', '-passlogfile', - '-ac', '2', '-ar', '44100', '/target/file', + '/target/file', )), $listeners, $progressableFormat), array(true, array(array( '-y', '-i', __FILE__, @@ -370,7 +391,7 @@ class VideoTest extends AbstractStreamableTestCase '-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop', '-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6', '-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-pass', '1', '-passlogfile', - '-an', '/target/file', + '/target/file', ), array( '-y', '-i', __FILE__, '-threads', 24, @@ -378,7 +399,7 @@ class VideoTest extends AbstractStreamableTestCase '-refs', '6', '-coder', '1', '-sc_threshold', '40', '-flags', '+loop', '-me_range', '16', '-subq', '7', '-i_qfactor', '0.71', '-qcomp', '0.6', '-qdiff', '4', '-trellis', '1', '-b:a', '92k', '-pass', '2', '-passlogfile', - '-ac', '2', '-ar', '44100', '/target/file', + '/target/file', )), $listeners, $progressableFormat), ); }