| Total Complexity | 113 |
| Total Lines | 857 |
| Duplicated Lines | 0 % |
| Changes | 19 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Transcode 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 Transcode, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class Transcode extends Component |
||
| 35 | { |
||
| 36 | // Constants |
||
| 37 | // ========================================================================= |
||
| 38 | |||
| 39 | // Suffixes to add to the generated filename params |
||
| 40 | const SUFFIX_MAP = [ |
||
| 41 | 'videoFrameRate' => 'fps', |
||
| 42 | 'videoBitRate' => 'bps', |
||
| 43 | 'audioBitRate' => 'bps', |
||
| 44 | 'audioChannels' => 'c', |
||
| 45 | 'height' => 'h', |
||
| 46 | 'width' => 'w', |
||
| 47 | 'timeInSecs' => 's', |
||
| 48 | ]; |
||
| 49 | |||
| 50 | // Params that should be excluded from being part of the generated filename |
||
| 51 | const EXCLUDE_PARAMS = [ |
||
| 52 | 'videoEncoder', |
||
| 53 | 'audioEncoder', |
||
| 54 | 'fileSuffix', |
||
| 55 | 'sharpen', |
||
| 56 | ]; |
||
| 57 | |||
| 58 | // Mappings for getFileInfo() summary values |
||
| 59 | const INFO_SUMMARY = [ |
||
| 60 | 'format' => [ |
||
| 61 | 'filename' => 'filename', |
||
| 62 | 'duration' => 'duration', |
||
| 63 | 'size' => 'size', |
||
| 64 | ], |
||
| 65 | 'audio' => [ |
||
| 66 | 'codec_name' => 'audioEncoder', |
||
| 67 | 'bit_rate' => 'audioBitRate', |
||
| 68 | 'sample_rate' => 'audioSampleRate', |
||
| 69 | 'channels' => 'audioChannels', |
||
| 70 | ], |
||
| 71 | 'video' => [ |
||
| 72 | 'codec_name' => 'videoEncoder', |
||
| 73 | 'bit_rate' => 'videoBitRate', |
||
| 74 | 'avg_frame_rate' => 'videoFrameRate', |
||
| 75 | 'height' => 'height', |
||
| 76 | 'width' => 'width', |
||
| 77 | ], |
||
| 78 | ]; |
||
| 79 | |||
| 80 | // Public Methods |
||
| 81 | // ========================================================================= |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Returns a URL to the transcoded video or "" if it doesn't exist (at |
||
| 85 | * which |
||
| 86 | * time it will create it). |
||
| 87 | * |
||
| 88 | * @param $filePath string path to the original video -OR- an |
||
| 89 | * Asset |
||
| 90 | * @param $videoOptions array of options for the video |
||
| 91 | * @param bool $generate whether the video should be encoded |
||
| 92 | * |
||
| 93 | * @return string URL of the transcoded video or "" |
||
| 94 | */ |
||
| 95 | public function getVideoUrl($filePath, $videoOptions, $generate = true): string |
||
| 96 | { |
||
| 97 | $result = ''; |
||
| 98 | $settings = Transcoder::$plugin->getSettings(); |
||
| 99 | $subfolder = ''; |
||
| 100 | |||
| 101 | // sub folder check |
||
| 102 | if (\is_object($filePath) && ($filePath instanceof Asset) && $settings['createSubfolders']) { |
||
| 103 | $subfolder = $filePath->folderPath; |
||
| 104 | } |
||
| 105 | |||
| 106 | // file path |
||
| 107 | $filePath = $this->getAssetPath($filePath); |
||
| 108 | |||
| 109 | if (!empty($filePath)) { |
||
| 110 | $destVideoPath = $settings['transcoderPaths']['video'].$subfolder ?? $settings['transcoderPaths']['default']; |
||
| 111 | $destVideoPath = Craft::getAlias($destVideoPath); |
||
| 112 | $videoOptions = $this->coalesceOptions('defaultVideoOptions', $videoOptions); |
||
| 113 | |||
| 114 | // Get the video encoder presets to use |
||
| 115 | $videoEncoders = $settings['videoEncoders']; |
||
| 116 | $thisEncoder = $videoEncoders[$videoOptions['videoEncoder']]; |
||
| 117 | |||
| 118 | $videoOptions['fileSuffix'] = $thisEncoder['fileSuffix']; |
||
| 119 | |||
| 120 | // Build the basic command for ffmpeg |
||
| 121 | $ffmpegCmd = $settings['ffmpegPath'] |
||
| 122 | .' -i '.escapeshellarg($filePath) |
||
| 123 | .' -vcodec '.$thisEncoder['videoCodec'] |
||
| 124 | .' '.$thisEncoder['videoCodecOptions'] |
||
| 125 | .' -bufsize 1000k' |
||
| 126 | .' -threads '.$thisEncoder['threads']; |
||
| 127 | |||
| 128 | // Set the framerate if desired |
||
| 129 | if (!empty($videoOptions['videoFrameRate'])) { |
||
| 130 | $ffmpegCmd .= ' -r '.$videoOptions['videoFrameRate']; |
||
| 131 | } |
||
| 132 | |||
| 133 | // Set the bitrate if desired |
||
| 134 | if (!empty($videoOptions['videoBitRate'])) { |
||
| 135 | $ffmpegCmd .= ' -b:v '.$videoOptions['videoBitRate'].' -maxrate '.$videoOptions['videoBitRate']; |
||
| 136 | } |
||
| 137 | |||
| 138 | // Adjust the scaling if desired |
||
| 139 | $ffmpegCmd = $this->addScalingFfmpegArgs( |
||
| 140 | $videoOptions, |
||
| 141 | $ffmpegCmd |
||
| 142 | ); |
||
| 143 | |||
| 144 | // Handle any audio transcoding |
||
| 145 | if (empty($videoOptions['audioBitRate']) |
||
| 146 | && empty($videoOptions['audioSampleRate']) |
||
| 147 | && empty($videoOptions['audioChannels']) |
||
| 148 | ) { |
||
| 149 | // Just copy the audio if no options are provided |
||
| 150 | $ffmpegCmd .= ' -c:a copy'; |
||
| 151 | } else { |
||
| 152 | // Do audio transcoding based on the settings |
||
| 153 | $ffmpegCmd .= ' -acodec '.$thisEncoder['audioCodec']; |
||
| 154 | if (!empty($videoOptions['audioBitRate'])) { |
||
| 155 | $ffmpegCmd .= ' -b:a '.$videoOptions['audioBitRate']; |
||
| 156 | } |
||
| 157 | if (!empty($videoOptions['audioSampleRate'])) { |
||
| 158 | $ffmpegCmd .= ' -ar '.$videoOptions['audioSampleRate']; |
||
| 159 | } |
||
| 160 | if (!empty($videoOptions['audioChannels'])) { |
||
| 161 | $ffmpegCmd .= ' -ac '.$videoOptions['audioChannels']; |
||
| 162 | } |
||
| 163 | $ffmpegCmd .= ' '.$thisEncoder['audioCodecOptions']; |
||
| 164 | } |
||
| 165 | |||
| 166 | // Create the directory if it isn't there already |
||
| 167 | if (!is_dir($destVideoPath)) { |
||
| 168 | try { |
||
| 169 | FileHelper::createDirectory($destVideoPath); |
||
| 170 | } catch (Exception $e) { |
||
| 171 | Craft::error($e->getMessage(), __METHOD__); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | $destVideoFile = $this->getFilename($filePath, $videoOptions); |
||
| 176 | |||
| 177 | // File to store the video encoding progress in |
||
| 178 | $progressFile = sys_get_temp_dir().DIRECTORY_SEPARATOR.$destVideoFile.'.progress'; |
||
| 179 | |||
| 180 | // Assemble the destination path and final ffmpeg command |
||
| 181 | $destVideoPath .= $destVideoFile; |
||
| 182 | $ffmpegCmd .= ' -f ' |
||
| 183 | .$thisEncoder['fileFormat'] |
||
| 184 | .' -y '.escapeshellarg($destVideoPath) |
||
| 185 | .' 1> '.$progressFile.' 2>&1 & echo $!'; |
||
| 186 | |||
| 187 | // Make sure there isn't a lockfile for this video already |
||
| 188 | $lockFile = sys_get_temp_dir().DIRECTORY_SEPARATOR.$destVideoFile.'.lock'; |
||
| 189 | $oldPid = @file_get_contents($lockFile); |
||
| 190 | if ($oldPid !== false) { |
||
| 191 | exec("ps $oldPid", $ProcessState); |
||
| 192 | if (\count($ProcessState) >= 2) { |
||
| 193 | return $result; |
||
| 194 | } |
||
| 195 | // It's finished transcoding, so delete the lockfile and progress file |
||
| 196 | @unlink($lockFile); |
||
| 197 | @unlink($progressFile); |
||
| 198 | } |
||
| 199 | |||
| 200 | // If the video file already exists and hasn't been modified, return it. Otherwise, start it transcoding |
||
| 201 | if (file_exists($destVideoPath) && (@filemtime($destVideoPath) >= @filemtime($filePath))) { |
||
| 202 | $url = $settings['transcoderUrls']['video'].$subfolder ?? $settings['transcoderUrls']['default']; |
||
| 203 | $result = Craft::getAlias($url).$destVideoFile; |
||
| 204 | // skip encoding |
||
| 205 | } elseif (!$generate) { |
||
| 206 | $result = ""; |
||
| 207 | } else { |
||
| 208 | // Kick off the transcoding |
||
| 209 | $pid = $this->executeShellCommand($ffmpegCmd); |
||
| 210 | Craft::info($ffmpegCmd."\nffmpeg PID: ".$pid, __METHOD__); |
||
| 211 | |||
| 212 | // Create a lockfile in tmp |
||
| 213 | file_put_contents($lockFile, $pid); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | return $result; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Returns a URL to a video thumbnail |
||
| 222 | * |
||
| 223 | * @param string $filePath path to the original video or an Asset |
||
| 224 | * @param array $thumbnailOptions of options for the thumbnail |
||
| 225 | * @param bool $generate whether the thumbnail should be |
||
| 226 | * generated if it doesn't exists |
||
| 227 | * @param bool $asPath Whether we should return a path or not |
||
| 228 | * |
||
| 229 | * @return string|false|null URL or path of the video thumbnail |
||
| 230 | */ |
||
| 231 | public function getVideoThumbnailUrl($filePath, $thumbnailOptions, $generate = true, $asPath = false) |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Returns a URL to the transcoded audio file or "" if it doesn't exist |
||
| 315 | * (at which time it will create it). |
||
| 316 | * |
||
| 317 | * @param $filePath string path to the original audio file -OR- an Asset |
||
| 318 | * @param $audioOptions array of options for the audio file |
||
| 319 | * |
||
| 320 | * @return string URL of the transcoded audio file or "" |
||
| 321 | */ |
||
| 322 | public function getAudioUrl($filePath, $audioOptions): string |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Extract information from a video/audio file |
||
| 439 | * |
||
| 440 | * @param $filePath |
||
| 441 | * @param bool $summary |
||
| 442 | * |
||
| 443 | * @return null|array |
||
| 444 | */ |
||
| 445 | public function getFileInfo($filePath, $summary = false) |
||
| 446 | { |
||
| 447 | $result = null; |
||
| 448 | $settings = Transcoder::$plugin->getSettings(); |
||
| 449 | $filePath = $this->getAssetPath($filePath); |
||
| 450 | |||
| 451 | if (!empty($filePath)) { |
||
| 452 | // Build the basic command for ffprobe |
||
| 453 | $ffprobeOptions = $settings['ffprobeOptions']; |
||
| 454 | $ffprobeCmd = $settings['ffprobePath'] |
||
| 455 | .' '.$ffprobeOptions |
||
| 456 | .' '.escapeshellarg($filePath); |
||
| 457 | |||
| 458 | $shellOutput = $this->executeShellCommand($ffprobeCmd); |
||
| 459 | Craft::info($ffprobeCmd, __METHOD__); |
||
| 460 | $result = JsonHelper::decodeIfJson($shellOutput, true); |
||
| 461 | Craft::info(print_r($result, true), __METHOD__); |
||
| 462 | |||
| 463 | // Trim down the arrays to just a summary |
||
| 464 | if ($summary && !empty($result)) { |
||
| 465 | $summaryResult = []; |
||
| 466 | foreach ($result as $topLevelKey => $topLevelValue) { |
||
| 467 | switch ($topLevelKey) { |
||
| 468 | // Format info |
||
| 469 | case 'format': |
||
| 470 | foreach (self::INFO_SUMMARY['format'] as $settingKey => $settingValue) { |
||
| 471 | if (!empty($topLevelValue[$settingKey])) { |
||
| 472 | $summaryResult[$settingValue] = $topLevelValue[$settingKey]; |
||
| 473 | } |
||
| 474 | } |
||
| 475 | break; |
||
| 476 | // Stream info |
||
| 477 | case 'streams': |
||
| 478 | foreach ($topLevelValue as $stream) { |
||
| 479 | $infoSummaryType = $stream['codec_type']; |
||
| 480 | if (in_array($infoSummaryType, self::INFO_SUMMARY, false)) { |
||
| 481 | foreach (self::INFO_SUMMARY[$infoSummaryType] as $settingKey => $settingValue) { |
||
| 482 | if (!empty($stream[$settingKey])) { |
||
| 483 | $summaryResult[$settingValue] = $stream[$settingKey]; |
||
| 484 | } |
||
| 485 | } |
||
| 486 | } |
||
| 487 | } |
||
| 488 | break; |
||
| 489 | // Unknown info |
||
| 490 | default: |
||
| 491 | break; |
||
| 492 | } |
||
| 493 | } |
||
| 494 | // Handle cases where the framerate is returned as XX/YY |
||
| 495 | if (!empty($summaryResult['videoFrameRate']) |
||
| 496 | && (strpos($summaryResult['videoFrameRate'], '/') !== false) |
||
| 497 | ) { |
||
| 498 | $parts = explode('/', $summaryResult['videoFrameRate']); |
||
| 499 | $summaryResult['videoFrameRate'] = (float)$parts[0] / (float)$parts[1]; |
||
| 500 | } |
||
| 501 | $result = $summaryResult; |
||
| 502 | } |
||
| 503 | } |
||
| 504 | |||
| 505 | return $result; |
||
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Get the name of a video file from a path and options |
||
| 510 | * |
||
| 511 | * @param $filePath |
||
| 512 | * @param $videoOptions |
||
| 513 | * |
||
| 514 | * @return string |
||
| 515 | */ |
||
| 516 | public function getVideoFilename($filePath, $videoOptions): string |
||
| 517 | { |
||
| 518 | $settings = Transcoder::$plugin->getSettings(); |
||
| 519 | $videoOptions = $this->coalesceOptions('defaultVideoOptions', $videoOptions); |
||
| 520 | |||
| 521 | // Get the video encoder presets to use |
||
| 522 | $videoEncoders = $settings['videoEncoders']; |
||
| 523 | $thisEncoder = $videoEncoders[$videoOptions['videoEncoder']]; |
||
| 524 | |||
| 525 | $videoOptions['fileSuffix'] = $thisEncoder['fileSuffix']; |
||
| 526 | |||
| 527 | return $this->getFilename($filePath, $videoOptions); |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Get the name of an audio file from a path and options |
||
| 532 | * |
||
| 533 | * @param $filePath |
||
| 534 | * @param $audioOptions |
||
| 535 | * |
||
| 536 | * @return string |
||
| 537 | */ |
||
| 538 | public function getAudioFilename($filePath, $audioOptions): string |
||
| 539 | { |
||
| 540 | $settings = Transcoder::$plugin->getSettings(); |
||
| 541 | $audioOptions = $this->coalesceOptions('defaultAudioOptions', $audioOptions); |
||
| 542 | |||
| 543 | // Get the video encoder presets to use |
||
| 544 | $audioEncoders = $settings['audioEncoders']; |
||
| 545 | $thisEncoder = $audioEncoders[$audioOptions['audioEncoder']]; |
||
| 546 | |||
| 547 | $audioOptions['fileSuffix'] = $thisEncoder['fileSuffix']; |
||
| 548 | |||
| 549 | return $this->getFilename($filePath, $audioOptions); |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Get the name of a gif video file from a path and options |
||
| 554 | * |
||
| 555 | * @param $filePath |
||
| 556 | * @param $gifOptions |
||
| 557 | * |
||
| 558 | * @return string |
||
| 559 | */ |
||
| 560 | public function getGifFilename($filePath, $gifOptions): string |
||
| 561 | { |
||
| 562 | $settings = Transcoder::$plugin->getSettings(); |
||
| 563 | $gifOptions = $this->coalesceOptions('defaultGifOptions', $gifOptions); |
||
| 564 | |||
| 565 | // Get the video encoder presets to use |
||
| 566 | $videoEncoders = $settings['videoEncoders']; |
||
| 567 | $thisEncoder = $videoEncoders[$gifOptions['videoEncoder']]; |
||
| 568 | |||
| 569 | $gifOptions['fileSuffix'] = $thisEncoder['fileSuffix']; |
||
| 570 | |||
| 571 | return $this->getFilename($filePath, $gifOptions); |
||
| 572 | } |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Handle generated a thumbnail for the Control Panel |
||
| 576 | * |
||
| 577 | * @param AssetThumbEvent $event |
||
| 578 | * |
||
| 579 | * @return null|false|string |
||
| 580 | */ |
||
| 581 | public function handleGetAssetThumbPath(AssetThumbEvent $event) |
||
| 582 | { |
||
| 583 | $options = [ |
||
| 584 | 'width' => $event->width, |
||
| 585 | 'height' => $event->height, |
||
| 586 | ]; |
||
| 587 | $thumbPath = $this->getVideoThumbnailUrl($event->asset, $options, $event->generate, true); |
||
| 588 | |||
| 589 | return $thumbPath; |
||
| 590 | } |
||
| 591 | |||
| 592 | // Protected Methods |
||
| 593 | // ========================================================================= |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Returns a URL to a encoded GIF file (mp4) |
||
| 597 | * |
||
| 598 | * @param string $filePath path to the original video or an Asset |
||
| 599 | * @param array $gifOptions of options for the GIF file |
||
| 600 | * |
||
| 601 | * @return string|false|null URL or path of the GIF file |
||
| 602 | */ |
||
| 603 | |||
| 604 | public function getGifUrl($filePath, $gifOptions): string |
||
| 605 | { |
||
| 606 | $result = ''; |
||
| 607 | $settings = Transcoder::$plugin->getSettings(); |
||
| 608 | $subfolder = ''; |
||
| 609 | |||
| 610 | // sub folder check |
||
| 611 | if (\is_object($filePath) && ($filePath instanceof Asset) && $settings['createSubfolders']) { |
||
| 612 | $subfolder = $filePath->folderPath; |
||
| 613 | } |
||
| 614 | |||
| 615 | $filePath = $this->getAssetPath($filePath); |
||
| 616 | |||
| 617 | if (!empty($filePath)) { |
||
| 618 | // Dest path |
||
| 619 | $destVideoPath = $settings['transcoderPaths']['gif'].$subfolder ?? $settings['transcoderPaths']['default']; |
||
| 620 | $destVideoPath = Craft::getAlias($destVideoPath); |
||
| 621 | |||
| 622 | // Options |
||
| 623 | $gifOptions = $this->coalesceOptions('defaultGifOptions', $gifOptions); |
||
| 624 | |||
| 625 | // Get the video encoder presets to use |
||
| 626 | $videoEncoders = $settings['videoEncoders']; |
||
| 627 | $thisEncoder = $videoEncoders[$gifOptions['videoEncoder']]; |
||
| 628 | $gifOptions['fileSuffix'] = $thisEncoder['fileSuffix']; |
||
| 629 | |||
| 630 | // Build the basic command for ffmpeg |
||
| 631 | $ffmpegCmd = $settings['ffmpegPath'] |
||
| 632 | .' -f gif' |
||
| 633 | .' -i '.escapeshellarg($filePath) |
||
| 634 | .' -vcodec '.$thisEncoder['videoCodec'] |
||
| 635 | .' '.$thisEncoder['videoCodecOptions']; |
||
| 636 | |||
| 637 | |||
| 638 | // Create the directory if it isn't there already |
||
| 639 | if (!is_dir($destVideoPath)) { |
||
| 640 | try { |
||
| 641 | FileHelper::createDirectory($destVideoPath); |
||
| 642 | } catch (Exception $e) { |
||
| 643 | Craft::error($e->getMessage(), __METHOD__); |
||
| 644 | } |
||
| 645 | } |
||
| 646 | |||
| 647 | $destVideoFile = $this->getFilename($filePath, $gifOptions); |
||
| 648 | |||
| 649 | // File to store the video encoding progress in |
||
| 650 | $progressFile = sys_get_temp_dir().DIRECTORY_SEPARATOR.$destVideoFile.'.progress'; |
||
| 651 | |||
| 652 | // Assemble the destination path and final ffmpeg command |
||
| 653 | $destVideoPath .= $destVideoFile; |
||
| 654 | $ffmpegCmd .= ' ' |
||
| 655 | .' -y '.escapeshellarg($destVideoPath) |
||
| 656 | .' 1> '.$progressFile.' 2>&1 & echo $!'; |
||
| 657 | |||
| 658 | // Make sure there isn't a lockfile for this video already |
||
| 659 | $lockFile = sys_get_temp_dir().DIRECTORY_SEPARATOR.$destVideoFile.'.lock'; |
||
| 660 | $oldPid = @file_get_contents($lockFile); |
||
| 661 | if ($oldPid !== false) { |
||
| 662 | exec("ps $oldPid", $ProcessState); |
||
| 663 | if (\count($ProcessState) >= 2) { |
||
| 664 | return $result; |
||
| 665 | } |
||
| 666 | // It's finished transcoding, so delete the lockfile and progress file |
||
| 667 | @unlink($lockFile); |
||
| 668 | @unlink($progressFile); |
||
| 669 | } |
||
| 670 | |||
| 671 | // If the video file already exists and hasn't been modified, return it. Otherwise, start it transcoding |
||
| 672 | if (file_exists($destVideoPath) && (@filemtime($destVideoPath) >= @filemtime($filePath))) { |
||
| 673 | $url = $settings['transcoderUrls']['gif'].$subfolder ?? $settings['transcoderUrls']['default']; |
||
| 674 | $result = Craft::getAlias($url).$destVideoFile; |
||
| 675 | } else { |
||
| 676 | // Kick off the transcoding |
||
| 677 | $pid = $this->executeShellCommand($ffmpegCmd); |
||
| 678 | Craft::info($ffmpegCmd."\nffmpeg PID: ".$pid, __METHOD__); |
||
| 679 | |||
| 680 | // Create a lockfile in tmp |
||
| 681 | file_put_contents($lockFile, $pid); |
||
| 682 | } |
||
| 683 | } |
||
| 684 | |||
| 685 | return $result; |
||
| 686 | } |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Get the name of a file from a path and options |
||
| 690 | * |
||
| 691 | * @param $filePath |
||
| 692 | * @param $options |
||
| 693 | * |
||
| 694 | * @return string |
||
| 695 | */ |
||
| 696 | protected function getFilename($filePath, $options): string |
||
| 697 | { |
||
| 698 | $settings = Transcoder::$plugin->getSettings(); |
||
| 699 | $filePath = $this->getAssetPath($filePath); |
||
| 700 | |||
| 701 | $validator = new UrlValidator(); |
||
| 702 | $error = ''; |
||
| 703 | if ($validator->validate($filePath, $error)) { |
||
| 704 | $urlParts = parse_url($filePath); |
||
| 705 | $pathParts = pathinfo($urlParts['path']); |
||
| 706 | } else { |
||
| 707 | $pathParts = pathinfo($filePath); |
||
| 708 | } |
||
| 709 | $fileName = $pathParts['filename']; |
||
| 710 | |||
| 711 | // Add our options to the file name |
||
| 712 | foreach ($options as $key => $value) { |
||
| 713 | if (!empty($value)) { |
||
| 714 | $suffix = ''; |
||
| 715 | if (!empty(self::SUFFIX_MAP[$key])) { |
||
| 716 | $suffix = self::SUFFIX_MAP[$key]; |
||
| 717 | } |
||
| 718 | if (\is_bool($value)) { |
||
| 719 | $value = $value ? $key : 'no'.$key; |
||
| 720 | } |
||
| 721 | if (!\in_array($key, self::EXCLUDE_PARAMS, true)) { |
||
| 722 | $fileName .= '_'.$value.$suffix; |
||
| 723 | } |
||
| 724 | } |
||
| 725 | } |
||
| 726 | // See if we should use a hash instead |
||
| 727 | if ($settings['useHashedNames']) { |
||
| 728 | $fileName = $pathParts['filename'].md5($fileName); |
||
| 729 | } |
||
| 730 | $fileName .= $options['fileSuffix']; |
||
| 731 | |||
| 732 | return $fileName; |
||
| 733 | } |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Extract a file system path if $filePath is an Asset object |
||
| 737 | * |
||
| 738 | * @param $filePath |
||
| 739 | * |
||
| 740 | * @return string |
||
| 741 | */ |
||
| 742 | protected function getAssetPath($filePath): string |
||
| 743 | { |
||
| 744 | // If we're passed an Asset, extract the path from it |
||
| 745 | if (\is_object($filePath) && ($filePath instanceof Asset)) { |
||
| 746 | /** @var Asset $asset */ |
||
| 747 | $asset = $filePath; |
||
| 748 | $assetVolume = null; |
||
| 749 | try { |
||
| 750 | $assetVolume = $asset->getVolume(); |
||
| 751 | } catch (InvalidConfigException $e) { |
||
| 752 | Craft::error($e->getMessage(), __METHOD__); |
||
| 753 | } |
||
| 754 | |||
| 755 | if ($assetVolume) { |
||
| 756 | // If it's local, get a path to the file |
||
| 757 | if ($assetVolume instanceof Local) { |
||
| 758 | $sourcePath = rtrim($assetVolume->path, DIRECTORY_SEPARATOR); |
||
| 759 | $sourcePath .= '' === $sourcePath ? '' : DIRECTORY_SEPARATOR; |
||
| 760 | $folderPath = ''; |
||
| 761 | try { |
||
| 762 | $folderPath = rtrim($asset->getFolder()->path, DIRECTORY_SEPARATOR); |
||
| 763 | } catch (InvalidConfigException $e) { |
||
| 764 | Craft::error($e->getMessage(), __METHOD__); |
||
| 765 | } |
||
| 766 | $folderPath .= '' === $folderPath ? '' : DIRECTORY_SEPARATOR; |
||
| 767 | |||
| 768 | $filePath = $sourcePath.$folderPath.$asset->filename; |
||
| 769 | } else { |
||
| 770 | // Otherwise, get a URL |
||
| 771 | $filePath = $asset->getUrl(); |
||
| 772 | } |
||
| 773 | } |
||
| 774 | } |
||
| 775 | |||
| 776 | $filePath = Craft::getAlias($filePath); |
||
| 777 | |||
| 778 | // Make sure that $filePath is either an existing file, or a valid URL |
||
| 779 | if (!file_exists($filePath)) { |
||
| 780 | $validator = new UrlValidator(); |
||
| 781 | $error = ''; |
||
| 782 | if (!$validator->validate($filePath, $error)) { |
||
| 783 | Craft::error($error, __METHOD__); |
||
| 784 | $filePath = ''; |
||
| 785 | } |
||
| 786 | } |
||
| 787 | |||
| 788 | return $filePath; |
||
| 789 | } |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Set the width & height if desired |
||
| 793 | * |
||
| 794 | * @param $options |
||
| 795 | * @param $ffmpegCmd |
||
| 796 | * |
||
| 797 | * @return string |
||
| 798 | */ |
||
| 799 | protected function addScalingFfmpegArgs($options, $ffmpegCmd): string |
||
| 800 | { |
||
| 801 | if (!empty($options['width']) && !empty($options['height'])) { |
||
| 802 | // Handle "none", "crop", and "letterbox" aspectRatios |
||
| 803 | $aspectRatio = ''; |
||
| 804 | if (!empty($options['aspectRatio'])) { |
||
| 805 | switch ($options['aspectRatio']) { |
||
| 806 | // Scale to the appropriate aspect ratio, padding |
||
| 807 | case 'letterbox': |
||
| 808 | $letterboxColor = ''; |
||
| 809 | if (!empty($options['letterboxColor'])) { |
||
| 810 | $letterboxColor = ':color='.$options['letterboxColor']; |
||
| 811 | } |
||
| 812 | $aspectRatio = ':force_original_aspect_ratio=decrease' |
||
| 813 | .',pad='.$options['width'].':'.$options['height'].':(ow-iw)/2:(oh-ih)/2' |
||
| 814 | .$letterboxColor; |
||
| 815 | break; |
||
| 816 | // Scale to the appropriate aspect ratio, cropping |
||
| 817 | case 'crop': |
||
| 818 | $aspectRatio = ':force_original_aspect_ratio=increase' |
||
| 819 | .',crop='.$options['width'].':'.$options['height']; |
||
| 820 | break; |
||
| 821 | // No aspect ratio scaling at all |
||
| 822 | default: |
||
| 823 | $aspectRatio = ':force_original_aspect_ratio=disable'; |
||
| 824 | $options['aspectRatio'] = 'none'; |
||
| 825 | break; |
||
| 826 | } |
||
| 827 | } |
||
| 828 | $sharpen = ''; |
||
| 829 | if (!empty($options['sharpen']) && ($options['sharpen'] !== false)) { |
||
| 830 | $sharpen = ',unsharp=5:5:1.0:5:5:0.0'; |
||
| 831 | } |
||
| 832 | $ffmpegCmd .= ' -vf "scale=' |
||
| 833 | .$options['width'].':'.$options['height'] |
||
| 834 | .$aspectRatio |
||
| 835 | .$sharpen |
||
| 836 | .'"'; |
||
| 837 | } |
||
| 838 | |||
| 839 | return $ffmpegCmd; |
||
| 840 | } |
||
| 841 | |||
| 842 | // Protected Methods |
||
| 843 | // ========================================================================= |
||
| 844 | |||
| 845 | /** |
||
| 846 | * Combine the options arrays |
||
| 847 | * |
||
| 848 | * @param $defaultName |
||
| 849 | * @param $options |
||
| 850 | * |
||
| 851 | * @return array |
||
| 852 | */ |
||
| 853 | protected function coalesceOptions($defaultName, $options): array |
||
| 854 | { |
||
| 855 | // Default options |
||
| 856 | $settings = Transcoder::$plugin->getSettings(); |
||
| 857 | $defaultOptions = $settings[$defaultName]; |
||
| 858 | |||
| 859 | // Coalesce the passed in $options with the $defaultOptions |
||
| 860 | $options = array_merge($defaultOptions, $options); |
||
| 861 | |||
| 862 | return $options; |
||
| 863 | } |
||
| 864 | |||
| 865 | /** |
||
| 866 | * Execute a shell command |
||
| 867 | * |
||
| 868 | * @param string $command |
||
| 869 | * |
||
| 870 | * @return string |
||
| 871 | */ |
||
| 872 | protected function executeShellCommand(string $command): string |
||
| 891 | } |
||
| 892 | } |
||
| 893 |