From fe47ab74ef7849f958c21bb0ca6c599a082a45a3 Mon Sep 17 00:00:00 2001 From: GuillaumeVerdon Date: Tue, 14 Feb 2017 18:21:07 +0100 Subject: [PATCH] Added a getAsBase64() method to Frame object (#292) * Added a getAsBase64() method to Frame object * Merged the getBase64 method in save method * Differentiate the two possible return * Corrected typo (Edited from github) --- src/FFMpeg/Media/Frame.php | 21 +++++++++++++++------ tests/Unit/Media/FrameTest.php | 33 +++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/FFMpeg/Media/Frame.php b/src/FFMpeg/Media/Frame.php index 7b08eb3..590deff 100644 --- a/src/FFMpeg/Media/Frame.php +++ b/src/FFMpeg/Media/Frame.php @@ -85,26 +85,31 @@ class Frame extends AbstractMediaType * * @throws RuntimeException */ - public function save($pathfile, $accurate = false) + public function save($pathfile, $accurate = false, $returnBase64 = false) { /** * might be optimized with http://ffmpeg.org/trac/ffmpeg/wiki/Seeking%20with%20FFmpeg * @see http://ffmpeg.org/ffmpeg.html#Main-options */ + $outputFormat = $returnBase64 ? "image2pipe" : "image2"; if (!$accurate) { $commands = array( '-y', '-ss', (string) $this->timecode, '-i', $this->pathfile, '-vframes', '1', - '-f', 'image2' + '-f', $outputFormat ); } else { $commands = array( '-y', '-i', $this->pathfile, '-vframes', '1', '-ss', (string) $this->timecode, - '-f', 'image2' + '-f', $outputFormat ); } + + if($returnBase64) { + array_push($commands, "-"); + } foreach ($this->filters as $filter) { $commands = array_merge($commands, $filter->apply($this)); @@ -113,12 +118,16 @@ class Frame extends AbstractMediaType $commands = array_merge($commands, array($pathfile)); try { - $this->driver->command($commands); + if(!$returnBase64) { + $this->driver->command($commands); + return $this; + } + else { + return $this->driver->command($commands); + } } catch (ExecutionFailureException $e) { $this->cleanupTemporaryFile($pathfile); throw new RuntimeException('Unable to save frame', $e->getCode(), $e); } - - return $this; } } diff --git a/tests/Unit/Media/FrameTest.php b/tests/Unit/Media/FrameTest.php index 43f70c3..51ee2cd 100644 --- a/tests/Unit/Media/FrameTest.php +++ b/tests/Unit/Media/FrameTest.php @@ -50,7 +50,7 @@ class FrameTest extends AbstractMediaTestCase /** * @dataProvider provideSaveOptions */ - public function testSave($accurate, $commands) + public function testSave($accurate, $base64, $commands) { $driver = $this->getFFMpegDriverMock(); $ffprobe = $this->getFFProbeMock(); @@ -67,24 +67,41 @@ class FrameTest extends AbstractMediaTestCase ->method('command') ->with($commands); - $frame = new Frame($this->getVideoMock(__FILE__), $driver, $ffprobe, $timecode); - $this->assertSame($frame, $frame->save($pathfile, $accurate)); + if(!$base64) { + $frame = new Frame($this->getVideoMock(__FILE__), $driver, $ffprobe, $timecode); + $this->assertSame($frame, $frame->save($pathfile, $accurate, $base64)); + } + else { + $frame = new Frame($this->getVideoMock(__FILE__), $driver, $ffprobe, $timecode); + $frame->save($pathfile, $accurate, $base64); + } } - + public function provideSaveOptions() { return array( - array(false, array( + array(false, false, array( '-y', '-ss', 'timecode', '-i', __FILE__, '-vframes', '1', '-f', 'image2') ), - array(true, array( + array(true, false, array( '-y', '-i', __FILE__, '-vframes', '1', '-ss', 'timecode', - '-f', 'image2' - )), + '-f', 'image2') + ), + array(false, true, array( + '-y', '-ss', 'timecode', + '-i', __FILE__, + '-vframes', '1', + '-f', 'image2pipe', '-') + ), + array(true, true, array( + '-y', '-i', __FILE__, + '-vframes', '1', '-ss', 'timecode', + '-f', 'image2pipe', '-') + ) ); } }