Skip to content

Commit ca2a292

Browse files
committed
### Added
- Added `height` and `width` options for resizing the videos - Added an `aspectRatio` option to control how aspect ratio scaling is done - Added an `letterboxColor` option - The `ffmpeg` progress for a particular video is now written out to a `.progress` file - Added the `getFileInfo` variable to extract information from a video/audio file
1 parent 0ce7573 commit ca2a292

File tree

4 files changed

+101
-15
lines changed

4 files changed

+101
-15
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Transcoder Changelog
22

3+
## 1.0.1 - 2017.03.06
4+
### Added
5+
- Added `height` and `width` options for resizing the videos
6+
- Added an `aspectRatio` option to control how aspect ratio scaling is done
7+
- Added an `letterboxColor` option
8+
- The `ffmpeg` progress for a particular video is now written out to a `.progress` file
9+
- Added the `getFileInfo` variable to extract information from a video/audio file
10+
11+
### Fixed
12+
- Fixed some issues with the lockfile naming
13+
314
## 1.0.0 - 2017.03.05
415
### Added
516
- Initial release

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "nystudio107/craft3-transcoder",
33
"description": "Transcode videos to various formats, and provide thumbnails of the video",
44
"type": "craft-plugin",
5-
"version": "1.0.0",
5+
"version": "1.0.1",
66
"keywords": [
77
"craft",
88
"cms",

src/config.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,23 @@
4444
"defaultVideoOptions" => [
4545
"bitRate" => "800k",
4646
"frameRate" => 15,
47+
"width" => "",
48+
"height" => "",
49+
"sharpen" => true,
50+
// Can be "none", "crop", or "letterbox"
51+
"aspectRatio" => "letterbox",
52+
"letterboxColor" => "0x000000",
4753
],
4854

4955
// Default options for video thumbnails
5056
"defaultThumbnailOptions" => [
57+
"timeInSecs" => 10,
5158
"width" => "",
5259
"height" => "",
53-
"timeInSecs" => 10,
60+
"sharpen" => true,
61+
// Can be "none", "crop", or "letterbox"
62+
"aspectRatio" => "letterbox",
63+
"letterboxColor" => "0x000000",
5464
],
5565

5666
];

src/services/Transcoder.php

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use Craft;
1414
use craft\base\Component;
15+
use craft\console\Request;
1516
use craft\elements\Asset;
1617
use craft\volumes\Local;
1718

@@ -32,7 +33,7 @@ class Transcoder extends Component
3233
* time it will create it). By default, the video is never resized, and the
3334
* format is always .mp4
3435
*
35-
* @param $filePath path to the original video -OR- an AssetFileModel
36+
* @param $filePath path to the original video -OR- an Asset
3637
* @param $videoOptions array of options for the video
3738
*
3839
* @return string URL of the transcoded video or ""
@@ -77,21 +78,31 @@ public function getVideoUrl($filePath, $videoOptions): string
7778
$destVideoFile .= '_'.$videoOptions['bitRate'].'bps';
7879
}
7980

81+
// Adjust the scaling if desired
82+
list($destVideoFile, $ffmpegCmd) = $this->addScalingParams(
83+
$videoOptions,
84+
$destVideoFile,
85+
$ffmpegCmd
86+
);
87+
8088
// Create the directory if it isn't there already
8189
if (!file_exists($destVideoPath)) {
8290
mkdir($destVideoPath);
8391
}
8492

93+
// File to store the video encoding progress in
94+
$progressFile = sys_get_temp_dir().DIRECTORY_SEPARATOR.$destVideoFile.".progress";
95+
8596
// Assemble the destination path and final ffmpeg command
8697
$destVideoFile .= '.mp4';
8798
$destVideoPath = $destVideoPath.$destVideoFile;
88-
$ffmpegCmd .= ' -f mp4 -y '.escapeshellarg($destVideoPath).' >/dev/null 2>/dev/null & echo $!';
99+
$ffmpegCmd .= ' -f mp4 -y '.escapeshellarg($destVideoPath).' 1> '.$progressFile.' 2>&1 & echo $!';
89100

