Completed
Push — develop ( b4fd40...785f8f )
by greg
03:10
created

Ffmpeg::convertMp4ToOgv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 11
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 2
1
<?php
2
namespace PlaygroundCore\Service;
3
4
use Zend\ServiceManager\ServiceManager;
5
use ZfcBase\EventManager\EventProvider;
6
use PHPVideoToolkit\FfmpegProcessOutputException;
7
use Zend\ServiceManager\ServiceLocatorInterface;
8
9
/**
10
 * main class
11
 * To trace an execute command: \PHPVideoToolkit\Trace::vars($ffmpeg->getExecutedCommand(true));
12
 *
13
 *
14
 * On exception:
15
 *     $ffmpeg = $e->getProcess();
16
 *     execute command : \PHPVideoToolkit\Trace::vars($e);
17
 *     FFmpeg Process Messages: \PHPVideoToolkit\Trace::vars($ffmpeg->getMessages());
18
 *     Buffer Output: \PHPVideoToolkit\Trace::vars($ffmpeg->getBuffer(true));
19
 *
20
 */
21
class Ffmpeg extends EventProvider
22
{
23
24
    /**
25
     * @var ModuleOptions
26
     */
27
    protected $options;
28
29
    /**
30
     *
31
     * @var ServiceManager
32
     */
33
    protected $serviceLocator;
34
35
    public function __construct(ServiceLocatorInterface $locator)
36
    {
37
        $this->serviceLocator = $locator;
0 ignored issues
show
Documentation Bug introduced by
$locator is of type object<Zend\ServiceManag...erviceLocatorInterface>, but the property $serviceLocator was declared to be of type object<Zend\ServiceManager\ServiceManager>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
38
    }
39
    
40
    /**
41
     * This method create images from a video.
42
     * ffmpeg -i input.mov output_%03d.png
43
     */
44
    public function createImagesFromVideos($source, $target = 'step-%03d.jpg')
45
    {
46
        try {
47
            $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
48
49
            $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
50
            ->addPreInputCommand('-y')
51
            ->addCommand('-i', $source)
52
            ->setOutputPath($target)
53
            ->execute();
54
        } catch (FfmpegProcessOutputException $e) {
55
            throw new \PHPVideoToolkit\InvalidArgumentException('Error when merging videos');
56
        } catch (\PHPVideoToolkit\Exception $e) {
57
            throw new \PHPVideoToolkit\InvalidArgumentException('Error when merging videos');
58
        }
59
60
        return $target;
61
    }
62
63
    /**
64
     * This method create a video from still images (jpg or png).
65
     * ffmpeg -framerate 1/5 -i etape%01d.jpg -c:v libx264 -vf "fps=25,format=yuv420p" out.mp4
66
     */
67 View Code Duplication
    public function createVideoFromImages($path = 'data/etape*.jpg', $target = false, $framerate = '25', $fps = 25)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        try {
70
            // don't want this service to be a singleton. I have to reset the ffmpeg parameters for each call.
71
            $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
72
73
            $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
74
            ->addPreInputCommand('-y')
75
            ->addPreInputCommand('-framerate', $framerate)
76
            ->addPreInputCommand('-pattern_type', 'glob')
77
            ->addCommand('-i', $path)
78
            ->addCommand('-c:v', 'libx264')
79
            ->addCommand('-vf', 'fps='. $fps)
80
            ->addCommand('-pix_fmt', 'yuv420p')
81
            ->setOutputPath($target)
82
            ->execute();
83
        } catch (FfmpegProcessOutputException $e) {
84
            throw new \PHPVideoToolkit\InvalidArgumentException('Error when merging videos');
85
        } catch (\PHPVideoToolkit\Exception $e) {
86
            throw new \PHPVideoToolkit\InvalidArgumentException('Error when merging videos');
87
        }
88
89
        return $target;
90
    }
