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,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;
}
}

View file

@ -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', '-')
)
);
}
}