Skip to content

Commit 43c66b8

Browse files
committed
* Initial checkin
0 parents  commit 43c66b8

File tree

12 files changed

+757
-0
lines changed

12 files changed

+757
-0
lines changed

.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# COMPOSER
2+
/vendor
3+
4+
# BUILD FILES
5+
/bower_components/*
6+
/node_modules/*
7+
/build/*
8+
9+
# MISC FILES
10+
.cache
11+
.DS_Store
12+
.idea
13+
.project
14+
.settings
15+
*.esproj
16+
*.sublime-workspace
17+
*.sublime-project
18+
*.tmproj
19+
*.tmproject

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Transcoder Changelog
2+
3+
## 1.0.0 - 2017.03.05
4+
### Added
5+
- Initial release

LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 nystudio107
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# Transcoder plugin for Craft CMS 3.x
2+
3+
Transcode videos to various formats, and provide thumbnails of the video
4+
5+
## Installation
6+
7+
To install Transcoder, follow these steps:
8+
9+
3. Install with Composer via `composer require nystudio107/craft3-transcoder`
10+
4. Install plugin in the Craft Control Panel under Settings > Plugins
11+
12+
Transcoder works on Craft 3.x.
13+
14+
## Transcoder Overview
15+
16+
The Transcoder video allows you to take any locally stored video, and transcode it into any bitrate/framerate, and save it out as a web-ready `.mp4` file.
17+
18+
It also allows you to get a thumbnail of the video in any size and at any timecode.
19+
20+
Finally, it lets you download an arbitrary file (such as the transcoded video) via a special download link.
21+
22+
If the source file has changed since the last time the video was encoded, it will re-encode the video and replace it.
23+
24+
## Configuring Transcoder
25+
26+
The only configuration for Transcoder is in the `config.php` file, which is a multi-environment friendly way to store the default settings. Don't edit this file, instead copy it to `craft/config` as `transcoder.php` and make your changes there. Here's the default `config.php` file:
27+
28+
<?php
29+
/**
30+
* Transcoder plugin for Craft CMS 3.x
31+
*
32+
* Transcode videos to various formats, and provide thumbnails of the video
33+
*
34+
* @link https://nystudio107.com
35+
* @copyright Copyright (c) 2017 nystudio107
36+
*/
37+
38+
/**
39+
* Transcoder config.php
40+
*
41+
* Completely optional configuration settings for Transcoder if you want to
42+
* customize some of its more esoteric behavior, or just want specific control
43+
* over things.
44+
*
45+
* Don't edit this file, instead copy it to 'craft/config' as 'transcoder.php'
46+
* and make your changes there.
47+
*
48+
* Once copied to 'craft/config', this file will be multi-environment aware as
49+
* well, so you can have different settings groups for each environment, just as
50+
* you do for 'general.php'
51+
*/
52+
53+
return [
54+
55+
// The path to the ffmpeg binary
56+
"ffmpegPath" => "/usr/bin/ffmpeg",
57+
58+
// The path where the transcoded videos are stored
59+
"transcoderPath" => $_SERVER['DOCUMENT_ROOT'] . "/transcoder/",
60+
61+
// The URL where the transcoded videos are stored
62+
"transcoderUrl" => "/transcoder/",
63+
64+
];
65+
66+
## Using Transcoder
67+
68+
### Generating a Transcoded Video
69+
70+
To generate a transcoded video, do the following:
71+
72+
{% set transVideoUrl = craft.transcoder.getVideoUrl('/home/vagrant/sites/nystudio107/public/trimurti.mp4', {
73+
"frameRate": 20,
74+
"bitRate": "500k"
75+
}) %}
76+
77+
You can also pass in an `AssetFileModel`:
78+
79+
{% set myAsset = entry.someAsset.first() %}
80+
{% set transVideoUrl = craft.transcoder.getVideoUrl(myAsset, {
81+
"frameRate": 20,
82+
"bitRate": "500k"
83+
}) %}
84+
85+
It will return to you a URL to the transcoded video if it already exists, or if it doesn't exist, it will return `""` and kick off the transcoding process (which can be quite lengthy for long videos).
86+
87+
In the array you pass in, the default values are used if the key/value pair does not exist:
88+
89+
{
90+
"bitRate" => "800k",
91+
"frameRate" => 15,
92+
}
93+
94+
If you want to have the Transcoder not change a parameter, pass in an empty value in the key/value pair, e.g.:
95+
96+
{% set transVideoUrl = craft.transcoder.getVideoUrl('/home/vagrant/sites/nystudio107/public/trimurti.mp4', {
97+
"frameRate": "",
98+
"bitRate": ""
99+
}) %}
100+
101+
The above example would cause it to not change the frameRate or bitRate of the source movie (not recommended for client-proofing purposes).
102+
103+
### Generating a Video Thumbnail
104+
105+
To generate a thumbnail from a video, do the following:
106+
107+
{% set transVideoThumbUrl = craft.transcoder.getVideoThumbnailUrl('/home/vagrant/sites/nystudio107/public/trimurti.mp4', {
108+
"width": 300,
109+
"height": 200,
110+
"timeInSecs": 20,
111+
}) %}
112+
113+
It will return to you a URL to the thumbnail of the video, in the size you specify, from the timecode `timeInSecs` in the video. It creates this thumbnail immediately if it doesn't already exist.
114+
115+
In the array you pass in, the default values are used if the key/value pair does not exist:
116+
117+
{
118+
"width" => 200,
119+
"height" => 100,
120+
"timeInSecs" => 10,
121+
}
122+
123+
If you want to have the Transcoder not change a parameter, pass in an empty value in the key/value pair, e.g.:
124+
125+
{% set transVideoThumbUrl = craft.transcoder.getVideoThumbnailUrl('/home/vagrant/sites/nystudio107/public/trimurti.mp4', {
126+
"width": "",
127+
"height": "",
128+
"timeInSecs": 20,
129+
}) %}
130+
131+
The above example would cause it to generate a thumbnail at whatever size the video is (not recommended for client-proofing purposes).
132+
133+
### Generating a Download URL
134+
135+
To generate a download URL for a file, do the following:
136+
137+
{% set downloadUrl = craft.transcoder.getDownloadUrl('/some/url') %}
138+
139+
When the user clicks on the URL, it will download the file to their local computer. If the file doesn't exist, `""` is returned.
140+
141+
The file must reside in the webroot (thus a URL or URI must be passed in as a parameter, not a path), for security reasons.
142+
143+
## Transcoder Roadmap
144+
145+
Some things to do, and ideas for potential features:
146+
147+
* The videos could potentially be saved in different formats (though `.mp4` really is "the" standard for video)
148+
* The videos could potentially be resized, either to an aspect ratio or an absolute size or what have you
149+
* Accessors could be written to get information about a video (height, width, duration, and so on)
150+
151+
## Transcoder Changelog
152+
153+
### 1.0.0 -- 2017.03.05
154+
155+
* Initial release
156+
157+
Brought to you by [nystudio107](https://nystudio107.com)

composer.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "nystudio107/craft3-transcoder",
3+
"description": "Transcode videos to various formats, and provide thumbnails of the video",
4+
"type": "craft-plugin",
5+
"version": "1.0.0",
6+
"keywords": [
7+
"craft",
8+
"cms",
9+
"craftcms",
10+
"craft-plugin",
11+
"transcoder"
12+
],
13+
"support": {
14+
"docs": "https://github.com/nystudio107/craft3-transcoder/blob/master/README.md",
15+
"issues": "https://github.com/nystudio107/craft3-transcoder/issues"
16+
},
17+
"license": "MIT",
18+
"authors": [
19+
{
20+
"name": "nystudio107",
21+
"homepage": "https://nystudio107.com"
22+
}
23+
],
24+
"require": {
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"nystudio107\\transcoder\\": "src/"
29+
}
30+
},
31+
"extra": {
32+
"name": "Transcoder",
33+
"handle": "transcoder",
34+
"schemaVersion": "1.0.0",
35+
"hasSettings": false,
36+
"hasCpSection": false,
37+
"changelogUrl": "https://raw.githubusercontent.com/nystudio107/craft3-transcoder/master/CHANGELOG.md",
38+
"components": {
39+
"transcoder": "nystudio107\\transcoder\\services\\Transcoder"
40+
},
41+
"class": "nystudio107\\transcoder\\Transcoder"
42+
}
43+
}

