Total Complexity | 43 |
Total Lines | 261 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Video often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Video, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class Video extends Media |
||
7 | { |
||
8 | // configurables |
||
9 | public static $ExtractFrameCommand = 'avconv -ss %2$u -i %1$s -an -vframes 1 -f mjpeg -'; // 1=video path, 2=position |
||
10 | public static $ExtractFramePosition = 3; |
||
11 | public static $encodingProfiles = [ |
||
12 | // from https://www.virag.si/2012/01/web-video-encoding-tutorial-with-ffmpeg-0-9/ |
||
13 | 'h264-high-480p' => [ |
||
14 | 'enabled' => true, |
||
15 | 'extension' => 'mp4', |
||
16 | 'mimeType' => 'video/mp4', |
||
17 | 'inputOptions' => [], |
||
18 | 'videoCodec' => 'h264', |
||
19 | 'videoOptions' => [ |
||
20 | 'profile:v' => 'high', |
||
21 | 'preset' => 'slow', |
||
22 | 'b:v' => '500k', |
||
23 | 'maxrate' => '500k', |
||
24 | 'bufsize' => '1000k', |
||
25 | 'vf' => 'scale="trunc(oh*a/2)*2:480"', // http://superuser.com/questions/571141/ffmpeg-avconv-force-scaled-output-to-be-divisible-by-2 |
||
26 | ], |
||
27 | 'audioCodec' => 'aac', |
||
28 | 'audioOptions' => [ |
||
29 | 'strict' => 'experimental', |
||
30 | ], |
||
31 | ], |
||
32 | |||
33 | // from http://superuser.com/questions/556463/converting-video-to-webm-with-ffmpeg-avconv |
||
34 | 'webm-480p' => [ |
||
35 | 'enabled' => true, |
||
36 | 'extension' => 'webm', |
||
37 | 'mimeType' => 'video/webm', |
||
38 | 'inputOptions' => [], |
||
39 | 'videoCodec' => 'libvpx', |
||
40 | 'videoOptions' => [ |
||
41 | 'vf' => 'scale=-1:480', |
||
42 | ], |
||
43 | 'audioCodec' => 'libvorbis', |
||
44 | ], |
||
45 | ]; |
||
46 | |||
47 | |||
48 | // magic methods |
||
49 | public static function __classLoaded() |
||
58 | } |
||
59 | |||
60 | |||
61 | public function getValue($name) |
||
86 | } |
||
87 | } |
||
88 | |||
89 | |||
90 | // public methods |
||
91 | public function getImage($sourceFile = null) |
||
92 | { |
||
93 | if (!isset($sourceFile)) { |
||
94 | $sourceFile = $this->FilesystemPath ? $this->FilesystemPath : $this->BlankPath; |
||
95 | } |
||
96 | |||
97 | $cmd = sprintf(self::$ExtractFrameCommand, $sourceFile, min(self::$ExtractFramePosition, floor($this->Duration))); |
||
98 | |||
99 | if ($imageData = shell_exec($cmd)) { |
||
100 | return imagecreatefromstring($imageData); |
||
101 | } elseif ($sourceFile != $this->BlankPath) { |
||
102 | return static::getImage($this->BlankPath); |
||
103 | } |
||
104 | |||
105 | return null; |
||
106 | } |
||
107 | |||
108 | // static methods |
||
109 | public static function analyzeFile($filename, $mediaInfo = []) |
||
110 | { |
||
111 | // examine media with avprobe |
||
112 | $output = shell_exec("avprobe -of json -show_streams -v quiet $filename"); |
||
113 | |||
114 | if (!$output || !($output = json_decode($output, true)) || empty($output['streams'])) { |
||
115 | throw new MediaTypeException('Unable to examine video with avprobe, ensure lib-avtools is installed on the host system'); |
||
116 | } |
||
117 | |||
118 | // extract video streams |
||
119 | $videoStreams = array_filter($output['streams'], function ($streamInfo) { |
||
120 | return $streamInfo['codec_type'] == 'video'; |
||
121 | }); |
||
122 | |||
123 | if (!count($videoStreams)) { |
||
124 | throw new Exception('avprobe did not detect any video streams'); |
||
125 | } |
||
126 | |||
127 | // convert and write interesting information to mediaInfo |
||
128 | $mediaInfo['streams'] = $output['streams']; |
||
129 | $mediaInfo['videoStream'] = array_shift($videoStreams); |
||
130 | |||
131 | $mediaInfo['width'] = (int)$mediaInfo['videoStream']['width']; |
||
132 | $mediaInfo['height'] = (int)$mediaInfo['videoStream']['height']; |
||
133 | $mediaInfo['duration'] = (double)$mediaInfo['videoStream']['duration']; |
||
134 | |||
135 | return $mediaInfo; |
||
136 | } |
||
137 | |||
138 | public function writeFile($sourceFile) |
||
139 | { |
||
140 | parent::writeFile($sourceFile); |
||
141 | |||
142 | |||
143 | // determine rotation metadata with exiftool |
||
144 | $exifToolOutput = exec("exiftool -S -Rotation $this->FilesystemPath"); |
||
145 | |||
146 | if (!$exifToolOutput || !preg_match('/Rotation\s*:\s*(?<rotation>\d+)/', $exifToolOutput, $matches)) { |
||
147 | throw new Exception('Unable to examine video with exiftool, ensure libimage-exiftool-perl is installed on the host system'); |
||
148 | } |
||
149 | |||
150 | $sourceRotation = intval($matches['rotation']); |
||
151 | |||
152 | |||
153 | // fork encoding job with each configured profile |
||
154 | foreach (static::$encodingProfiles as $profileName => $profile) { |
||
155 | if (empty($profile['enabled'])) { |
||
156 | continue; |
||
157 | } |
||
158 | |||
159 | |||
160 | // build paths and create directories if needed |
||
161 | $outputPath = $this->getFilesystemPath($profileName); |
||
162 | if (!is_dir($outputDir = dirname($outputPath))) { |
||
163 | mkdir($outputDir, static::$newDirectoryPermissions, true); |
||
164 | } |
||
165 | |||
166 | $tmpOutputPath = $outputDir.'/'.'tmp-'.basename($outputPath); |
||
167 | ; |
||
168 | |||
169 | |||
170 | // build avconv command |
||
171 | $cmd = ['avconv', '-loglevel quiet']; |
||
172 | |||
173 | // -- input options |
||
174 | if (!empty($profile['inputOptions'])) { |
||
175 | static::_appendAvconvOptions($cmd, $profile['inputOptions']); |
||
176 | } |
||
177 | $cmd[] = '-i'; |
||
178 | $cmd[] = $this->FilesystemPath; |
||
179 | |||
180 | // -- video output options |
||
181 | $cmd[] = '-codec:v'; |
||
182 | $cmd[] = $profile['videoCodec']; |
||
183 | if (!empty($profile['videoOptions'])) { |
||
184 | static::_appendAvconvOptions($cmd, $profile['videoOptions']); |
||
185 | } |
||
186 | |||
187 | // -- audio output options |
||
188 | $cmd[] = '-codec:a'; |
||
189 | $cmd[] = $profile['audioCodec']; |
||
190 | if (!empty($profile['audioOptions'])) { |
||
191 | static::_appendAvconvOptions($cmd, $profile['audioOptions']); |
||
192 | } |
||
193 | |||
194 | // -- normalize smartphone rotation |
||
195 | $cmd[] = '-metadata:s:v rotate="0"'; |
||
196 | |||
197 | if ($sourceRotation == 90) { |
||
198 | $cmd[] = '-vf "transpose=1"'; |
||
199 | } elseif ($sourceRotation == 180) { |
||
200 | $cmd[] = '-vf "transpose=1,transpose=1"'; |
||
201 | } elseif ($sourceRotation == 270) { |
||
202 | $cmd[] = '-vf "transpose=1,transpose=1,transpose=1"'; |
||
203 | } |
||
204 | |||
205 | // -- general output options |
||
206 | if (!empty($profile['outputOptions'])) { |
||
207 | static::_appendAvconvOptions($cmd, $profile['outputOptions']); |
||
208 | } |
||
209 | $cmd[] = $tmpOutputPath; |
||
210 | |||
211 | |||
212 | // move to final path after it finished |
||
213 | $cmd[] = "&& mv $tmpOutputPath $outputPath"; |
||
214 | |||
215 | |||
216 | // convert command to string and decorate for process control |
||
217 | $cmd = '(nohup '.implode(' ', $cmd).') > /dev/null 2>/dev/null & echo $! &'; |
||
218 | |||
219 | |||
220 | // execute command and retrieve the spawned PID |
||
221 | $pid = exec($cmd); |
||
222 | // TODO: store PID somewhere in APCU cache so we can do something smarter when a video is requested before it's done encoding |
||
223 | } |
||
224 | } |
||
225 | |||
226 | public function getFilesystemPath($variant = 'original', $filename = null) |
||
227 | { |
||
228 | if (!$filename && array_key_exists($variant, static::$encodingProfiles)) { |
||
229 | $filename = $this->ID.'.'.static::$encodingProfiles[$variant]['extension']; |
||
230 | $variant = 'video-'.$variant; |
||
231 | } |
||
232 | |||
233 | return parent::getFilesystemPath($variant, $filename); |
||
234 | } |
||
235 | |||
236 | public function getMIMEType($variant = 'original') |
||
243 | } |
||
244 | |||
245 | public function isVariantAvailable($variant) |
||
246 | { |
||
256 | } |
||
257 | |||
258 | protected static function _appendAvconvOptions(array &$cmd, array $options) |
||
267 | } |
||
268 | } |
||
271 |