Skip to content

Commit fe47ab7

Browse files
GuillaumeVerdonRomain
authored andcommitted
Added a getAsBase64() method to Frame object (PHP-FFMpeg#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)
1 parent 21c28de commit fe47ab7

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

src/FFMpeg/Media/Frame.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,31 @@ public function getTimeCode()
8585
*
8686
* @throws RuntimeException
8787
*/
88-
public function save($pathfile, $accurate = false)
88+
public function save($pathfile, $accurate = false, $returnBase64 = false)
8989
{
9090
/**
9191
* might be optimized with http://ffmpeg.org/trac/ffmpeg/wiki/Seeking%20with%20FFmpeg
9292
* @see http://ffmpeg.org/ffmpeg.html#Main-options
9393
*/
94+
$outputFormat = $returnBase64 ? "image2pipe" : "image2";
9495
if (!$accurate) {
9596
$commands = array(
9697
'-y', '-ss', (string) $this->timecode,
9798
'-i', $this->pathfile,
9899
'-vframes', '1',
99-
'-f', 'image2'
100+
'-f', $outputFormat
100101
);
101102
} else {
102103
$commands = array(
103104
'-y', '-i', $this->pathfile,
104105
'-vframes', '1', '-ss', (string) $this->timecode,
105-
'-f', 'image2'
106+
'-f', $outputFormat
106107
);
107108
}
109+
110+
if($returnBase64) {
111+
array_push($commands, "-");
112+
}
108113

109114
foreach ($this->filters as $filter) {
110115
$commands = array_merge($commands, $filter->apply($this));
@@ -113,12 +118,16 @@ public function save($pathfile, $accurate = false)
113118
$commands = array_merge($commands, array($pathfile));
114119

115120
try {
116-
$this->driver->command($commands);
121+
if(!$returnBase64) {
122+
$this->driver->command($commands);
123+
return $this;
124+
}
125+
else {
126+
return $this->driver->command($commands);
127+
}
117128
} catch (ExecutionFailureException $e) {
118129
$this->cleanupTemporaryFile($pathfile);
119130
throw new RuntimeException('Unable to save frame', $e->getCode(), $e);
120131
}
121-
122-
return $this;
123132
}
124133
}

tests/Unit/Media/FrameTest.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testAddFiltersAddsAFilter()
5050
/**
5151
* @dataProvider provideSaveOptions
5252
*/
53-
public function testSave($accurate, $commands)
53+
public function testSave($accurate, $base64, $commands)
5454
{
5555
$driver = $this->getFFMpegDriverMock();
5656
$ffprobe = $this->getFFProbeMock();
@@ -67,24 +67,41 @@ public function testSave($accurate, $commands)
6767
->method('command')
6868
->with($commands);
6969

70-
$frame = new Frame($this->getVideoMock(__FILE__), $driver, $ffprobe, $timecode);
71-
$this->assertSame($frame, $frame->save($pathfile, $accurate));
70+
if(!$base64) {
71+
$frame = new Frame($this->getVideoMock(__FILE__), $driver, $ffprobe, $timecode);
72+
$this->assertSame($frame, $frame->save($pathfile, $accurate, $base64));
73+
}
74+
else {
75+
$frame = new Frame($this->getVideoMock(__FILE__), $driver, $ffprobe, $timecode);
76+
$frame->save($pathfile, $accurate, $base64);
77+
}
7278
}
73-
79+
7480
public function provideSaveOptions()
7581
{
7682
return array(
77-
array(false, array(
83+
array(false, false, array(
7884
'-y', '-ss', 'timecode',
7985
'-i', __FILE__,
8086
'-vframes', '1',
8187
'-f', 'image2')
8288
),
83-
array(true, array(
89+
array(true, false, array(
8490
'-y', '-i', __FILE__,
8591
'-vframes', '1', '-ss', 'timecode',
86-
'-f', 'image2'
87-
)),
92+
'-f', 'image2')
93+
),
94+
array(false, true, array(
95+
'-y', '-ss', 'timecode',
96+
'-i', __FILE__,
97+
'-vframes', '1',
98+
'-f', 'image2pipe', '-')
99+
),
100+
array(true, true, array(
101+
'-y', '-i', __FILE__,
102+
'-vframes', '1', '-ss', 'timecode',
103+
'-f', 'image2pipe', '-')
104+
)
88105
);
89106
}
90107
}

0 commit comments

Comments
 (0)