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