| Total Complexity | 88 |
| Total Lines | 458 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MediaExtractionService 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 MediaExtractionService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class MediaExtractionService |
||
| 31 | { |
||
| 32 | private ?FFMpeg $ffmpeg = null; |
||
| 33 | private ?FFProbe $ffprobe = null; |
||
| 34 | private ?MediaInfo $mediaInfo = null; |
||
| 35 | private ?ManticoreSearch $manticore = null; |
||
| 36 | private ?ElasticSearchSiteSearch $elasticsearch = null; |
||
| 37 | |||
| 38 | public function __construct( |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Get video time code for sample extraction. |
||
| 47 | */ |
||
| 48 | public function getVideoTime(string $videoLocation): string |
||
| 49 | { |
||
| 50 | try { |
||
| 51 | if (! $this->ffprobe()->isValid($videoLocation)) { |
||
| 52 | return ''; |
||
| 53 | } |
||
| 54 | $time = $this->ffprobe()->format($videoLocation)->get('duration'); |
||
| 55 | } catch (\Throwable $e) { |
||
| 56 | if ($this->config->debugMode) { |
||
| 57 | Log::debug($e->getMessage()); |
||
| 58 | } |
||
| 59 | return ''; |
||
| 60 | } |
||
| 61 | |||
| 62 | if (empty($time) || ! preg_match('/time=(\d{1,2}:\d{1,2}:)?(\d{1,2})\.(\d{1,2})\s*bitrate=/i', $time, $numbers)) { |
||
| 63 | return ''; |
||
| 64 | } |
||
| 65 | |||
| 66 | if ($numbers[3] > 0) { |
||
| 67 | $numbers[3]--; |
||
| 68 | } elseif ($numbers[1] > 0) { |
||
| 69 | $numbers[2]--; |
||
| 70 | $numbers[3] = '99'; |
||
| 71 | } |
||
| 72 | |||
| 73 | return '00:00:'.str_pad($numbers[2], 2, '0', STR_PAD_LEFT).'.'.str_pad($numbers[3], 2, '0', STR_PAD_LEFT); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Extract a sample image from a video file. |
||
| 78 | */ |
||
| 79 | public function getSample(string $fileLocation, string $tmpPath, string $guid): bool |
||
| 80 | { |
||
| 81 | if (! $this->config->processThumbnails || ! File::isFile($fileLocation)) { |
||
| 82 | return false; |
||
| 83 | } |
||
| 84 | |||
| 85 | $fileName = $tmpPath.'zzzz'.random_int(5, 12).random_int(5, 12).'.jpg'; |
||
| 86 | $time = $this->getVideoTime($fileLocation); |
||
| 87 | |||
| 88 | try { |
||
| 89 | if ($this->ffprobe()->isValid($fileLocation)) { |
||
| 90 | $this->ffmpeg()->open($fileLocation) |
||
| 91 | ->frame(TimeCode::fromString($time === '' ? '00:00:03:00' : $time)) |
||
| 92 | ->save($fileName); |
||
| 93 | } |
||
| 94 | } catch (\Throwable $e) { |
||
| 95 | if ($this->config->debugMode) { |
||
| 96 | Log::error($e->getTraceAsString()); |
||
| 97 | } |
||
| 98 | return false; |
||
| 99 | } |
||
| 100 | |||
| 101 | if (! File::isFile($fileName)) { |
||
| 102 | return false; |
||
| 103 | } |
||
| 104 | |||
| 105 | $saved = $this->releaseImage->saveImage( |
||
| 106 | $guid.'_thumb', |
||
| 107 | $fileName, |
||
| 108 | $this->releaseImage->imgSavePath, |
||
| 109 | 800, |
||
| 110 | 600 |
||
| 111 | ); |
||
| 112 | |||
| 113 | File::delete($fileName); |
||
| 114 | |||
| 115 | return $saved === 1; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Create a video sample clip. |
||
| 120 | */ |
||
| 121 | public function getVideo(string $fileLocation, string $tmpPath, string $guid): bool |
||
| 122 | { |
||
| 123 | if (! $this->config->processVideo || ! File::isFile($fileLocation)) { |
||
| 124 | return false; |
||
| 125 | } |
||
| 126 | |||
| 127 | $fileName = $tmpPath.'zzzz'.$guid.'.ogv'; |
||
| 128 | $newMethod = false; |
||
| 129 | |||
| 130 | // Try to get sample from end of video if duration is short |
||
| 131 | if ($this->config->ffmpegDuration < 60) { |
||
| 132 | $time = $this->getVideoTime($fileLocation); |
||
| 133 | if ($time !== '' && preg_match('/(\d{2}).(\d{2})/', $time, $numbers)) { |
||
| 134 | $newMethod = true; |
||
| 135 | if ($numbers[1] <= $this->config->ffmpegDuration) { |
||
| 136 | $lowestLength = '00:00:00.00'; |
||
| 137 | } else { |
||
| 138 | $lowestLength = ($numbers[1] - $this->config->ffmpegDuration); |
||
| 139 | $end = '.'.$numbers[2]; |
||
| 140 | $lowestLength = match (strlen($lowestLength)) { |
||
| 141 | 1 => '00:00:0'.$lowestLength.$end, |
||
| 142 | 2 => '00:00:'.$lowestLength.$end, |
||
| 143 | default => '00:00:60.00', |
||
| 144 | }; |
||
| 145 | } |
||
| 146 | |||
| 147 | try { |
||
| 148 | if ($this->ffprobe()->isValid($fileLocation)) { |
||
| 149 | $video = $this->ffmpeg()->open($fileLocation); |
||
| 150 | $clip = $video->clip( |
||
| 151 | TimeCode::fromString($lowestLength), |
||
| 152 | TimeCode::fromSeconds($this->config->ffmpegDuration) |
||
| 153 | ); |
||
| 154 | $format = new Ogg(); |
||
| 155 | $format->setAudioCodec('libvorbis'); |
||
| 156 | $clip->filters()->resize(new Dimension(320, -1), ResizeFilter::RESIZEMODE_SCALE_HEIGHT); |
||
| 157 | $clip->save($format, $fileName); |
||
| 158 | } |
||
| 159 | } catch (\Throwable $e) { |
||
| 160 | if ($this->config->debugMode) { |
||
| 161 | Log::error($e->getTraceAsString()); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | // Fallback: use start of video |
||
| 168 | if (! $newMethod) { |
||
| 169 | try { |
||
| 170 | if ($this->ffprobe()->isValid($fileLocation)) { |
||
| 171 | $video = $this->ffmpeg()->open($fileLocation); |
||
| 172 | $clip = $video->clip( |
||
| 173 | TimeCode::fromSeconds(0), |
||
| 174 | TimeCode::fromSeconds($this->config->ffmpegDuration) |
||
| 175 | ); |
||
| 176 | $format = new Ogg(); |
||
| 177 | $format->setAudioCodec('libvorbis'); |
||
| 178 | $clip->filters()->resize(new Dimension(320, -1), ResizeFilter::RESIZEMODE_SCALE_HEIGHT); |
||
| 179 | $clip->save($format, $fileName); |
||
| 180 | } |
||
| 181 | } catch (\Throwable $e) { |
||
| 182 | if ($this->config->debugMode) { |
||
| 183 | Log::error($e->getTraceAsString()); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | if (! File::isFile($fileName)) { |
||
| 189 | return false; |
||
| 190 | } |
||
| 191 | |||
| 192 | $newFile = $this->releaseImage->vidSavePath.$guid.'.ogv'; |
||
| 193 | |||
| 194 | if (! @File::move($fileName, $newFile)) { |
||
| 195 | $copied = @File::copy($fileName, $newFile); |
||
| 196 | File::delete($fileName); |
||
| 197 | if (! $copied) { |
||
| 198 | return false; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | @chmod($newFile, 0764); |
||
| 203 | Release::query()->where('guid', $guid)->update(['videostatus' => 1]); |
||
| 204 | |||
| 205 | return true; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Extract media info from a video file. |
||
| 210 | */ |
||
| 211 | public function getMediaInfo(string $fileLocation, int $releaseId): bool |
||
| 212 | { |
||
| 213 | if (! $this->config->processMediaInfo || ! File::isFile($fileLocation)) { |
||
| 214 | return false; |
||
| 215 | } |
||
| 216 | |||
| 217 | try { |
||
| 218 | $xmlArray = $this->mediaInfo()->getInfo($fileLocation, true); |
||
| 219 | \App\Models\MediaInfo::addData($releaseId, $xmlArray); |
||
| 220 | $this->releaseExtra->addFromXml($releaseId, $xmlArray); |
||
| 221 | return true; |
||
| 222 | } catch (\Throwable $e) { |
||
| 223 | Log::debug($e->getMessage()); |
||
| 224 | return false; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Process a JPG sample image. |
||
| 230 | */ |
||
| 231 | public function getJPGSample(string $fileLocation, string $guid): bool |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Process audio file for media info and sample. |
||
| 251 | * |
||
| 252 | * @return array{audioInfo: bool, audioSample: bool} |
||
| 253 | */ |
||
| 254 | public function getAudioInfo( |
||
| 255 | string $fileLocation, |
||
| 256 | string $fileExtension, |
||
| 257 | ReleaseProcessingContext $context, |
||
| 258 | string $tmpPath |
||
| 259 | ): array { |
||
| 260 | $result = ['audioInfo' => false, 'audioSample' => false]; |
||
| 261 | |||
| 262 | if (! $this->config->processAudioSample) { |
||
| 263 | $result['audioSample'] = true; |
||
| 264 | } |
||
| 265 | if (! $this->config->processAudioInfo) { |
||
| 266 | $result['audioInfo'] = true; |
||
| 267 | } |
||
| 268 | |||
| 269 | $rQuery = Release::query() |
||
| 270 | ->where('proc_pp', '=', 0) |
||
| 271 | ->where('id', $context->release->id) |
||
| 272 | ->select(['searchname', 'fromname', 'categories_id', 'groups_id']) |
||
| 273 | ->first(); |
||
| 274 | |||
| 275 | $musicParent = (string) Category::MUSIC_ROOT; |
||
| 276 | if ($rQuery === null || ! preg_match( |
||
| 277 | sprintf( |
||
| 278 | '/%d\d{3}|%d|%d|%d/', |
||
| 279 | $musicParent[0], |
||
| 280 | Category::OTHER_MISC, |
||
| 281 | Category::MOVIE_OTHER, |
||
| 282 | Category::TV_OTHER |
||
| 283 | ), |
||
| 284 | $rQuery->categories_id |
||
| 285 | )) { |
||
| 286 | return $result; |
||
| 287 | } |
||
| 288 | |||
| 289 | if (! File::isFile($fileLocation)) { |
||
| 290 | return $result; |
||
| 291 | } |
||
| 292 | |||
| 293 | // Get media info |
||
| 294 | if (! $result['audioInfo']) { |
||
| 295 | try { |
||
| 296 | $xmlArray = $this->mediaInfo()->getInfo($fileLocation, false); |
||
| 297 | if ($xmlArray !== null) { |
||
| 298 | foreach ($xmlArray->getAudios() as $track) { |
||
| 299 | if ($track->get('album') !== null && $track->get('performer') !== null) { |
||
| 300 | if ((int) $context->release->predb_id === 0 && $this->config->renameMusicMediaInfo) { |
||
| 301 | $ext = strtoupper($fileExtension); |
||
| 302 | |||
| 303 | $newName = $track->get('performer')->getFullName().' - '.$track->get('album')->getFullName(); |
||
| 304 | if (! empty($track->get('recorded_date')) |
||
| 305 | && preg_match('/(?:19|20)\d\d/', $track->get('recorded_date')->getFullname, $Year) |
||
| 306 | ) { |
||
| 307 | $newName .= ' ('.$Year[0].') '.$ext; |
||
| 308 | } else { |
||
| 309 | $newName .= ' '.$ext; |
||
| 310 | } |
||
| 311 | |||
| 312 | $newCat = match ($ext) { |
||
| 313 | 'MP3' => Category::MUSIC_MP3, |
||
| 314 | 'FLAC' => Category::MUSIC_LOSSLESS, |
||
| 315 | default => $this->categorize->determineCategory($rQuery->groups_id, $newName, $rQuery->fromname), |
||
| 316 | }; |
||
| 317 | |||
| 318 | $newTitle = escapeString(substr($newName, 0, 255)); |
||
| 319 | Release::whereId($context->release->id)->update([ |
||
| 320 | 'searchname' => $newTitle, |
||
| 321 | 'categories_id' => is_array($newCat) ? $newCat['categories_id'] : $newCat, |
||
| 322 | 'iscategorized' => 1, |
||
| 323 | 'isrenamed' => 1, |
||
| 324 | 'proc_pp' => 1, |
||
| 325 | ]); |
||
| 326 | |||
| 327 | if ($this->config->elasticsearchEnabled) { |
||
| 328 | $this->elasticsearch()->updateRelease($context->release->id); |
||
| 329 | } else { |
||
| 330 | $this->manticore()->updateRelease($context->release->id); |
||
| 331 | } |
||
| 332 | |||
| 333 | if ($this->config->echoCLI) { |
||
| 334 | NameFixer::echoChangedReleaseName([ |
||
| 335 | 'new_name' => $newTitle, |
||
| 336 | 'old_name' => $rQuery->searchname, |
||
| 337 | 'new_category' => $newCat, |
||
| 338 | 'old_category' => $rQuery->categories_id, |
||
| 339 | 'group' => $rQuery->groups_id, |
||
| 340 | 'releases_id' => $context->release->id, |
||
| 341 | 'method' => 'MediaExtractionService->getAudioInfo', |
||
| 342 | ]); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | $this->releaseExtra->addFromXml($context->release->id, $xmlArray); |
||
| 347 | $result['audioInfo'] = true; |
||
| 348 | $context->foundAudioInfo = true; |
||
| 349 | break; |
||
| 350 | } |
||
| 351 | } |
||
| 352 | } |
||
| 353 | } catch (\Throwable $e) { |
||
| 354 | Log::debug($e->getMessage()); |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | // Create audio sample |
||
| 359 | if (! $result['audioSample']) { |
||
| 360 | $audioFileName = $context->release->guid.'.ogg'; |
||
| 361 | |||
| 362 | try { |
||
| 363 | if ($this->ffprobe()->isValid($fileLocation)) { |
||
| 364 | $audioSample = $this->ffmpeg()->open($fileLocation); |
||
| 365 | $format = new Vorbis(); |
||
| 366 | $audioSample->clip(TimeCode::fromSeconds(30), TimeCode::fromSeconds(30)); |
||
| 367 | $audioSample->save($format, $tmpPath.$audioFileName); |
||
| 368 | } |
||
| 369 | } catch (\Throwable $e) { |
||
| 370 | if ($this->config->debugMode) { |
||
| 371 | Log::error($e->getTraceAsString()); |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | if (File::isFile($tmpPath.$audioFileName)) { |
||
| 376 | $renamed = File::move($tmpPath.$audioFileName, $this->config->audioSavePath.$audioFileName); |
||
| 377 | if (! $renamed) { |
||
| 378 | $copied = File::copy($tmpPath.$audioFileName, $this->config->audioSavePath.$audioFileName); |
||
| 379 | File::delete($tmpPath.$audioFileName); |
||
| 380 | if (! $copied) { |
||
| 381 | return $result; |
||
| 382 | } |
||
| 383 | } |
||
| 384 | |||
| 385 | @chmod($this->config->audioSavePath.$audioFileName, 0764); |
||
| 386 | Release::query()->where('id', $context->release->id)->update(['audiostatus' => 1]); |
||
| 387 | $result['audioSample'] = true; |
||
| 388 | $context->foundAudioSample = true; |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | return $result; |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Process a video file for sample, video clip, and media info. |
||
| 397 | */ |
||
| 398 | public function processVideoFile( |
||
| 399 | string $fileLocation, |
||
| 400 | ReleaseProcessingContext $context, |
||
| 401 | string $tmpPath |
||
| 402 | ): array { |
||
| 403 | $result = [ |
||
| 404 | 'sample' => false, |
||
| 405 | 'video' => false, |
||
| 406 | 'mediaInfo' => false, |
||
| 407 | ]; |
||
| 408 | |||
| 409 | if (! $context->foundSample) { |
||
| 410 | $result['sample'] = $this->getSample($fileLocation, $tmpPath, $context->release->guid); |
||
| 411 | if ($result['sample']) { |
||
| 412 | $context->foundSample = true; |
||
| 413 | } |
||
| 414 | } |
||
| 415 | |||
| 416 | // Only get video if sampleMessageIDs count is less than 2 |
||
| 417 | if (! $context->foundVideo && count($context->sampleMessageIDs) < 2) { |
||
| 418 | $result['video'] = $this->getVideo($fileLocation, $tmpPath, $context->release->guid); |
||
| 419 | if ($result['video']) { |
||
| 420 | $context->foundVideo = true; |
||
| 421 | } |
||
| 422 | } |
||
| 423 | |||
| 424 | if (! $context->foundMediaInfo) { |
||
| 425 | $result['mediaInfo'] = $this->getMediaInfo($fileLocation, $context->release->id); |
||
| 426 | if ($result['mediaInfo']) { |
||
| 427 | $context->foundMediaInfo = true; |
||
| 428 | } |
||
| 429 | } |
||
| 430 | |||
| 431 | return $result; |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Check if data appears to be a JPEG image. |
||
| 436 | */ |
||
| 437 | public function isJpegData(string $filePath): bool |
||
| 438 | { |
||
| 439 | if (! File::isFile($filePath)) { |
||
| 440 | return false; |
||
| 441 | } |
||
| 442 | return exif_imagetype($filePath) === IMAGETYPE_JPEG; |
||
| 443 | } |
||
| 444 | |||
| 445 | private function ffmpeg(): FFMpeg |
||
| 452 | } |
||
| 453 | |||
| 454 | private function ffprobe(): FFProbe |
||
| 460 | } |
||
| 461 | |||
| 462 | private function mediaInfo(): MediaInfo |
||
| 463 | { |
||
| 472 | } |
||
| 473 | |||
| 474 | private function manticore(): ManticoreSearch |
||
| 480 | } |
||
| 481 | |||
| 482 | private function elasticsearch(): ElasticSearchSiteSearch |
||
| 488 | } |
||
| 489 | } |
||
| 491 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths