scratch – Blame information for rev

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) Alchemy <info@alchemy.fr>
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\Frame\FrameFilterInterface;
16 use FFMpeg\Filters\Frame\FrameFilters;
17 use FFMpeg\Driver\FFMpegDriver;
18 use FFMpeg\FFProbe;
19 use FFMpeg\Exception\RuntimeException;
20 use FFMpeg\Coordinate\TimeCode;
21  
22 class Frame extends AbstractMediaType
23 {
24 /** @var TimeCode */
25 private $timecode;
26 /** @var Video */
27 private $video;
28  
29 public function __construct(Video $video, FFMpegDriver $driver, FFProbe $ffprobe, TimeCode $timecode)
30 {
31 parent::__construct($video->getPathfile(), $driver, $ffprobe);
32 $this->timecode = $timecode;
33 $this->video = $video;
34 }
35  
36 /**
37 * Returns the video related to the frame.
38 *
39 * @return Video
40 */
41 public function getVideo()
42 {
43 return $this->video;
44 }
45  
46 /**
47 * {@inheritdoc}
48 *
49 * @return FrameFilters
50 */
51 public function filters()
52 {
53 return new FrameFilters($this);
54 }
55  
56 /**
57 * {@inheritdoc}
58 *
59 * @return Frame
60 */
61 public function addFilter(FrameFilterInterface $filter)
62 {
63 $this->filters->add($filter);
64  
65 return $this;
66 }
67  
68 /**
69 * @return TimeCode
70 */
71 public function getTimeCode()
72 {
73 return $this->timecode;
74 }
75  
76 /**
77 * Saves the frame in the given filename.
78 *
79 * Uses the `unaccurate method by default.`
80 *
81 * @param string $pathfile
82 * @param Boolean $accurate
83 *
84 * @return Frame
85 *
86 * @throws RuntimeException
87 */
88 public function save($pathfile, $accurate = false, $returnBase64 = false)
89 {
90 /**
91 * might be optimized with http://ffmpeg.org/trac/ffmpeg/wiki/Seeking%20with%20FFmpeg
92 * @see http://ffmpeg.org/ffmpeg.html#Main-options
93 */
94 $outputFormat = $returnBase64 ? "image2pipe" : "image2";
95 if (!$accurate) {
96 $commands = array(
97 '-y', '-ss', (string) $this->timecode,
98 '-i', $this->pathfile,
99 '-vframes', '1',
100 '-f', $outputFormat
101 );
102 } else {
103 $commands = array(
104 '-y', '-i', $this->pathfile,
105 '-vframes', '1', '-ss', (string) $this->timecode,
106 '-f', $outputFormat
107 );
108 }
109  
110 if($returnBase64) {
111 array_push($commands, "-");
112 }
113  
114 foreach ($this->filters as $filter) {
115 $commands = array_merge($commands, $filter->apply($this));
116 }
117  
118 $commands = array_merge($commands, array($pathfile));
119  
120 try {
121 if(!$returnBase64) {
122 $this->driver->command($commands);
123 return $this;
124 }
125 else {
126 return $this->driver->command($commands);
127 }
128 } catch (ExecutionFailureException $e) {
129 $this->cleanupTemporaryFile($pathfile);
130 throw new RuntimeException('Unable to save frame', $e->getCode(), $e);
131 }
132 }
133 }