Conditions | 13 |
Paths | 131 |
Total Lines | 84 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
143 | public function writeFile($sourceFile) |
||
144 | { |
||
145 | parent::writeFile($sourceFile); |
||
146 | |||
147 | |||
148 | // determine rotation metadata with exiftool |
||
149 | $exifToolOutput = exec("exiftool -S -Rotation $this->FilesystemPath"); |
||
150 | |||
151 | if (!$exifToolOutput || !preg_match('/Rotation\s*:\s*(?<rotation>\d+)/', $exifToolOutput, $matches)) { |
||
152 | throw new Exception('Unable to examine video with exiftool, ensure libimage-exiftool-perl is installed on the host system'); |
||
153 | } |
||
154 | |||
155 | $sourceRotation = intval($matches['rotation']); |
||
156 | |||
157 | |||
158 | // fork encoding job with each configured profile |
||
159 | foreach (static::$encodingProfiles as $profileName => $profile) { |
||
160 | if (empty($profile['enabled'])) { |
||
161 | continue; |
||
162 | } |
||
163 | |||
164 | |||
165 | // build paths and create directories if needed |
||
166 | $outputPath = $this->getFilesystemPath($profileName); |
||
167 | if (!is_dir($outputDir = dirname($outputPath))) { |
||
168 | mkdir($outputDir, static::$newDirectoryPermissions, true); |
||
169 | } |
||
170 | |||
171 | $tmpOutputPath = $outputDir.'/'.'tmp-'.basename($outputPath); |
||
172 | ; |
||
173 | |||
174 | |||
175 | // build avconv command |
||
176 | $cmd = ['avconv', '-loglevel quiet']; |
||
177 | |||
178 | // -- input options |
||
179 | if (!empty($profile['inputOptions'])) { |
||
180 | static::_appendAvconvOptions($cmd, $profile['inputOptions']); |
||
181 | } |
||
182 | $cmd[] = '-i'; |
||
183 | $cmd[] = $this->FilesystemPath; |
||
184 | |||
185 | // -- video output options |
||
186 | $cmd[] = '-codec:v'; |
||
187 | $cmd[] = $profile['videoCodec']; |
||
188 | if (!empty($profile['videoOptions'])) { |
||
189 | static::_appendAvconvOptions($cmd, $profile['videoOptions']); |
||
190 | } |
||
191 | |||
192 | // -- audio output options |
||
193 | $cmd[] = '-codec:a'; |
||
194 | $cmd[] = $profile['audioCodec']; |
||
195 | if (!empty($profile['audioOptions'])) { |
||
196 | static::_appendAvconvOptions($cmd, $profile['audioOptions']); |
||
197 | } |
||
198 | |||
199 | // -- normalize smartphone rotation |
||
200 | $cmd[] = '-metadata:s:v rotate="0"'; |
||
201 | |||
202 | if ($sourceRotation == 90) { |
||
203 | $cmd[] = '-vf "transpose=1"'; |
||
204 | } elseif ($sourceRotation == 180) { |
||
205 | $cmd[] = '-vf "transpose=1,transpose=1"'; |
||
206 | } elseif ($sourceRotation == 270) { |
||
207 | $cmd[] = '-vf "transpose=1,transpose=1,transpose=1"'; |
||
208 | } |
||
209 | |||
210 | // -- general output options |
||
211 | if (!empty($profile['outputOptions'])) { |
||
212 | static::_appendAvconvOptions($cmd, $profile['outputOptions']); |
||
213 | } |
||
214 | $cmd[] = $tmpOutputPath; |
||
215 | |||
216 | |||
217 | // move to final path after it finished |
||
218 | $cmd[] = "&& mv $tmpOutputPath $outputPath"; |
||
219 | |||
220 | |||
221 | // convert command to string and decorate for process control |
||
222 | $cmd = '(nohup '.implode(' ', $cmd).') > /dev/null 2>/dev/null & echo $! &'; |
||
223 | |||
224 | |||
225 | // execute command and retrieve the spawned PID |
||
226 | $pid = exec($cmd); |
||
227 | // TODO: store PID somewhere in APCU cache so we can do something smarter when a video is requested before it's done encoding |
||
276 |