Skip to content

Commit 3c3f703

Browse files
committed
- added support for multiple encoding paths based on type of media
- added GIF encoding (GIF to MP4) - extra checks at reading ffmpeg log for GIF encoding (no duration/progress)
1 parent 1e580dc commit 3c3f703

File tree

6 files changed

+258
-42
lines changed

6 files changed

+258
-42
lines changed

src/Transcoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function (RegisterCacheOptionsEvent $event) {
9898
$event->options[] = [
9999
'key' => 'transcoder',
100100
'label' => Craft::t('transcoder', 'Transcoder caches'),
101-
'action' => Transcoder::$plugin->getSettings()->transcoderPath,
101+
'action' => Transcoder::$plugin->getSettings()->transcoderPaths['default'],
102102
];
103103
}
104104
);

src/config.php

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Transcoder plugin for Craft CMS 3.x
45
*
@@ -31,16 +32,28 @@
3132
'ffprobePath' => '/usr/bin/ffprobe',
3233

3334
// The options to use for ffprobe
34-
'ffprobeOptions' => '-v quiet -print_format json -show_format -show_streams',
35+
'ffprobeOptions' => '-v quiet -print_format json -show_format -show_streams',
3536

3637
// The path where the transcoded videos are stored; must have a trailing /
37-
// Yii2 aliases are supported here
38-
'transcoderPath' => '@webroot/transcoder/',
39-
38+
// Yii2 aliases are supported here
39+
'transcoderPaths' => [
40+
'default' => '@webroot/transcoder/',
41+
'video' => '@webroot/transcoder/video/',
42+
'audio' => '@webroot/transcoder/audio/',
43+
'thumbnail' => '@webroot/transcoder/thumbnail/',
44+
'gif' => '@webroot/transcoder/gif/'
45+
],
46+
4047
// The URL where the transcoded videos are stored; must have a trailing /
4148
// Yii2 aliases are supported here
42-
'transcoderUrl' => '@web/transcoder/',
43-
49+
'transcoderUrls' => [
50+
'default' => '@web/transcoder/',
51+
'video' => '@web/transcoder/video/',
52+
'audio' => '@web/transcoder/audio/',
53+
'thumbnail' => '@web/transcoder/thumbnail/',
54+
'gif' => '@web/transcoder/gif/'
55+
],
56+
4457
// Use a md5 hash for the filenames instead of parameterized naming
4558
'useHashedNames' => false,
4659

@@ -62,6 +75,12 @@
6275
'audioCodec' => 'libvorbis',
6376
'audioCodecOptions' => '-async 1000',
6477
],
78+
'gif' => [
79+
'fileSuffix' => '.mp4',
80+
'fileFormat' => 'mp4',
81+
'videoCodec' => 'libx264',
82+
'videoCodecOptions' => '-pix_fmt yuv420p -movflags +faststart -filter:v crop=\'floor(in_w/2)*2:floor(in_h/2)*2\' ',
83+
]
6584
],
6685

6786
// Preset audio encoders
@@ -126,4 +145,12 @@
126145
'audioChannels' => '2',
127146
],
128147

148+
// Default options for Gif encoding
149+
'defaultGifOptions' => [
150+
'videoEncoder' => 'gif',
151+
'fileSuffix' => '.mp4',
152+
'fileFormat' => 'gif',
153+
'videoCodec' => 'libx264',
154+
'videoCodecOptions' => '-pix_fmt yuv420p -movflags +faststart -filter:v crop=\'floor(in_w/2)*2:floor(in_h/2)*2\' ',
155+
],
129156
];

