| 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 |
||
| 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 |
||
| 271 |