91
92
    /**
93
     * This method create a video from still images (jpg or png).
94
     * ffmpeg -framerate 1/5 -i etape%01d.jpg -c:v libx264 -vf "fps=25,format=yuv420p" out.mp4
95
     * ffmpeg -framerate 1/5 -i etape%01d.jpg '-vcodec' 'qtrle' out.mov
96
     */
97 View Code Duplication
    public function createMovFromAlphaImages($path = 'data/etape*.jpg', $target = false, $framerate = '25', $fps = 25)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        try {
100
            // don't want this service to be a singleton. I have to reset the ffmpeg parameters for each call.
101
            $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
102
103
            $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
104
            ->addPreInputCommand('-y')
105
            ->addPreInputCommand('-framerate', $framerate)
106
            //->addPreInputCommand('-start_number', '00154')
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
107
            ->addPreInputCommand('-pattern_type', 'glob')
108
            ->addCommand('-i', $path)
109
            ->addCommand('-vcodec', 'qtrle')
110
            ->addCommand('-vf', 'fps='. $fps)
111
            ->setOutputPath($target)
112
            ->execute();
113
        } catch (FfmpegProcessOutputException $e) {
114
            throw new \PHPVideoToolkit\InvalidArgumentException('Error when merging videos');
115
        } catch (\PHPVideoToolkit\Exception $e) {
116
            throw new \PHPVideoToolkit\InvalidArgumentException('Error when merging videos');
117
        }
118
119
        return $target;
120
    }
121
122
    /**
123
     * This method takes a mp4 source and transform it to Mpeg (with .ts as extension)
124
     *  ffmpeg -i in.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts out.ts
125
     */