90101
// Make sure there isn't a lockfile for this video already
91-
$lockFile = sys_get_temp_dir().'/'.$destVideoFile."lock";
92-
$oldpid = @file_get_contents($lockFile);
93-
if ($oldpid !== false) {
94-
exec("ps $oldpid", $ProcessState);
102+
$lockFile = sys_get_temp_dir().DIRECTORY_SEPARATOR.$destVideoFile.".lock";
103+
$oldPid = @file_get_contents($lockFile);
104+
if ($oldPid !== false) {
105+
exec("ps $oldPid", $ProcessState);
95106
if (count($ProcessState) >= 2) {
96107
return $result;
97108
}
@@ -145,13 +156,12 @@ public function getVideoThumbnailUrl($filePath, $thumbnailOptions): string
145156
.' -vcodec mjpeg'
146157
.' -vframes 1';
147158

148-
// Set the width & height if desired
149-
if (!empty($thumbnailOptions['width']) && !empty($thumbnailOptions['height'])) {
150-
$ffmpegCmd .= ' -vf "scale='
151-
.$thumbnailOptions['width'].':'.$thumbnailOptions['height']
152-
.', unsharp=5:5:1.0:5:5:0.0"';
153-
$destThumbnailFile .= '_'.$thumbnailOptions['width'].'x'.$thumbnailOptions['height'];
154-
}
159+
// Adjust the scaling if desired
160+
list($destThumbnailFile, $ffmpegCmd) = $this->addScalingParams(
161+
$thumbnailOptions,
162+
$destThumbnailFile,
163+
$ffmpegCmd
164+
);
155165

156166
// Set the timecode to get the thumbnail from if desired
157167
if (!empty($thumbnailOptions['timeInSecs'])) {
@@ -244,4 +254,59 @@ protected function getAssetPath($filePath): string
244254

245255
return $filePath;
246256
}
257+
258+
/**
259+
* Set the width & height if desired
260+
*
261+
* @param $options
262+
* @param $destFile
263+
* @param $ffmpegCmd
264+
*
265+
* @return array
266+
*/
267+
protected function addScalingParams($options, $destFile, $ffmpegCmd): array
268+
{
269+
if (!empty($options['width']) && !empty($options['height'])) {
270+
// Handle "none", "crop", and "letterbox" aspectRatios
271+
if (!empty($options['aspectRatio'])) {
272+
switch ($options['aspectRatio']) {
273+
// Scale to the appropriate aspect ratio, padding
274+
case "letterbox":
275+
$letterboxColor = "";
276+
if (!empty($options['letterboxColor'])) {
277+
$letterboxColor = ":color=".$options['letterboxColor'];
278+
}
279+
$aspectRatio = ':force_original_aspect_ratio=decrease'
280+
.',pad='.$options['width'].':'.$options['height'].':(ow-iw)/2:(oh-ih)/2'
281+
.$letterboxColor;
282+
break;
283+
// Scale to the appropriate aspect ratio, cropping
284+
case "crop":
285+
$aspectRatio = ':force_original_aspect_ratio=increase'
286+
.',crop='.$options['width'].':'.$options['height'];
287+
break;
288+
// No aspect ratio scaling at all
289+
default:
290+
$aspectRatio = ':force_original_aspect_ratio=disable';
291+
$options['aspectRatio'] = "none";
292+
break;
293+
}
294+
$destFile .= '_'.$options['aspectRatio'];
295+
}
296+
$sharpen = "";
297+
if (!empty($options['sharpen']) && ($options['sharpen'] !== false)) {
298+
$sharpen = ',unsharp=5:5:1.0:5:5:0.0';
299+
}
300+
$ffmpegCmd .= ' -vf "scale='
301+
.$options['width'].':'.$options['height']
302+
.$aspectRatio
303+
.$sharpen
304+
.'"';
305+
$destFile .= '_'.$options['width'].'x'.$options['height'];
306+
307+
return [$destFile, $ffmpegCmd];
308+
}
309+
310+
return [$destFile, $ffmpegCmd];
311+
}
247312
}

0 commit comments

Comments
 (0)