scratch – Blame information for rev 115

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 1 <?php
2  
3 /*
4 * This file is part of PHP-FFmpeg.
5 *
6 * (c) Strime <contact@strime.io>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11  
12 namespace FFMpeg\Media;
13  
14 use Alchemy\BinaryDriver\Exception\ExecutionFailureException;
15 use FFMpeg\Filters\Gif\GifFilterInterface;
16 use FFMpeg\Filters\Gif\GifFilters;
17 use FFMpeg\Driver\FFMpegDriver;
18 use FFMpeg\FFProbe;
19 use FFMpeg\Exception\RuntimeException;
20 use FFMpeg\Coordinate\TimeCode;
21 use FFMpeg\Coordinate\Dimension;
22  
23 class Gif extends AbstractMediaType
24 {
25 /** @var TimeCode */
26 private $timecode;
27 /** @var Dimension */
28 private $dimension;
29 /** @var integer */
30 private $duration;
31 /** @var Video */
32 private $video;
33  
34 public function __construct(Video $video, FFMpegDriver $driver, FFProbe $ffprobe, TimeCode $timecode, Dimension $dimension, $duration = null)
35 {
36 parent::__construct($video->getPathfile(), $driver, $ffprobe);
37 $this->timecode = $timecode;
38 $this->dimension = $dimension;
39 $this->duration = $duration;
40 $this->video = $video;
41 }
42  
43 /**
44 * Returns the video related to the gif.
45 *
46 * @return Video
47 */
48 public function getVideo()
49 {
50 return $this->video;
51 }
52  
53 /**
54 * {@inheritdoc}
55 *
56 * @return GifFilters
57 */
58 public function filters()
59 {
60 return new GifFilters($this);
61 }
62  
63 /**
64 * {@inheritdoc}
65 *
66 * @return Gif
67 */
68 public function addFilter(GifFilterInterface $filter)
69 {
70 $this->filters->add($filter);
71  
72 return $this;
73 }
74  
75 /**
76 * @return TimeCode
77 */
78 public function getTimeCode()
79 {
80 return $this->timecode;
81 }
82  
83 /**
84 * @return Dimension
85 */
86 public function getDimension()
87 {
88 return $this->dimension;
89 }
90  
91 /**
92 * Saves the gif in the given filename.
93 *
94 * @param string $pathfile
95 *
96 * @return Gif
97 *
98 * @throws RuntimeException
99 */
100 public function save($pathfile)
101 {
102 /**
103 * @see http://ffmpeg.org/ffmpeg.html#Main-options
104 */
105 $commands = array(
106 '-ss', (string)$this->timecode
107 );
108  
109 if(null !== $this->duration) {
110 $commands[] = '-t';
111 $commands[] = (string)$this->duration;
112 }
113  
114 $commands[] = '-i';
115 $commands[] = $this->pathfile;
116 $commands[] = '-vf';
117 $commands[] = 'scale=' . $this->dimension->getWidth() . ':-1';
118 $commands[] = '-gifflags';
119 $commands[] = '+transdiff';
120 $commands[] = '-y';
121  
122 foreach ($this->filters as $filter) {
123 $commands = array_merge($commands, $filter->apply($this));
124 }
125  
126 $commands = array_merge($commands, array($pathfile));
127  
128 try {
129 $this->driver->command($commands);
130 } catch (ExecutionFailureException $e) {
131 $this->cleanupTemporaryFile($pathfile);
132 throw new RuntimeException('Unable to save gif', $e->getCode(), $e);
133 }
134  
135 return $this;
136 }
137 }