Skip to content

Commit 6b99c5b

Browse files
committed
### Added
- Added a summary option to `getFileInfo()` ### Changed - Refactored the `config.php` options to be more consistent
1 parent c48bc3f commit 6b99c5b

File tree

6 files changed

+116
-26
lines changed

6 files changed

+116
-26
lines changed

CHANGELOG.md

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

3+
## 1.0.2 - 2017.03.07
4+
### Added
5+
- Added a summary option to `getFileInfo()`
6+
7+
### Changed
8+
- Refactored the `config.php` options to be more consistent
9+
310
## 1.0.1 - 2017.03.06
411
### Added
512
- Added `height` and `width` options for resizing the videos

README.md

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ It will return to you a URL to the transcoded video if it already exists, or if
6666
In the array you pass in, the default values are used if the key/value pair does not exist:
6767

6868
{
69-
"videoFormat" => "mp4",
69+
"videoEncoder" => "h264",
7070
"videoBitRate" => "800k",
7171
"videoFrameRate" => 15,
7272
"aspectRatio" => "letterbox",
@@ -102,7 +102,7 @@ You can control the color of the letterboxed area (it's `black` by default) via
102102

103103
The `sharpen` option determines whether an unsharp mask filter should be applied to the scaled video.
104104

105-
The file format setting `videoFormat` is preset to what you'll need to generate `mp4` videos, but it can also generate `webm` videos, or any other format that `ffmpeg` supports. See the `config.php` file for details
105+
The file format setting `videoEncoder` is preset to what you'll need to generate `h264` videos, but it can also generate `webm` videos, or any other format that `ffmpeg` supports. See the `config.php` file for details
106106

107107
### Generating a Transcoded Audio File
108108

@@ -128,7 +128,7 @@ It will return to you a URL to the transcoded audio file if it already exists, o
128128
In the array you pass in, the default values are used if the key/value pair does not exist:
129129

130130
{
131-
"audioFormat" => "mp3",
131+
"audioEncoder" => "mp3",
132132
"audioBitRate" => "128k",
133133
"audioSampleRate" => "44100",
134134
"audioChannels" => "2",
@@ -146,7 +146,7 @@ If you want to have the Transcoder not change a parameter, pass in an empty valu
146146

147147
The above example would cause it to not change the audio of the source audio file at all (not recommended for client-proofing purposes).
148148

149-
The file format setting `audioFormat` is preset to what you'll need to generate `mp3` audio files, but it can also generate `m4a`, `ogg`, or any other format that `ffmpeg` supports. See the `config.php` file for details
149+
The file format setting `audioEncoder` is preset to what you'll need to generate `mp3` audio files, but it can also generate `aac`, `ogg`, or any other format that `ffmpeg` supports. See the `config.php` file for details
150150

151151
### Getting Transcoding Progress
152152

@@ -242,14 +242,31 @@ The `sharpen` option determines whether an unsharp mask filter should be applied
242242

243243
To get information about an existing video/audio file, you can use `craft.transcoder.getFileInfo()`:
244244

245-
{% set fileInfo = craft.transcoder.getFileInfo('/home/vagrant/sites/nystudio107/public/oceans.mp4') %}
245+
{% set fileInfo = craft.transcoder.getFileInfo('/home/vagrant/sites/nystudio107/public/oceans.mp4', true) %}
246246

247247
You can also pass in an `Asset`:
248248

249249
{% set myAsset = entry.someAsset.first() %}
250-
{% set fileInfo = craft.transcoder.getFileInfo(myAsset) %}
250+
{% set fileInfo = craft.transcoder.getFileInfo(myAsset, true) %}
251251

252-
This returns an array with two top-level keys:
252+
By passing in `true` as the second argument, we get just a summary of the video/audio file information in an array:
253+
254+
[
255+
'videoEncoder' => 'h264'
256+
'videoBitRate' => '3859635'
257+
'videoFrameRate' => 23.976023976024
258+
'height' => 400
259+
'width' => 960
260+
'audioEncoder' => 'aac'
261+
'audioBitRate' => '92926'
262+
'audioSampleRate' => '48000'
263+
'audioChannels' => 2
264+
'filename' => '/htdocs/craft3/public/assets/oceans.mp4'
265+
'duration' => '46.613333'
266+
'size' => '23014356'
267+
]
268+
269+
If you instead pass in `false` as the second parameter (or omit it), then `craft.transcoder.getFileInfo()` returns the full video/audio file info an array with two top-level keys:
253270

254271
* `format` - information about the container file format
255272
* `streams` - information about each stream in the container; many videos have multiple streams, for instance, one for the video streams, and another for the audio stream. There can even be multiple video or audio streams in a container.
@@ -387,7 +404,6 @@ The file must reside in the webroot (thus a URL or URI must be passed in as a pa
387404

388405
Some things to do, and ideas for potential features:
389406

390-
* Add a simpler way to extract the most common video/audio info from a file
391407
* Add a console command for doing encodings via console
392408
* Figure out a way to reliably do multi-pass video encoding
393409
* Add audio normalization via `loudnorm` http://k.ylo.ph/2016/04/04/loudnorm.html

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 video & audio files to various formats, and provide video thumbnails",
44
"type": "craft-plugin",
5-
"version": "1.0.1",
5+
"version": "1.0.2",
66
"keywords": [
77
"craft",
88
"cms",

src/config.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,17 @@
4545

4646
// Preset video encoders
4747
"videoEncoders" => [
48-
"mp4" => [
48+
"h264" => [
4949
"fileSuffix" => ".mp4",
50+
"fileFormat" => "mp4",
5051
"videoCodec" => "libx264",
5152
"videoCodecOptions" => "-vprofile high -preset slow -crf 22",
5253
"audioCodec" => "libfdk_aac",
5354
"audioCodecOptions" => "-async 1000",
5455
],
5556
"webm" => [
5657
"fileSuffix" => ".webm",
58+
"fileFormat" => "webm",
5759
"videoCodec" => "libvpx",
5860
"videoCodecOptions" => "-quality good -cpu-used 0",
5961
"audioCodec" => "libvorbis",
@@ -65,17 +67,20 @@
6567
"audioEncoders" => [
6668
"mp3" => [
6769
"fileSuffix" => ".mp3",
70+
"fileFormat" => "mp3",
6871
"audioCodec" => "libmp3lame",
6972
"audioCodecOptions" => "",
7073
],
71-
"m4a" => [
74+
"aac" => [
7275
"fileSuffix" => ".m4a",
76+
"fileFormat" => "aac",
7377
"audioCodec" => "libfdk_aac",
7478
"audioCodecOptions" => "",
7579

7680
],
7781
"ogg" => [
7882
"fileSuffix" => ".ogg",
83+
"fileFormat" => "ogg",
7984
"audioCodec" => "libvorbis",
8085
"audioCodecOptions" => "",
8186
],
@@ -84,7 +89,7 @@
8489
// Default options for encoded videos
8590
"defaultVideoOptions" => [
8691
// Video settings
87-
"videoFormat" => "mp4",
92+
"videoEncoder" => "h264",
8893
"videoBitRate" => "800k",
8994
"videoFrameRate" => 15,
9095
// Audio settings
@@ -114,7 +119,7 @@
114119

115120
// Default options for encoded videos
116121
"defaultAudioOptions" => [
117-
"audioFormat" => "mp3",
122+
"audioEncoder" => "mp3",
118123
"audioBitRate" => "128k",
119124
"audioSampleRate" => "44100",
120125
"audioChannels" => "2",

src/services/Transcoder.php

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

1313
use Craft;
1414
use craft\base\Component;
15-
use craft\console\Request;
1615
use craft\elements\Asset;
1716
use craft\volumes\Local;
1817

@@ -41,12 +40,34 @@ class Transcoder extends Component
4140

4241
// Params that should be excluded from being part of the generated filename
4342
protected $excludeParams = [
44-
'videoFormat',
45-
'audioFormat',
43+
'videoEncoder',
44+
'audioEncoder',
4645
'fileSuffix',
4746
'sharpen'
4847
];
4948

49+
// Mappings for getFileInfo() summary values
50+
protected $infoSummary = [
51+
'format' => [
52+
'filename' => 'filename',
53+
'duration' => 'duration',
54+
'size' => 'size',
55+
],
56+
'audio' => [
57+
'codec_name' => 'audioEncoder',
58+
'bit_rate' => 'audioBitRate',
59+
'sample_rate' => 'audioSampleRate',
60+
'channels' => 'audioChannels',
61+
],
62+
'video' => [
63+
'codec_name' => 'videoEncoder',
64+
'bit_rate' => 'videoBitRate',
65+
'avg_frame_rate' => 'videoFrameRate',
66+
'height' => 'height',
67+
'width' => 'width',
68+
],
69+
];
70+
5071
// Public Methods
5172
// =========================================================================
5273

@@ -72,7 +93,7 @@ public function getVideoUrl($filePath, $videoOptions): string
7293

7394
// Get the video encoder presets to use
7495
$videoEncoders = Craft::$app->config->get("videoEncoders", "transcoder");
75-
$thisEncoder = $videoEncoders[$videoOptions['videoFormat']];
96+
$thisEncoder = $videoEncoders[$videoOptions['videoEncoder']];
7697

7798
$videoOptions['fileSuffix'] = $thisEncoder['fileSuffix'];
7899

@@ -135,7 +156,7 @@ public function getVideoUrl($filePath, $videoOptions): string
135156
// Assemble the destination path and final ffmpeg command
136157
$destVideoPath = $destVideoPath.$destVideoFile;
137158
$ffmpegCmd .= ' -f '
138-
.$videoOptions['videoFormat']
159+
.$thisEncoder['fileFormat']
139160
.' -y '.escapeshellarg($destVideoPath)
140161
.' 1> '.$progressFile.' 2>&1 & echo $!';
141162

@@ -249,7 +270,7 @@ public function getAudioUrl($filePath, $audioOptions): string
249270

250271
// Get the audio encoder presets to use
251272
$audioEncoders = Craft::$app->config->get("audioEncoders", "transcoder");
252-
$thisEncoder = $audioEncoders[$audioOptions['audioFormat']];
273+
$thisEncoder = $audioEncoders[$audioOptions['audioEncoder']];
253274

254275
$audioOptions['fileSuffix'] = $thisEncoder['fileSuffix'];
255276

@@ -289,7 +310,7 @@ public function getAudioUrl($filePath, $audioOptions): string
289310
// Assemble the destination path and final ffmpeg command
290311
$destAudioPath = $destAudioPath.$destAudioFile;
291312
$ffmpegCmd .= ' -f '
292-
.$audioOptions['audioFormat']
313+
.$thisEncoder['fileFormat']
293314
.' -y '.escapeshellarg($destAudioPath)
294315
.' 1> '.$progressFile.' 2>&1 & echo $!';
295316

@@ -325,11 +346,12 @@ public function getAudioUrl($filePath, $audioOptions): string
325346
/**
326347
* Extract information from a video/audio file
327348
*
328-
* @param $filePath
349+
* @param $filePath
350+
* @param bool $summary
329351
*
330352
* @return array
331353
*/
332-
public function getFileInfo($filePath): array
354+
public function getFileInfo($filePath, $summary = false): array
333355
{
334356

335357
$result = null;
@@ -346,6 +368,46 @@ public function getFileInfo($filePath): array
346368
Craft::info($ffprobeCmd, __METHOD__);
347369
$result = json_decode($shellOutput, true);
348370
Craft::info(print_r($result, true), __METHOD__);
371+
372+
// Trim down the arrays to just a summary
373+
if ($summary && !empty($result)) {
374+
$summaryResult = [];
375+
foreach ($result as $topLevelKey => $topLevelValue) {
376+
switch ($topLevelKey) {
377+
// Format info
378+
case "format":
379+
foreach ($this->infoSummary['format'] as $settingKey => $settingValue) {
380+
if (!empty($topLevelValue[$settingKey])) {
381+
$summaryResult[$settingValue] = $topLevelValue[$settingKey];
382+
}
383+
}
384+
break;
385+
// Stream info
386+
case "streams":
387+
foreach ($topLevelValue as $stream) {
388+
$infoSummaryType = $stream['codec_type'];
389+
foreach ($this->infoSummary[$infoSummaryType] as $settingKey => $settingValue) {
390+
if (!empty($stream[$settingKey])) {
391+
$summaryResult[$settingValue] = $stream[$settingKey];
392+
}
393+
}
394+
395+
}
396+
break;
397+
// Unknown info
398+
default:
399+
break;
400+
}
401+
}
402+
// Handle cases where the framerate is returned as XX/YY
403+
if (!empty($summaryResult['videoFrameRate'])
404+
&& (strpos($summaryResult['videoFrameRate'], '/') !== false)
405+
) {
406+
$parts = explode('/', $summaryResult['videoFrameRate']);
407+
$summaryResult['videoFrameRate'] = floatval($parts[0]) / floatval($parts[1]);
408+
}
409+
$result = $summaryResult;
410+
}
349411
}
350412

351413
return $result;
@@ -365,7 +427,7 @@ public function getVideoFilename($filePath, $videoOptions): string
365427

366428
// Get the video encoder presets to use
367429
$videoEncoders = Craft::$app->config->get("videoEncoders", "transcoder");
368-
$thisEncoder = $videoEncoders[$videoOptions['videoFormat']];
430+
$thisEncoder = $videoEncoders[$videoOptions['videoEncoder']];
369431

370432
$videoOptions['fileSuffix'] = $thisEncoder['fileSuffix'];
371433

@@ -388,7 +450,7 @@ public function getAudioFilename($filePath, $audioOptions): string
388450

389451
// Get the video encoder presets to use
390452
$audioEncoders = Craft::$app->config->get("audioEncoders", "transcoder");
391-
$thisEncoder = $audioEncoders[$audioOptions['audioFormat']];
453+
$thisEncoder = $audioEncoders[$audioOptions['audioEncoder']];
392454

393455
$audioOptions['fileSuffix'] = $thisEncoder['fileSuffix'];
394456

src/variables/TranscoderVariable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ public function getAudioUrl($filePath, $audioOptions): string
7979
*
8080
* @return array
8181
*/
82-
public function getFileInfo($filePath): array
82+
public function getFileInfo($filePath, $summary = false): array
8383
{
84-
$result = Transcoder::$plugin->transcoder->getFileInfo($filePath);
84+
$result = Transcoder::$plugin->transcoder->getFileInfo($filePath, $summary);
8585

8686
return $result;
8787
}

0 commit comments

Comments
 (0)