126 View Code Duplication
    public function transformMp4ToMpg($source, $target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
    {
128
        try {
129
            $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
130
            $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
131
            ->addCommand('-i', $source)
132
            ->addCommand('-c', 'copy')
133
            ->addCommand('-bsf:v', 'h264_mp4toannexb')
134
            ->addCommand('-f', 'mpegts')
135
            ->setOutputPath($target)
136
            ->execute();
137
        } catch (FfmpegProcessOutputException $e) {
138
            throw new \PHPVideoToolkit\InvalidArgumentException('Error when merging videos');
139
        } catch (\PHPVideoToolkit\Exception $e) {
140
            throw new \PHPVideoToolkit\InvalidArgumentException('Error when merging videos');
141
        }
142
143
        return $target;
144
    }
145
146
    /**
147
     * This method takes a mp4 source and transform it to Mpeg (with .ts as extension)
148
     *  ffmpeg -i in.mov -vcodec h264 -pix_fmt yuv420p -f mpegts out.ts
149
     */
150 View Code Duplication
    public function transformMovToMpg($source, $target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
    {
152
        try {
153
            $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
154
            $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
155
            ->addCommand('-i', $source)
156
            ->addCommand('-vcodec', 'h264')
157
            ->addCommand('-pix_fmt', 'yuv420p')
158
            ->addCommand('-f', 'mpegts')
159
            ->setOutputPath($target)
160
            ->execute();
161
        } catch (FfmpegProcessOutputException $e) {
162
            throw new \PHPVideoToolkit\InvalidArgumentException('Error when merging videos');
163
        } catch (\PHPVideoToolkit\Exception $e) {
164
            throw new \PHPVideoToolkit\InvalidArgumentException('Error when merging videos');
165
        }
166
167
        return $target;
168
    }
169
    
170
    /**
171
     * This method will merge videos in .mpg format with exactly the same codec and codec parameters :
172
     * http://trac.ffmpeg.org/wiki/Concatenate
173
     * @param  array $videos
174
     * @return string
175
     */
176
    public function mergeMpgVideos($videos = false, $target = false)
177
    {
178
        if (is_array($videos)) {
179
            try {
180
                $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
181
                $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
182
                    ->addPreInputCommand('-y')
183
                    ->addCommand('-i', 'concat:' . implode('|', $videos))
184
                    ->addCommand('-c', 'copy')
185
                    ->addCommand('-bsf:a', 'aac_adtstoasc')
186
                    ->addCommand('-bufsize', '1835k')
187
                    ->addCommand('-fflags', 'genpts')
188
                    ->addCommand('-f', 'vob')
189
                    ->setOutputPath($target)
190
                    ->execute();
191
            } catch (FfmpegProcessOutputException $e) {
192
                throw new \PHPVideoToolkit\InvalidArgumentException('Error when merging videos');
193
            } catch (\PHPVideoToolkit\Exception $e) {
194
                throw new \PHPVideoToolkit\InvalidArgumentException('Error when merging videos');
195
            }
196
        }
197
198
        return $target;
199
    }
200
201 View Code Duplication
    public function mergeMp3ToMp4($audioSource, $videoSource, $target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
    {
203
        $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
204
       
205
        $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
206
           ->addCommand('-i', $videoSource)
207
           ->addCommand('-i', $audioSource, true)
208
           ->setOutputPath($target)
209
           ->execute();
210
        return $target;
211
    }
212
213 View Code Duplication
    public function convertMp4ToOgv($videoSource, $target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
214
    {
215
        $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
216
       
217
        $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
218
            ->addCommand('-i', $videoSource)
219
            ->setOutputPath($target)
220
            ->execute();
221
        
222
        return $target;
223
    }
224
225
    /**
226
     * ffmpeg -i sound.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 out.mp3
227
     */
228 View Code Duplication
    public function convertToMp3($source, $target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
229
    {
230
        $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
231
       
232
        $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
233
            ->addPreInputCommand('-y')
234
            ->addCommand('-i', $source)
235
            ->addCommand('-vn')
236
            ->addCommand('-ar', '44100')
237
            ->addCommand('-ac', '2')
238
            ->addCommand('-ab', '192')
239
            ->addCommand('-f', 'mp3')
240
            ->setOutputPath($target)
241
            ->execute();
242
        
243
        return $target;
244
    }
245
246 View Code Duplication
    public function convertMovToMp4($videoSource, $target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
247
    {
248
        $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
249
       
250
        $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
251
            ->addPreInputCommand('-y')
252
            ->addCommand('-i', $videoSource)
253
            ->addCommand('-vcodec', 'h264')
254
            ->addCommand('-acodec', 'aac')
255
            ->addCommand('-strict', '2')
256
            ->addCommand('-pix_fmt', 'yuv420p')
257
            ->addCommand('-movflags', '+faststart')
258
            ->setOutputPath($target)
259
            ->execute();
260
        
261
        return $target;
262
    }
263
264 View Code Duplication
    public function mergeMp4($videoSource, $target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
265
    {
266
        // don't want this service to be a singleton. I have to reset the ffmpeg parameters for each call.
267
        $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
268
       
269
        $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
270
            ->addPreInputCommand('-y')
271
            ->addCommand('-i', $videoSource)
272
            ->setOutputPath($target)
273
            ->execute();
274
        
275
        return $target;
276
    }
277
278
    /*
279
    *  this method takes an image (with alpha) or a mov video (the format to keep alpha channel) and overlay this layer
280
    *  on a background video.
281
    *  ffmpeg -i in.mov -i in.png -filter_complex "[0:0][1:0]overlay=format=rgb[out]" -map [out] -vcodec qtrle out.mov
282
    */
283
    public function overlayOnMp4($source, $layer, $target)
284
    {
285
        if (!is_array($layer)) {
286
            $layer = array($layer);
287
        }
288
        $overlay = '';
289
        $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
290
       
291
        $ffmpeg = $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
292
            ->addPreInputCommand('-y')
293
            ->addCommand('-i', $source, true);
294
295
        foreach ($layer as $k => $l) {
296
            $ffmpeg->addCommand('-i', $l, true);
297
            $overlay .= 'overlay=format=rgb';
298
            if ($k<count($layer)-1) {
299
                $overlay .= ',';
300
            }
301
        }
302
303
        $ffmpeg->addCommand('-filter_complex', $overlay.'[out]')
304
            ->addCommand('-map', '[out]')
305
            ->addCommand('-vcodec', 'qtrle')
306
            ->setOutputPath($target)
307
            ->execute();
308
309
        return $target;
310
    }
311
    
312
    /*
313
    *  this method takes an image (with alpha) or a mov video (the format to keep alpha channel) and overlay this layer
314
    *  on a background video. 
315
    *  ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac -strict experimental output.mp4
316
    */
317
    public function addWavToMp4($video, $sound, $target)
318
    {
319
        $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
320
       
321
        $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
322
            ->addPreInputCommand('-y')
323
            ->addCommand('-i', $video, true)
324
            ->addCommand('-i', $sound, true)
325
            ->addCommand('-c:v', 'copy')
326
            ->addCommand('-c:a', 'aac')
327
            ->addCommand('-strict', 'experimental')
328
            ->setOutputPath($target)
329
            ->execute();
330
331
        return $target;
332
    }
333
334
    /*
335
    *  this method takes an audio file and increase its level
336
    *  on a background video.
337
    *
338
    * ffmpeg -i title.wav -af "volume=4" titleok.wav
339
    */
340 View Code Duplication
    public function increaseVolumeSound($source, $level, $target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
341
    {
342
        $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
343
       
344
        $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
345
            ->addPreInputCommand('-y')
346
            ->addCommand('-i', $source, true)
347
            ->addCommand('-af', 'volume='.$level)
348
            ->setOutputPath($target)
349
            ->execute();
350
351
        return $target;
352
    }
353
354
    /*
355
    *  this method creates an audio file of $duration seconds with no sound
356
    *  on a background video.
357
    *  ffmpeg -y -filter_complex 'aevalsrc=0:d=1.6' silence.wav
358
    */
359 View Code Duplication
    public function createNullSound($duration = 1, $target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
360
    {
361
        $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
362
       
363
        $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
364
            ->addPreInputCommand('-y')
365
            ->addCommand('-filter_complex', 'aevalsrc=0:d='.$duration)
366
            ->setOutputPath($target)
367
            ->execute();
368
369
        return $target;
370
    }
371
372
    /**
373
    * This method concatenate an array of sounds
374
    * ffmpeg -y -i in-1.wav -i in-2.wav -i in-3.wav
375
    * -filter_complex '[0:0][1:0][2:0]concat=n=3:v=0:a=1[out]' -map '[out]' out.wav
376
    */
377 View Code Duplication
    public function concatenateSounds($sounds = array(), $target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
378
    {
379
        if (!is_array($sounds)) {
380
            $sounds = array($sounds);
381
        }
382
383
        $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
384
        $ffmpeg = $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
385
            ->addPreInputCommand('-y');
386
387
        $concat = '';
388
        foreach ($sounds as $k => $s) {
389
            $ffmpeg->addCommand('-i', $s, true);
390
            $concat .= '['.$k.':0]';
391
        }
392
        $concat .= 'concat=n='. count($sounds) .':v=0:a=1[out]';
393
394
        $ffmpeg->addCommand('-filter_complex', $concat)
395
            ->addCommand('-map', '[out]')
396
            ->setOutputPath($target)
397
            ->execute();
398
399
        return $target;
400
    }
401
402
    /**
403
    * This method merge an array of sounds
404
    * ffmpeg -i in-1.wav -i in-2.wav -i in-3.wav
405
    * -filter_complex "[0:a][1:a][2:a]amerge=inputs=3[aout]" -map "[aout]" -ac 2 out.wav
406
    */
407 View Code Duplication
    public function mergeSounds($sounds = array(), $target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
408
    {
409
        if (!is_array($sounds)) {
410
            $sounds = array($sounds);
411
        }
412
413
        $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
414
        $ffmpeg = $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
415
            ->addPreInputCommand('-y');
416
417
        $merge = '';
418
        foreach ($sounds as $k => $s) {
419
            $ffmpeg->addCommand('-i', $s, true);
420
            $merge .= '['.$k.':a]';
421
        }
422
        $merge .= 'amerge=inputs='. count($sounds) .'[aout]';
423
424
        $ffmpeg->addCommand('-filter_complex', $merge)
425
            ->addCommand('-map', '[aout]')
426
            ->addCommand('-ac', '2')
427
            ->setOutputPath($target)
428
            ->execute();
429
430
        return $target;
431
    }
432
433
    /*
434
    *  this method takes an image (with alpha) or a mov video (the format to keep alpha channel) and overlay this layer
435
    *  on a background video.
436
    *  ffmpeg -i sub_video3.mp4 -vf drawtext="fontfile=/usr/share/fonts/truetype/ttf-dejavu/DejaVuSerif.ttf:
437
    *  text='Text to write is this one, overlaid':fontsize=20:fontcolor=red:x=100:y=100" with_text.mp4
438
    */
439
    public function overlayTextOnMp4($source, $font, $fontSize, $fontColor, $message, $x, $y, $target)
440
    {
441
        $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
442
443
        $text = "fontfile=$font:text='". $message."':fontsize=". $fontSize .":fontcolor=" . $fontColor . ":x=".$x.":y=".$y;
444
       
445
        $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
446
            ->addPreInputCommand('-y')
447
            ->addCommand('-i', $source)
448
            ->addCommand('-vf', 'drawtext='.$text)
449
            ->setOutputPath($target)
450
            ->execute();
451
        
452
        return $target;
453
    }
454
455
    /**
456
    *  this method extracts an image form a video at the $time second in the video.
457
    *  ffmpeg -ss 00:00:04 -i video.mp4 -vframes 1 out.png
458
    */
459
    public function extractImage($source, $target, $start = '00:00:01', $frames = 1)
460
    {
461
        $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
462
       
463
        $ffmpeg = $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
464
            ->addPreInputCommand('-y');
465
466
        if (!empty($start)) {
467
            $ffmpeg->addPreInputCommand('-ss', $start);
468
        }
469
470
        $ffmpeg->addCommand('-i', $source)
471
            ->addCommand('-vframes', $frames)
472
            ->setOutputPath($target)
473
            ->execute();
474
475
        return $target;
476
    }
477
478
    /**
479
    *  this method splits a video into n chunks defined by the frames array.
480
    *  $frames = array(array(0, 12), array(13, 110), array(111, 200));
481
    *  ffmpeg -i quickns.mov -an -vf "select=between(n\,110\,200),setpts=PTS-STARTPTS" grg.mov
482
    */
483
    public function splitVideo($source, $frames = array(), $target)
484
    {
485
        $this->serviceLocator->setShared('playgroundcore_phpvideotoolkit', false);
486
        
487
        $i=1;
488
        foreach ($frames as $frame) {
489
            $this->serviceLocator->get('playgroundcore_phpvideotoolkit')
490
                ->addPreInputCommand('-y')
491
                ->addCommand('-i', $source)
492
                ->addCommand('-an')
493
                ->addCommand('-vf', 'select=between(n\,' . $frame[0] . '\,' . $frame[1] . '),setpts=PTS-STARTPTS')
494
                ->setOutputPath($target . sprintf('s%02d', $i) . '.mov')
495
                ->execute();
496
            $i++;
497
        }
498
499
        return $target;
500
    }
501
502
    public function setOptions($options)
503
    {
504
        $this->options = $options;
505
506
        return $this;
507
    }
508
509
    public function getOptions()
510
    {
511
        if (!$this->options) {
512
            $this->setOptions($this->serviceLocator->get('playgroundcore_module_options'));
513
        }
514
515
        return $this->options;
516
    }
517
}
518