From ae92dd58876cb325cfb6b579d1993a5470c45284 Mon Sep 17 00:00:00 2001 From: Shaked Klein Orbach Date: Mon, 5 Mar 2018 18:59:57 -0600 Subject: [PATCH 1/7] allow to set frame file type instead of hardcoded jpg --- .../Filters/Video/ExtractMultipleFramesFilter.php | 7 ++++++- .../Video/ExtractMultipleFramesFilterTest.php | 15 ++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/FFMpeg/Filters/Video/ExtractMultipleFramesFilter.php b/src/FFMpeg/Filters/Video/ExtractMultipleFramesFilter.php index d0ed54f..57b3c6d 100644 --- a/src/FFMpeg/Filters/Video/ExtractMultipleFramesFilter.php +++ b/src/FFMpeg/Filters/Video/ExtractMultipleFramesFilter.php @@ -35,6 +35,7 @@ class ExtractMultipleFramesFilter implements VideoFilterInterface private $priority; private $frameRate; private $destinationFolder; + private $frameFileType = 'jpg'; public function __construct($frameRate = self::FRAMERATE_EVERY_SEC, $destinationFolder = __DIR__, $priority = 0) { @@ -49,6 +50,10 @@ class ExtractMultipleFramesFilter implements VideoFilterInterface $this->destinationFolder = $destinationFolder; } + public function setFrameFileType($frameFileType) { + $this->frameFileType = $frameFileType; + } + /** * {@inheritdoc} */ @@ -117,7 +122,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/tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php b/tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php index 6619df7..e45afa1 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,19 @@ 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,'png', '/', 100, 2, array('-vf', 'fps=1/60', '/frame-%02d.png')), ); } } From 9fe417745ebab9b7d55327f99e3b26ce994567a7 Mon Sep 17 00:00:00 2001 From: Shaked Klein Orbach Date: Tue, 6 Mar 2018 17:16:37 -0600 Subject: [PATCH 2/7] add phpdoc & test png files --- src/FFMpeg/Filters/Video/ExtractMultipleFramesFilter.php | 3 +++ .../Unit/Filters/Video/ExtractMultipleFramesFilterTest.php | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/src/FFMpeg/Filters/Video/ExtractMultipleFramesFilter.php b/src/FFMpeg/Filters/Video/ExtractMultipleFramesFilter.php index 57b3c6d..bc8fa66 100644 --- a/src/FFMpeg/Filters/Video/ExtractMultipleFramesFilter.php +++ b/src/FFMpeg/Filters/Video/ExtractMultipleFramesFilter.php @@ -50,6 +50,9 @@ class ExtractMultipleFramesFilter implements VideoFilterInterface $this->destinationFolder = $destinationFolder; } + /** + * @param string $frameFileType + */ public function setFrameFileType($frameFileType) { $this->frameFileType = $frameFileType; } diff --git a/tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php b/tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php index e45afa1..db14ab4 100644 --- a/tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php +++ b/tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php @@ -46,6 +46,12 @@ class ExtractMultipleFramesFilterTest extends TestCase 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,'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')), ); } From fcd494f72303441048188008c6c6a536d8c48d8c Mon Sep 17 00:00:00 2001 From: Shaked Klein Orbach Date: Wed, 7 Mar 2018 14:27:27 -0600 Subject: [PATCH 3/7] improve tests, validate frame file type --- .../Video/ExtractMultipleFramesFilter.php | 12 +++++- .../Video/ExtractMultipleFramesFilterTest.php | 40 +++++++++++++------ 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/FFMpeg/Filters/Video/ExtractMultipleFramesFilter.php b/src/FFMpeg/Filters/Video/ExtractMultipleFramesFilter.php index bc8fa66..d2f79bb 100644 --- a/src/FFMpeg/Filters/Video/ExtractMultipleFramesFilter.php +++ b/src/FFMpeg/Filters/Video/ExtractMultipleFramesFilter.php @@ -37,6 +37,9 @@ class ExtractMultipleFramesFilter implements VideoFilterInterface 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) { $this->priority = $priority; @@ -52,9 +55,16 @@ class ExtractMultipleFramesFilter implements VideoFilterInterface /** * @param string $frameFileType + * @throws \FFMpeg\Exception\InvalidArgumentException + * @return ExtractMultipleFramesFilter */ public function setFrameFileType($frameFileType) { - $this->frameFileType = $frameFileType; + if (in_array($frameFileType, self::$supportedFrameFileTypes)) { + $this->frameFileType = $frameFileType; + return $this; + } + + throw new InvalidArgumentException('Invalid frame file type, use: ' . implode(',', self::$supportedFrameFileTypes)); } /** diff --git a/tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php b/tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php index db14ab4..ee20dea 100644 --- a/tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php +++ b/tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php @@ -41,18 +41,34 @@ class ExtractMultipleFramesFilterTest extends TestCase public function provideFrameRates() { return array( - 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,'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')), + 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'); + } } From afc236985ebf3a9b40b54c035501b088abc4800e Mon Sep 17 00:00:00 2001 From: Shaked Klein Orbach Date: Thu, 8 Mar 2018 10:53:38 -0600 Subject: [PATCH 4/7] fix identation --- tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php b/tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php index ee20dea..f121691 100644 --- a/tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php +++ b/tests/Unit/Filters/Video/ExtractMultipleFramesFilterTest.php @@ -68,7 +68,7 @@ class ExtractMultipleFramesFilterTest extends TestCase * @expectedException \FFMpeg\Exception\InvalidArgumentException */ public function testInvalidFrameFileType() { - $filter = new ExtractMultipleFramesFilter('1/1', '/'); + $filter = new ExtractMultipleFramesFilter('1/1', '/'); $filter->setFrameFileType('webm'); } } From fc55cf51d84078621dee8a8b34357db944574453 Mon Sep 17 00:00:00 2001 From: Jens Hausdorf Date: Thu, 10 May 2018 14:29:24 +0200 Subject: [PATCH 5/7] fix documentation and add a todo comment fixes #529 --- src/FFMpeg/Media/Concat.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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); } From 8b03686aef7f5e9f38445324d4a8ebbf2c0b93c8 Mon Sep 17 00:00:00 2001 From: qcjackman Date: Thu, 17 May 2018 11:12:58 +0800 Subject: [PATCH 6/7] fixed a bug when call Frame->save() when set is true --- src/FFMpeg/Media/Frame.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/FFMpeg/Media/Frame.php b/src/FFMpeg/Media/Frame.php index 590deff..0544113 100644 --- a/src/FFMpeg/Media/Frame.php +++ b/src/FFMpeg/Media/Frame.php @@ -115,7 +115,10 @@ class Frame extends AbstractMediaType $commands = array_merge($commands, $filter->apply($this)); } - $commands = array_merge($commands, array($pathfile)); + // without output filename when return binary string + if(!$returnBase64) { + $commands = array_merge($commands, array($pathfile)); + } try { if(!$returnBase64) { From 1711c66aad1c96ed504e4a8b43f3fec18494c9a3 Mon Sep 17 00:00:00 2001 From: qcjackman Date: Mon, 28 May 2018 14:00:32 +0800 Subject: [PATCH 7/7] fix the test expectations --- src/FFMpeg/Media/Frame.php | 3 +-- tests/Unit/Media/FrameTest.php | 4 +++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/FFMpeg/Media/Frame.php b/src/FFMpeg/Media/Frame.php index 0544113..21499f9 100644 --- a/src/FFMpeg/Media/Frame.php +++ b/src/FFMpeg/Media/Frame.php @@ -115,8 +115,7 @@ class Frame extends AbstractMediaType $commands = array_merge($commands, $filter->apply($this)); } - // without output filename when return binary string - if(!$returnBase64) { + if (!$returnBase64) { $commands = array_merge($commands, array($pathfile)); } 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')