Issues (306)

src/models/Settings.php (25 issues)

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
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
Missing @license tag in file comment
Loading history...
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
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
20
 * @package   Transcode
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
21
 * @since     1.0.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
22
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
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 $ffmpegPath = '/usr/bin/ffmpeg';
35
36
    /**
37
     * The path to the ffprobe binary
38
     *
39
     * @var string
40
     */
41
    public $ffprobePath = '/usr/bin/ffprobe';
42
43
    /**
44
     * The options to use for ffprobe
45
     *
46
     * @var string
47
     */
48
    public $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 $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 $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
Missing short description in doc comment
Loading history...
79
     * @var bool Determines whether the download file endpoint should be enabled for anonymous frontend access
80
     */
81
    public $enableDownloadFileEndpoint = false;
82
83
    /**
84
     * Use a md5 hash for the filenames instead of parameterized naming
85
     *
86
     * @var bool
87
     */
88
    public $useHashedNames = false;
89
90
    /**
91
     * if a upload location has a subfolder defined, add this to the transcoder
0 ignored issues
show
Doc comment short description must start with a capital letter
Loading history...
92
     * paths too
93
     *
94
     * @var bool
95
     */
96
    public $createSubfolders = true;
97
98
    /**
99
     * clear caches when somebody clears all caches from the CP?
0 ignored issues
show
Doc comment short description must start with a capital letter
Loading history...
100
     *
101
     * @var bool
102
     */
103
    public $clearCaches = false;
104
105
    /**
106
     * Preset video encoders
107
     *
108
     * @var array
109
     */
110
    public $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 $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 $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 $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 $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 $defaultGifOptions = [
227
        'videoEncoder' => 'gif',
228
        'fileSuffix' => '',
229
        'fileFormat' => '',
230
        'videoCodec' => '',
231
        'videoCodecOptions' => '',
232
    ];
233
234
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
Parameter $config should have a doc-comment as per coding-style.
Loading history...
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
Missing short description in doc comment
Loading history...
258
     * @inheritdoc
259
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
260
    public function init()
261
    {
262
        parent::init();
263
    }
264
265
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
266
     * @inheritdoc
267
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
268
    public function rules()
269
    {
270
        return [
271
            ['ffmpegPath', 'string'],
272
            ['ffmpegPath', 'required'],
273
            ['ffprobePath', 'string'],
274
            ['ffprobePath', 'required'],
275
            ['ffprobeOptions', 'string'],
276
            ['ffprobeOptions', 'safe'],
277
            ['transcoderPath', 'string'],
278
            ['transcoderPath', 'required'],
279
            ['transcoderPaths', ArrayValidator::class],
280
            ['transcoderPaths', 'required'],
281
            ['transcoderUrls', ArrayValidator::class],
282
            ['enableDownloadFileEndpoint', 'boolean'],
283
            ['useHashedNames', 'boolean'],
284
            ['createSubfolders', 'boolean'],
285
            ['clearCaches', 'boolean'],
286
            ['videoEncoders', 'required'],
287
            ['audioEncoders', 'required'],
288
            ['defaultVideoOptions', 'required'],
289
            ['defaultThumbnailOptions', 'required'],
290
            ['defaultAudioOptions', 'required'],
291
        ];
292
    }
293
}
294