diff --git a/src/FFMpeg/Filters/Video/ExtractMultipleFramesFilter.php b/src/FFMpeg/Filters/Video/ExtractMultipleFramesFilter.php index d0ed54f..d2f79bb 100644 --- a/src/FFMpeg/Filters/Video/ExtractMultipleFramesFilter.php +++ b/src/FFMpeg/Filters/Video/ExtractMultipleFramesFilter.php @@ -35,6 +35,10 @@ class ExtractMultipleFramesFilter implements VideoFilterInterface private $priority; private $frameRate; private $destinationFolder; + private $frameFileType = 'jpg'; + + /** @var array */ + private static $supportedFrameFileTypes = ['jpg', 'jpeg', 'png']; public function __construct($frameRate = self::FRAMERATE_EVERY_SEC, $destinationFolder = __DIR__, $priority = 0) { @@ -49,6 +53,20 @@ class ExtractMultipleFramesFilter implements VideoFilterInterface $this->destinationFolder = $destinationFolder; } + /** + * @param string $frameFileType + * @throws \FFMpeg\Exception\InvalidArgumentException + * @return ExtractMultipleFramesFilter + */ + public function setFrameFileType($frameFileType) { + if (in_array($frameFileType, self::$supportedFrameFileTypes)) { + $this->frameFileType = $frameFileType; + return $this; + } + + throw new InvalidArgumentException('Invalid frame file type, use: ' . implode(',', self::$supportedFrameFileTypes)); + } + /** * {@inheritdoc} */ @@ -117,7 +135,7 @@ class ExtractMultipleFramesFilter implements VideoFilterInterface // Set the parameters $commands[] = '-vf'; $commands[] = 'fps=' . $this->frameRate; - $commands[] = $this->destinationFolder . 'frame-%'.$nbDigitsInFileNames.'d.jpg'; + $commands[] = $this->destinationFolder . 'frame-%'.$nbDigitsInFileNames.'d.' . $this->frameFileType; } catch (RuntimeException $e) { throw new RuntimeException('An error occured while extracting the frames: ' . $e->getMessage() . '. The code: ' . $e->getCode()); diff --git a/src/FFMpeg/Media/Concat.php b/src/FFMpeg/Media/Concat.php index 9671a9a..97f5e1e 100644 --- a/src/FFMpeg/Media/Concat.php +++ b/src/FFMpeg/Media/Concat.php @@ -72,7 +72,7 @@ class Concat extends AbstractMediaType /** * Saves the concatenated video in the given array, considering that the sources videos are all encoded with the same codec. * - * @param array $outputPathfile + * @param string $outputPathfile * @param string $streamCopy * * @return Concat @@ -144,6 +144,7 @@ class Concat extends AbstractMediaType $this->driver->command($commands); } catch (ExecutionFailureException $e) { $this->cleanupTemporaryFile($outputPathfile); + // TODO@v1: paste this line into an `finally` block. $this->cleanupTemporaryFile($sourcesFile); throw new RuntimeException('Unable to save concatenated video', $e->getCode(), $e); } diff --git a/src/FFMpeg/Media/Frame.php b/src/FFMpeg/Media/Frame.php index 590deff..21499f9 100644 --- a/src/FFMpeg/Media/Frame.php +++ b/src/FFMpeg/Media/Frame.php @@ -115,7 +115,9 @@ class Frame extends AbstractMediaType $commands = array_merge($commands, $filter->apply($this)); } - $commands = array_merge($commands, array($pathfile)); + if (!$returnBase64) { + $commands = array_merge($commands, array($pathfile)); + } try { if(!$returnBase64) { diff --git a/tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php b/tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php index 6619df7..f121691 100644 --- a/tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php +++ b/tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php @@ -12,7 +12,7 @@ class ExtractMultipleFramesFilterTest extends TestCase /** * @dataProvider provideFrameRates */ - public function testApply($frameRate, $destinationFolder, $duration, $modulus, $expected) + public function testApply($frameRate, $frameFileType,$destinationFolder, $duration, $modulus, $expected) { $video = $this->getVideoMock(); $pathfile = '/path/to/file'.mt_rand(); @@ -34,18 +34,41 @@ class ExtractMultipleFramesFilterTest extends TestCase ->will($this->returnValue($streams)); $filter = new ExtractMultipleFramesFilter($frameRate, $destinationFolder); + $filter->setFrameFileType($frameFileType); $this->assertEquals($expected, $filter->apply($video, $format)); } public function provideFrameRates() { return array( - array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_SEC, '/', 100, 2, array('-vf', 'fps=1/1', '/frame-%03d.jpg')), - array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_2SEC, '/', 100, 2, array('-vf', 'fps=1/2', '/frame-%02d.jpg')), - array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_5SEC, '/', 100, 2, array('-vf', 'fps=1/5', '/frame-%02d.jpg')), - array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_10SEC, '/', 100, 2, array('-vf', 'fps=1/10', '/frame-%02d.jpg')), - array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_30SEC, '/', 100, 2, array('-vf', 'fps=1/30', '/frame-%02d.jpg')), - array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_60SEC, '/', 100, 2, array('-vf', 'fps=1/60', '/frame-%02d.jpg')), + array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_SEC, 'jpg', '/', 100, 2, array('-vf', 'fps=1/1', '/frame-%03d.jpg')), + array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_2SEC, 'jpg', '/', 100, 2, array('-vf', 'fps=1/2', '/frame-%02d.jpg')), + array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_5SEC, 'jpg', '/', 100, 2, array('-vf', 'fps=1/5', '/frame-%02d.jpg')), + array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_10SEC, 'jpg', '/', 100, 2, array('-vf', 'fps=1/10', '/frame-%02d.jpg')), + array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_30SEC, 'jpg', '/', 100, 2, array('-vf', 'fps=1/30', '/frame-%02d.jpg')), + array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_60SEC, 'jpg', '/', 100, 2, array('-vf', 'fps=1/60', '/frame-%02d.jpg')), + + array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_SEC, 'jpeg', '/', 100, 2, array('-vf', 'fps=1/1', '/frame-%03d.jpeg')), + array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_2SEC, 'jpeg', '/', 100, 2, array('-vf', 'fps=1/2', '/frame-%02d.jpeg')), + array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_5SEC, 'jpeg', '/', 100, 2, array('-vf', 'fps=1/5', '/frame-%02d.jpeg')), + array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_10SEC, 'jpeg', '/', 100, 2, array('-vf', 'fps=1/10', '/frame-%02d.jpeg')), + array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_30SEC, 'jpeg', '/', 100, 2, array('-vf', 'fps=1/30', '/frame-%02d.jpeg')), + array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_60SEC, 'jpeg', '/', 100, 2, array('-vf', 'fps=1/60', '/frame-%02d.jpeg')), + + array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_SEC, 'png', '/', 100, 2, array('-vf', 'fps=1/1', '/frame-%03d.png')), + array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_2SEC, 'png', '/', 100, 2, array('-vf', 'fps=1/2', '/frame-%02d.png')), + array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_5SEC, 'png', '/', 100, 2, array('-vf', 'fps=1/5', '/frame-%02d.png')), + array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_10SEC, 'png', '/', 100, 2, array('-vf', 'fps=1/10', '/frame-%02d.png')), + array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_30SEC, 'png', '/', 100, 2, array('-vf', 'fps=1/30', '/frame-%02d.png')), + array(ExtractMultipleFramesFilter::FRAMERATE_EVERY_60SEC, 'png', '/', 100, 2, array('-vf', 'fps=1/60', '/frame-%02d.png')), ); } + + /** + * @expectedException \FFMpeg\Exception\InvalidArgumentException + */ + public function testInvalidFrameFileType() { + $filter = new ExtractMultipleFramesFilter('1/1', '/'); + $filter->setFrameFileType('webm'); + } } diff --git a/tests/Unit/Media/FrameTest.php b/tests/Unit/Media/FrameTest.php index 51ee2cd..4c96527 100644 --- a/tests/Unit/Media/FrameTest.php +++ b/tests/Unit/Media/FrameTest.php @@ -61,7 +61,9 @@ class FrameTest extends AbstractMediaTestCase $pathfile = '/target/destination'; - array_push($commands, $pathfile); + if (!$base64) { + array_push($commands, $pathfile); + } $driver->expects($this->once()) ->method('command')