src/Transcoder.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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;
12+
13+
use nystudio107\transcoder\variables\TranscoderVariable;
14+
15+
use Craft;
16+
use craft\base\Plugin;
17+
use craft\services\Plugins;
18+
use craft\events\PluginEvent;
19+
use craft\web\UrlManager;
20+
use craft\events\RegisterUrlRulesEvent;
21+
use yii\base\Event;
22+
23+
/**
24+
* @author nystudio107
25+
* @package Transcoder
26+
* @since 1.0.0
27+
*/
28+
class Transcoder extends Plugin
29+
{
30+
// Static Properties
31+
// =========================================================================
32+
33+
/**
34+
* @var static
35+
*/
36+
public static $plugin;
37+
38+
// Public Methods
39+
// =========================================================================
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
public function init()
45+
{
46+
parent::init();
47+
self::$plugin = $this;
48+
49+
Craft::info('Transcoder ' . Craft::t('transcoder', 'plugin loaded'), __METHOD__);
50+
}
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
public function defineTemplateComponent()
56+
{
57+
return TranscoderVariable::class;
58+
}
59+
60+
}

src/config.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
/**
12+
* Transcoder config.php
13+
*
14+
* Completely optional configuration settings for Transcoder if you want to
15+
* customize some of its more esoteric behavior, or just want specific control
16+
* over things.
17+
*
18+
* Don't edit this file, instead copy it to 'craft/config' as 'transcoder.php'
19+
* and make your changes there.
20+
*
21+
* Once copied to 'craft/config', this file will be multi-environment aware as
22+
* well, so you can have different settings groups for each environment, just as
23+
* you do for 'general.php'
24+
*/
25+
26+
return [
27+
28+
// The path to the ffmpeg binary
29+
"ffmpegPath" => "/usr/bin/ffmpeg",
30+
31+
// The path where the transcoded videos are stored
32+
"transcoderPath" => $_SERVER['DOCUMENT_ROOT'] . "/transcoder/",
33+
34+
// The URL where the transcoded videos are stored
35+
"transcoderUrl" => "/transcoder/",
36+
37+
];
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\controllers;
12+
13+
use nystudio107\transcoder\Transcoder;
14+
15+
use Craft;
16+
use craft\web\Controller;
17+
18+
/**
19+
* @author nystudio107
20+
* @package Transcoder
21+
* @since 1.0.0
22+
*/
23+
class TranscoderController extends Controller
24+
{
25+
26+
// Protected Properties
27+
// =========================================================================
28+
29+
/**
30+
* @var bool|array Allows anonymous access to this controller's actions.
31+
* The actions must be in 'kebab-case'
32+
* @access protected
33+
*/
34+
protected $allowAnonymous = ['download-file'];
35+
36+
// Public Methods
37+
// =========================================================================
38+
39+
/**
40+
* Force the download of a given $url. We do it this way to prevent people
41+
* from downloading things that are outside of the server root.
42+
*/
43+
public function actionDownloadFile()
44+
{
45+
$url = urldecode(Craft::$app->getRequest()->getParam('url'));
46+
$filePath = parse_url($url, PHP_URL_PATH);
47+
$filePath = $_SERVER['DOCUMENT_ROOT'] . $filePath;
48+
Craft::$app->getResponse()->sendFile(
49+
$filePath,
50+
null,
51+
['inline' => false]
52+
);
53+
}
54+
}

0 commit comments

Comments
 (0)