nystudio107 /
craft-transcoder
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Transcoder plugin for Craft CMS |
||
| 4 | * |
||
| 5 | * Transcode videos to various formats, and provide thumbnails of the video |
||
| 6 | * |
||
| 7 | * @link https://nystudio107.com |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 8 | * @copyright Copyright (c) 2017 nystudio107 |
||
|
0 ignored issues
–
show
|
|||
| 9 | */ |
||
|
0 ignored issues
–
show
|
|||
| 10 | |||
| 11 | namespace nystudio107\transcoder\models; |
||
| 12 | |||
| 13 | use craft\base\Model; |
||
| 14 | use craft\validators\ArrayValidator; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Transcoder Settings model |
||
| 18 | * |
||
| 19 | * @author nystudio107 |
||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 20 | * @package Transcode |
||
|
0 ignored issues
–
show
|
|||
| 21 | * @since 1.0.0 |
||
|
0 ignored issues
–
show
|
|||
| 22 | */ |
||
|
0 ignored issues
–
show
|
|||
| 23 | class Settings extends Model |
||
| 24 | { |
||
| 25 | // Public Properties |
||
| 26 | // ========================================================================= |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The path to the ffmpeg binary |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | |||
| 34 | public string $ffmpegPath = '/usr/bin/ffmpeg'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The path to the ffprobe binary |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | public string $ffprobePath = '/usr/bin/ffprobe'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The options to use for ffprobe |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | public string $ffprobeOptions = '-v quiet -print_format json -show_format -show_streams'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The path where the transcoded videos are stored; must have a trailing / |
||
| 52 | * Yii2 aliases are supported here |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | public array $transcoderPaths = [ |
||
| 57 | 'default' => '@webroot/transcoder/', |
||
| 58 | 'video' => '@webroot/transcoder/', |
||
| 59 | 'audio' => '@webroot/transcoder/', |
||
| 60 | 'thumbnail' => '@webroot/transcoder/', |
||
| 61 | 'gif' => '@webroot/transcoder/', |
||
| 62 | ]; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The URL where the transcoded videos are stored; must have a trailing / |
||
| 66 | * Yii2 aliases are supported here |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | public array $transcoderUrls = [ |
||
| 71 | 'default' => '@web/transcoder/', |
||
| 72 | 'video' => '@web/transcoder/', |
||
| 73 | 'audio' => '@web/transcoder/', |
||
| 74 | 'thumbnail' => '@web/transcoder/', |
||
| 75 | 'gif' => '@web/transcoder/', |
||
| 76 | ]; |
||
| 77 | |||
| 78 | /** |
||
|
0 ignored issues
–
show
|
|||
| 79 | * @var bool Determines whether the download file endpoint should be enabled for anonymous frontend access |
||
| 80 | */ |
||
| 81 | public bool $enableDownloadFileEndpoint = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Use a md5 hash for the filenames instead of parameterized naming |
||
| 85 | * |
||
| 86 | * @var bool |
||
| 87 | */ |
||
| 88 | public bool $useHashedNames = false; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * if a upload location has a subfolder defined, add this to the transcoder |
||
|
0 ignored issues
–
show
|
|||
| 92 | * paths too |
||
| 93 | * |
||
| 94 | * @var bool |
||
| 95 | */ |
||
| 96 | public bool $createSubfolders = true; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * clear caches when somebody clears all caches from the CP? |
||
|
0 ignored issues
–
show
|
|||
| 100 | * |
||
| 101 | * @var bool |
||
| 102 | */ |
||
| 103 | public bool $clearCaches = false; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Preset video encoders |
||
| 107 | * |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | public array $videoEncoders = [ |
||
| 111 | 'h264' => [ |
||
| 112 | 'fileSuffix' => '.mp4', |
||
| 113 | 'fileFormat' => 'mp4', |
||
| 114 | 'videoCodec' => 'libx264', |
||
| 115 | 'videoCodecOptions' => '-vprofile high -preset slow -crf 22', |
||
| 116 | 'audioCodec' => 'libfdk_aac', |
||
| 117 | 'audioCodecOptions' => '-async 1000', |
||
| 118 | 'threads' => '0', |
||
| 119 | ], |
||
| 120 | 'webm' => [ |
||
| 121 | 'fileSuffix' => '.webm', |
||
| 122 | 'fileFormat' => 'webm', |
||
| 123 | 'videoCodec' => 'libvpx', |
||
| 124 | 'videoCodecOptions' => '-quality good -cpu-used 0', |
||
| 125 | 'audioCodec' => 'libvorbis', |
||
| 126 | 'audioCodecOptions' => '-async 1000', |
||
| 127 | 'threads' => '0', |
||
| 128 | ], |
||
| 129 | 'gif' => [ |
||
| 130 | 'fileSuffix' => '.mp4', |
||
| 131 | 'fileFormat' => 'mp4', |
||
| 132 | 'videoCodec' => 'libx264', |
||
| 133 | 'videoCodecOptions' => '-pix_fmt yuv420p -movflags +faststart -filter:v crop=\'floor(in_w/2)*2:floor(in_h/2)*2\' ', |
||
| 134 | 'threads' => '0', |
||
| 135 | ], |
||
| 136 | ]; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Preset audio encoders |
||
| 140 | * |
||
| 141 | * @var array |
||
| 142 | */ |
||
| 143 | public array $audioEncoders = [ |
||
| 144 | 'mp3' => [ |
||
| 145 | 'fileSuffix' => '.mp3', |
||
| 146 | 'fileFormat' => 'mp3', |
||
| 147 | 'audioCodec' => 'libmp3lame', |
||
| 148 | 'audioCodecOptions' => '', |
||
| 149 | 'threads' => '0', |
||
| 150 | ], |
||
| 151 | 'aac' => [ |
||
| 152 | 'fileSuffix' => '.m4a', |
||
| 153 | 'fileFormat' => 'aac', |
||
| 154 | 'audioCodec' => 'libfdk_aac', |
||
| 155 | 'audioCodecOptions' => '', |
||
| 156 | 'threads' => '0', |
||
| 157 | |||
| 158 | ], |
||
| 159 | 'ogg' => [ |
||
| 160 | 'fileSuffix' => '.ogg', |
||
| 161 | 'fileFormat' => 'ogg', |
||
| 162 | 'audioCodec' => 'libvorbis', |
||
| 163 | 'audioCodecOptions' => '', |
||
| 164 | 'threads' => '0', |
||
| 165 | ], |
||
| 166 | ]; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Default options for encoded videos |
||
| 170 | * |
||
| 171 | * @var array |
||
| 172 | */ |
||
| 173 | public array $defaultVideoOptions = [ |
||
| 174 | // Video settings |
||
| 175 | 'videoEncoder' => 'h264', |
||
| 176 | 'videoBitRate' => '800k', |
||
| 177 | 'videoFrameRate' => 15, |
||
| 178 | // Audio settings |
||
| 179 | 'audioBitRate' => '', |
||
| 180 | 'audioSampleRate' => '', |
||
| 181 | 'audioChannels' => '', |
||
| 182 | // Spatial settings |
||
| 183 | 'width' => '', |
||
| 184 | 'height' => '', |
||
| 185 | 'sharpen' => true, |
||
| 186 | // Can be 'none', 'crop', or 'letterbox' |
||
| 187 | 'aspectRatio' => 'letterbox', |
||
| 188 | 'letterboxColor' => '', |
||
| 189 | ]; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Default options for video thumbnails |
||
| 193 | * |
||
| 194 | * @var array |
||
| 195 | */ |
||
| 196 | public array $defaultThumbnailOptions = [ |
||
| 197 | 'fileSuffix' => '.jpg', |
||
| 198 | 'timeInSecs' => 10, |
||
| 199 | 'width' => '', |
||
| 200 | 'height' => '', |
||
| 201 | 'sharpen' => true, |
||
| 202 | // Can be 'none', 'crop', or 'letterbox' |
||
| 203 | 'aspectRatio' => 'letterbox', |
||
| 204 | 'letterboxColor' => '', |
||
| 205 | ]; |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Default options for encoded videos |
||
| 209 | * |
||
| 210 | * @var array |
||
| 211 | */ |
||
| 212 | public array $defaultAudioOptions = [ |
||
| 213 | 'audioEncoder' => 'mp3', |
||
| 214 | 'audioBitRate' => '128k', |
||
| 215 | 'audioSampleRate' => '44100', |
||
| 216 | 'audioChannels' => '2', |
||
| 217 | 'synchronous' => false, |
||
| 218 | 'stripMetadata' => false, |
||
| 219 | ]; |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Default options for encoded GIF |
||
| 223 | * |
||
| 224 | * @var array |
||
| 225 | */ |
||
| 226 | public array $defaultGifOptions = [ |
||
| 227 | 'videoEncoder' => 'gif', |
||
| 228 | 'fileSuffix' => '', |
||
| 229 | 'fileFormat' => '', |
||
| 230 | 'videoCodec' => '', |
||
| 231 | 'videoCodecOptions' => '', |
||
| 232 | ]; |
||
| 233 | |||
| 234 | /** |
||
|
0 ignored issues
–
show
|
|||
| 235 | * @inheritdoc |
||
| 236 | */ |
||
| 237 | public function __construct(array $config = []) |
||
| 238 | { |
||
| 239 | // Unset any deprecated properties |
||
| 240 | if (!empty($config)) { |
||
| 241 | // If the old properties are set, remap them to the default |
||
| 242 | if (isset($config['transcoderPath'])) { |
||
| 243 | $config['transcoderPaths']['default'] = $config['transcoderPath']; |
||
| 244 | unset($config['transcoderPath']); |
||
| 245 | } |
||
| 246 | if (isset($config['transcoderUrl'])) { |
||
| 247 | $config['$transcoderUrls']['default'] = $config['transcoderUrl']; |
||
| 248 | unset($config['transcoderUrl']); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | parent::__construct($config); |
||
| 252 | } |
||
| 253 | |||
| 254 | // Public Methods |
||
| 255 | // ========================================================================= |
||
| 256 | |||
| 257 | /** |
||
|
0 ignored issues
–
show
|
|||
| 258 | * @inheritdoc |
||
| 259 | */ |
||
|
0 ignored issues
–
show
|
|||
| 260 | public function rules(): array |
||
| 261 | { |
||
| 262 | return [ |
||
| 263 | ['ffmpegPath', 'string'], |
||
| 264 | ['ffmpegPath', 'required'], |
||
| 265 | ['ffprobePath', 'string'], |
||
| 266 | ['ffprobePath', 'required'], |
||
| 267 | ['ffprobeOptions', 'string'], |
||
| 268 | ['ffprobeOptions', 'safe'], |
||
| 269 | ['transcoderPath', 'string'], |
||
| 270 | ['transcoderPath', 'required'], |
||
| 271 | ['transcoderPaths', ArrayValidator::class], |
||
| 272 | ['transcoderPaths', 'required'], |
||
| 273 | ['transcoderUrls', ArrayValidator::class], |
||
| 274 | ['enableDownloadFileEndpoint', 'boolean'], |
||
| 275 | ['useHashedNames', 'boolean'], |
||
| 276 | ['createSubfolders', 'boolean'], |
||
| 277 | ['clearCaches', 'boolean'], |
||
| 278 | ['videoEncoders', 'required'], |
||
| 279 | ['audioEncoders', 'required'], |
||
| 280 | ['defaultVideoOptions', 'required'], |
||
| 281 | ['defaultThumbnailOptions', 'required'], |
||
| 282 | ['defaultAudioOptions', 'required'], |
||
| 283 | ]; |
||
| 284 | } |
||
| 285 | } |
||
| 286 |