Passed
Branch v1 (0c7460)
by Andrew
06:14
created

Settings::__construct()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 5
nop 1
dl 0
loc 15
rs 10
c 0
b 0
f 0
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
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
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
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 4
Loading history...
20
 * @package   Transcode
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
21
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 3 spaces but found 5
Loading history...
22
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
23
class Settings extends Model
24
{
25
26
    // Static Methods
27
    // =========================================================================
28
29
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
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
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
233
     * @inheritdoc
234
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
235
    public function init()
236
    {
237
        parent::init();
238
    }
239
240
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
241
     * @inheritdoc
242
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
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