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)
This commit is contained in:
GuillaumeVerdon 2017-02-14 18:21:07 +01:00 committed by Romain Biard
commit fe47ab74ef
2 changed files with 40 additions and 14 deletions

View file

@ -85,27 +85,32 @@ class Frame extends AbstractMediaType
* *
* @throws RuntimeException * @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 * might be optimized with http://ffmpeg.org/trac/ffmpeg/wiki/Seeking%20with%20FFmpeg
* @see http://ffmpeg.org/ffmpeg.html#Main-options * @see http://ffmpeg.org/ffmpeg.html#Main-options
*/ */
$outputFormat = $returnBase64 ? "image2pipe" : "image2";
if (!$accurate) { if (!$accurate) {
$commands = array( $commands = array(
'-y', '-ss', (string) $this->timecode, '-y', '-ss', (string) $this->timecode,
'-i', $this->pathfile, '-i', $this->pathfile,
'-vframes', '1', '-vframes', '1',
'-f', 'image2' '-f', $outputFormat
); );
} else { } else {
$commands = array( $commands = array(
'-y', '-i', $this->pathfile, '-y', '-i', $this->pathfile,
'-vframes', '1', '-ss', (string) $this->timecode, '-vframes', '1', '-ss', (string) $this->timecode,
'-f', 'image2' '-f', $outputFormat
); );
} }
if($returnBase64) {
array_push($commands, "-");
}
foreach ($this->filters as $filter) { foreach ($this->filters as $filter) {
$commands = array_merge($commands, $filter->apply($this)); $commands = array_merge($commands, $filter->apply($this));
} }
@ -113,12 +118,16 @@ class Frame extends AbstractMediaType
$commands = array_merge($commands, array($pathfile)); $commands = array_merge($commands, array($pathfile));
try { try {
if(!$returnBase64) {
$this->driver->command($commands); $this->driver->command($commands);
return $this;
}
else {
return $this->driver->command($commands);
}
} catch (ExecutionFailureException $e) { } catch (ExecutionFailureException $e) {
$this->cleanupTemporaryFile($pathfile); $this->cleanupTemporaryFile($pathfile);
throw new RuntimeException('Unable to save frame', $e->getCode(), $e); throw new RuntimeException('Unable to save frame', $e->getCode(), $e);
} }
return $this;
} }
} }

View file

@ -50,7 +50,7 @@ class FrameTest extends AbstractMediaTestCase
/** /**
* @dataProvider provideSaveOptions * @dataProvider provideSaveOptions
*/ */
public function testSave($accurate, $commands) public function testSave($accurate, $base64, $commands)
{ {
$driver = $this->getFFMpegDriverMock(); $driver = $this->getFFMpegDriverMock();
$ffprobe = $this->getFFProbeMock(); $ffprobe = $this->getFFProbeMock();
@ -67,24 +67,41 @@ class FrameTest extends AbstractMediaTestCase
->method('command') ->method('command')
->with($commands); ->with($commands);
if(!$base64) {
$frame = new Frame($this->getVideoMock(__FILE__), $driver, $ffprobe, $timecode); $frame = new Frame($this->getVideoMock(__FILE__), $driver, $ffprobe, $timecode);
$this->assertSame($frame, $frame->save($pathfile, $accurate)); $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() public function provideSaveOptions()
{ {
return array( return array(
array(false, array( array(false, false, array(
'-y', '-ss', 'timecode', '-y', '-ss', 'timecode',
'-i', __FILE__, '-i', __FILE__,
'-vframes', '1', '-vframes', '1',
'-f', 'image2') '-f', 'image2')
), ),
array(true, array( array(true, false, array(
'-y', '-i', __FILE__, '-y', '-i', __FILE__,
'-vframes', '1', '-ss', 'timecode', '-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', '-')
)
); );
} }
} }