src/controllers/DefaultController.php

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,26 @@ public function actionProgress($filename)
7979
if (file_exists($progressFile)) {
8080
$content = @file_get_contents($progressFile);
8181
if ($content) {
82+
8283
// get duration of source
8384
preg_match("/Duration: (.*?), start:/", $content, $matches);
84-
$rawDuration = $matches[1];
85-
86-
// rawDuration is in 00:00:00.00 format. This converts it to seconds.
87-
$ar = array_reverse(explode(":", $rawDuration));
88-
$duration = floatval($ar[0]);
89-
if (!empty($ar[1])) {
90-
$duration += intval($ar[1]) * 60;
91-
}
92-
if (!empty($ar[2])) {
93-
$duration += intval($ar[2]) * 60 * 60;
94-
}
85+
86+
if(count($matches) > 0) {
87+
88+
$rawDuration = $matches[1];
89+
90+
// rawDuration is in 00:00:00.00 format. This converts it to seconds.
91+
$ar = array_reverse(explode(":", $rawDuration));
92+
$duration = floatval($ar[0]);
93+
if (!empty($ar[1])) {
94+
$duration += intval($ar[1]) * 60;
95+
}
96+
if (!empty($ar[2])) {
97+
$duration += intval($ar[2]) * 60 * 60;
98+
}
99+
} else {
100+
$duration = 'unknown'; // with GIF as input, duration is unknown
101+
}
95102

96103
// Get the time in the file that is already encoded
97104
preg_match_all("/time=(.*?) bitrate/", $content, $matches);
@@ -113,16 +120,29 @@ public function actionProgress($filename)
113120
}
114121

115122
//calculate the progress
116-
$progress = round(($time / $duration) * 100);
117-
118-
if ($progress < 100) {
119-
$result = [
120-
'filename' => $filename,
121-
'duration' => $duration,
122-
'time' => $time,
123-
'progress' => $progress,
124-
];
123+
if($duration != 'unknown') {
124+
$progress = round(($time / $duration) * 100);
125+
} else {
126+
$progress = 'unknown';
125127
}
128+
129+
// return results
130+
if($progress != "unknown" && $progress < 100) {
131+
$result = [
132+
'filename' => $filename,
133+
'duration' => $duration,
134+
'time' => $time,
135+
'progress' => $progress,
136+
];
137+
} elseif($progress == "unknown") {
138+
$result = [
139+
'filename' => $filename,
140+
'duration' => 'unknown',
141+
'time' => $time,
142+
'progress' => 'unknown',
143+
'message' => 'encoding GIF, can\'t determine duration'
144+
];
145+
}
126146
}
127147
}
128148

src/models/Settings.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ class Settings extends Model
5151
*
5252
* @var string
5353
*/
54-
public $transcoderPath = '@webroot/transcoder/';
55-
54+
public $transcoderPaths = [];
55+
5656
/**
5757
* The URL where the transcoded videos are stored; must have a trailing /
5858
* Yii2 aliases are supported here
5959
*
6060
* @var string
6161
*/
62-
public $transcoderUrl = '@web/transcoder/';
62+
public $transcoderUrls = [];
6363

6464
/**
6565
* Use a md5 hash for the filenames instead of parameterized naming
@@ -90,6 +90,12 @@ class Settings extends Model
9090
'audioCodec' => 'libvorbis',
9191
'audioCodecOptions' => '-async 1000',
9292
],
93+
'gif' => [
94+
'fileSuffix' => '.mp4',
95+
'fileFormat' => 'mp4',
96+
'videoCodec' => 'libx264',
97+
'videoCodecOptions' => '-pix_fmt yuv420p -movflags +faststart -filter:v crop=\'floor(in_w/2)*2:floor(in_h/2)*2\' ',
98+
]
9399
];
94100

95101
/**
@@ -170,7 +176,19 @@ class Settings extends Model
170176
'audioChannels' => '2',
171177
];
172178

173-
179+
/**
180+
* Default options for encoded GIF
181+
*
182+
* @var array
183+
*/
184+
public $defaultGifOptions = [
185+
'videoEncoder' => 'gif',
186+
'fileSuffix' => '',
187+
'fileFormat' => '',
188+
'videoCodec' => '',
189+
'videoCodecOptions' => '',
190+
];
191+
174192
// Public Methods
175193
// =========================================================================
176194

@@ -199,8 +217,10 @@ public function rules()
199217
['ffprobeOptions', 'safe'],
200218
['transcoderPath', 'string'],
201219
['transcoderPath', 'required'],
202-
['transcoderUrl', 'string'],
203-
['transcoderUrl', 'required'],
220+
['transcoderPaths', 'array'],
221+
['transcoderPaths', 'required'],
222+
['transcoderUrls', 'array'],
223+
['transcoderUrls', 'required'],
204224
['useHashedNames', 'boolean'],
205225
['useHashedNames', 'default', 'value' => false],
206226
['videoEncoders', 'required'],

0 commit comments

Comments
 (0)