1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jackal\Giffhanger\FFMpeg\ext\Media; |
4
|
|
|
|
5
|
|
|
use Alchemy\BinaryDriver\Exception\ExecutionFailureException; |
6
|
|
|
use FFMpeg\Driver\FFMpegDriver; |
7
|
|
|
use FFMpeg\FFProbe; |
8
|
|
|
use FFMpeg\Exception\RuntimeException; |
9
|
|
|
use FFMpeg\Coordinate\TimeCode; |
10
|
|
|
use FFMpeg\Coordinate\Dimension; |
11
|
|
|
use FFMpeg\Media\Video; |
12
|
|
|
|
13
|
|
|
class Gif extends \FFMpeg\Media\Gif |
14
|
|
|
{ |
15
|
|
|
/** @var TimeCode */ |
16
|
|
|
private $timecode; |
17
|
|
|
/** @var Dimension */ |
18
|
|
|
private $dimension; |
19
|
|
|
/** @var integer */ |
20
|
|
|
private $duration; |
21
|
|
|
/** @var Video */ |
22
|
|
|
private $video; |
23
|
|
|
|
24
|
|
|
public function __construct(Video $video, FFMpegDriver $driver, FFProbe $ffprobe, TimeCode $timecode, Dimension $dimension, $duration = null) |
25
|
|
|
{ |
26
|
|
|
parent::__construct($video, $driver, $ffprobe, $timecode, $dimension, $duration); |
27
|
|
|
$this->timecode = $timecode; |
28
|
|
|
$this->dimension = $dimension; |
29
|
|
|
$this->duration = $duration; |
30
|
|
|
$this->video = $video; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Saves the gif in the given filename. |
35
|
|
|
* |
36
|
|
|
* @param string $pathfile |
37
|
|
|
* |
38
|
|
|
* @return Gif |
39
|
|
|
* |
40
|
|
|
* @throws RuntimeException |
41
|
|
|
*/ |
42
|
|
|
public function save($pathfile) |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* @see http://ffmpeg.org/ffmpeg.html#Main-options |
46
|
|
|
*/ |
47
|
|
|
$commands = [ |
48
|
|
|
'-ss', (string) $this->timecode, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
if (null !== $this->duration) { |
52
|
|
|
$commands[] = '-t'; |
53
|
|
|
$commands[] = (string) $this->duration; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$commands[] = '-i'; |
57
|
|
|
$commands[] = $this->pathfile; |
58
|
|
|
$commands[] = '-vf'; |
59
|
|
|
$commands[] = 'scale=' . $this->dimension->getWidth() . ':-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse'; |
60
|
|
|
$commands[] = '-gifflags'; |
61
|
|
|
$commands[] = '+transdiff'; |
62
|
|
|
$commands[] = '-y'; |
63
|
|
|
|
64
|
|
|
foreach ($this->filters as $filter) { |
65
|
|
|
$commands = array_merge($commands, $filter->apply($this)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$commands = array_merge($commands, [$pathfile]); |
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
$this->driver->command($commands); |
72
|
|
|
} catch (ExecutionFailureException $e) { |
73
|
|
|
$this->cleanupTemporaryFile($pathfile); |
74
|
|
|
|
75
|
|
|
throw new RuntimeException('Unable to save gif', $e->getCode(), $e); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|