Passed
Push — develop ( 0835a7...8e80bc )
by Andrew
02:53
created

Settings   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 110
dl 0
loc 237
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

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