Skip to content

Commit 0e6ef8e

Browse files
authored
Merge pull request #11 from skoften/v1
Four enhancements
2 parents ede75ab + 4e58334 commit 0e6ef8e

File tree

4 files changed

+82
-32
lines changed

4 files changed

+82
-32
lines changed

src/Transcoder.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,28 @@ public function init()
8989
public function clearAllCaches()
9090
{
9191
$transcoderPaths = Transcoder::$plugin->getSettings()->transcoderPaths;
92-
foreach ($transcoderPaths as $key => $value) {
93-
$dir = Craft::getAlias($value);
94-
try {
95-
FileHelper::clearDirectory($dir);
96-
Craft::info(
97-
Craft::t(
98-
'transcoder',
99-
'{name} cache directory cleared',
100-
['name' => $key]
101-
),
102-
__METHOD__
103-
);
104-
} catch (ErrorException $e) {
105-
// the directory doesn't exist
106-
Craft::error($e->getMessage(), __METHOD__);
92+
$clearCaches = Transcoder::$plugin->getSettings()->clearCaches;
93+
94+
if($clearCaches) {
95+
foreach ($transcoderPaths as $key => $value) {
96+
$dir = Craft::getAlias($value);
97+
try {
98+
FileHelper::clearDirectory($dir);
99+
Craft::info(
100+
Craft::t(
101+
'transcoder',
102+
'{name} cache directory cleared',
103+
['name' => $key]
104+
),
105+
__METHOD__
106+
);
107+
} catch (ErrorException $e) {
108+
// the directory doesn't exist
109+
Craft::error($e->getMessage(), __METHOD__);
110+
}
107111
}
108112
}
113+
109114
}
110115

111116
// Protected Methods

src/config.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757
// Use a md5 hash for the filenames instead of parameterized naming
5858
'useHashedNames' => false,
5959

60+
// if a upload location has a subfolder defined, add this to the transcoder paths too
61+
'createSubfolders' => true,
62+
63+
// clear caches when somebody clears all caches from the CP?
64+
'clearCaches' => false,
65+
6066
// Preset video encoders
6167
'videoEncoders' => [
6268
'h264' => [

src/services/Transcode.php

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,28 @@ class Transcode extends Component
8686
*
8787
* @param $filePath string path to the original video -OR- an Asset
8888
* @param $videoOptions array of options for the video
89+
* @param bool $generate whether the video should be encoded
8990
*
9091
* @return string URL of the transcoded video or ""
9192
*/
92-
public function getVideoUrl($filePath, $videoOptions): string
93+
public function getVideoUrl($filePath, $videoOptions, $generate = true): string
9394
{
95+
9496
$result = '';
9597
$settings = Transcoder::$plugin->getSettings();
98+
$subfolder = '';
99+
100+
// sub folder check
101+
if(\is_object($filePath) && ($filePath instanceof Asset) && $settings['createSubfolders']) {
102+
$subfolder = $filePath->folderPath;
103+
}
104+
105+
// file path
96106
$filePath = $this->getAssetPath($filePath);
97-
107+
98108
if (!empty($filePath)) {
99-
$destVideoPath = $settings['transcoderPaths']['video'] ?? $settings['transcoderPaths']['default'];
100-
$destVideoPath = Craft::getAlias($destVideoPath);
109+
$destVideoPath = $settings['transcoderPaths']['video'] . $subfolder ?? $settings['transcoderPaths']['default'];
110+
$destVideoPath = Craft::getAlias($destVideoPath);
101111
$videoOptions = $this->coalesceOptions('defaultVideoOptions', $videoOptions);
102112

103113
// Get the video encoder presets to use
@@ -188,8 +198,12 @@ public function getVideoUrl($filePath, $videoOptions): string
188198

189199
// If the video file already exists and hasn't been modified, return it. Otherwise, start it transcoding
190200
if (file_exists($destVideoPath) && (@filemtime($destVideoPath) >= @filemtime($filePath))) {
191-
$url = $settings['transcoderUrls']['video'] ?? $settings['transcoderUrls']['default'];
201+
$url = $settings['transcoderUrls']['video'] . $subfolder ?? $settings['transcoderUrls']['default'];
192202
$result = Craft::getAlias($url).$destVideoFile;
203+
204+
// skip encoding
205+
} elseif (!$generate) {
206+
$result = "";
193207
} else {
194208
// Kick off the transcoding
195209
$pid = $this->executeShellCommand($ffmpegCmd);
@@ -215,14 +229,20 @@ public function getVideoUrl($filePath, $videoOptions): string
215229
* @return string|false|null URL or path of the video thumbnail
216230
*/
217231
public function getVideoThumbnailUrl($filePath, $thumbnailOptions, $generate = true, $asPath = false)
218-
{
219-
232+
{
220233
$result = null;
221234
$settings = Transcoder::$plugin->getSettings();
235+
$subfolder = '';
236+
237+
// sub folder check
238+
if(\is_object($filePath) && ($filePath instanceof Asset) && $settings['createSubfolders']) {
239+
$subfolder = $filePath->folderPath;
240+
}
241+
222242
$filePath = $this->getAssetPath($filePath);
223243

224244
if (!empty($filePath)) {
225-
$destThumbnailPath = $settings['transcoderPaths']['thumbnail'] ?? $settings['transcoderPaths']['default'];
245+
$destThumbnailPath = $settings['transcoderPaths']['thumbnail'] . $subfolder ?? $settings['transcoderPaths']['default'];
226246
$destThumbnailPath = Craft::getAlias($destThumbnailPath);
227247

228248
$thumbnailOptions = $this->coalesceOptions('defaultThumbnailOptions', $thumbnailOptions);
@@ -266,6 +286,12 @@ public function getVideoThumbnailUrl($filePath, $thumbnailOptions, $generate = t
266286
/** @noinspection PhpUnusedLocalVariableInspection */
267287
$shellOutput = $this->executeShellCommand($ffmpegCmd);
268288
Craft::info($ffmpegCmd, __METHOD__);
289+
290+
// if ffmpeg fails which we can't check because the process is ran in the background
291+
// dont return the future path of the image or else we can't check this in the front end
292+
293+
return false;
294+
269295
} else {
270296
Craft::info('Thumbnail does not exist, but not asked to generate it: '.$filePath, __METHOD__);
271297

@@ -277,7 +303,7 @@ public function getVideoThumbnailUrl($filePath, $thumbnailOptions, $generate = t
277303
if ($asPath) {
278304
$result = $destThumbnailPath;
279305
} else {
280-
$url = $settings['transcoderUrls']['thumbnail'] ?? $settings['transcoderUrls']['default'];
306+
$url = $settings['transcoderUrls']['thumbnail'] . $subfolder ?? $settings['transcoderUrls']['default'];
281307
$result = Craft::getAlias($url).$destThumbnailFile;
282308
}
283309
}
@@ -296,13 +322,19 @@ public function getVideoThumbnailUrl($filePath, $thumbnailOptions, $generate = t
296322
*/
297323
public function getAudioUrl($filePath, $audioOptions): string
298324
{
299-
300325
$result = '';
301326
$settings = Transcoder::$plugin->getSettings();
327+
$subfolder = '';
328+
329+
// sub folder check
330+
if(\is_object($filePath) && ($filePath instanceof Asset) && $settings['createSubfolders']) {
331+
$subfolder = $filePath->folderPath;
332+
}
333+
302334
$filePath = $this->getAssetPath($filePath);
303335

304336
if (!empty($filePath)) {
305-
$destAudioPath = $settings['transcoderPaths']['audio'] ?? $settings['transcoderPaths']['default'];
337+
$destAudioPath = $settings['transcoderPaths']['audio'] . $subfolder ?? $settings['transcoderPaths']['default'];
306338
$destAudioPath = Craft::getAlias($destAudioPath);
307339

308340
$audioOptions = $this->coalesceOptions('defaultAudioOptions', $audioOptions);
@@ -372,7 +404,7 @@ public function getAudioUrl($filePath, $audioOptions): string
372404

373405
// If the audio file already exists and hasn't been modified, return it. Otherwise, start it transcoding
374406
if (file_exists($destAudioPath) && (@filemtime($destAudioPath) >= @filemtime($filePath))) {
375-
$url = $settings['transcoderUrls']['audio'] ?? $settings['transcoderUrls']['default'];
407+
$url = $settings['transcoderUrls']['audio'] . $subfolder ?? $settings['transcoderUrls']['default'];
376408
$result = Craft::getAlias($url).$destAudioFile;
377409
} else {
378410
// Kick off the transcoding
@@ -397,7 +429,6 @@ public function getAudioUrl($filePath, $audioOptions): string
397429
*/
398430
public function getFileInfo($filePath, $summary = false): array
399431
{
400-
401432
$result = null;
402433
$settings = Transcoder::$plugin->getSettings();
403434
$filePath = $this->getAssetPath($filePath);
@@ -557,11 +588,18 @@ public function getGifUrl($filePath, $gifOptions): string
557588
{
558589
$result = '';
559590
$settings = Transcoder::$plugin->getSettings();
591+
$subfolder = '';
592+
593+
// sub folder check
594+
if(\is_object($filePath) && ($filePath instanceof Asset) && $settings['createSubfolders']) {
595+
$subfolder = $filePath->folderPath;
596+
}
597+
560598
$filePath = $this->getAssetPath($filePath);
561599

562600
if (!empty($filePath)) {
563601
// Dest path
564-
$destVideoPath = $settings['transcoderPaths']['gif'] ?? $settings['transcoderPaths']['default'];
602+
$destVideoPath = $settings['transcoderPaths']['gif'] . $subfolder ?? $settings['transcoderPaths']['default'];
565603
$destVideoPath = Craft::getAlias($destVideoPath);
566604

567605
// Options
@@ -615,7 +653,7 @@ public function getGifUrl($filePath, $gifOptions): string
615653

616654
// If the video file already exists and hasn't been modified, return it. Otherwise, start it transcoding
617655
if (file_exists($destVideoPath) && (@filemtime($destVideoPath) >= @filemtime($filePath))) {
618-
$url = $settings['transcoderUrls']['gif'] ?? $settings['transcoderUrls']['default'];
656+
$url = $settings['transcoderUrls']['gif'] . $subfolder ?? $settings['transcoderUrls']['default'];
619657
$result = Craft::getAlias($url).$destVideoFile;
620658
} else {
621659
// Kick off the transcoding

src/variables/TranscoderVariable.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ class TranscoderVariable extends ManifestVariable
3030
*
3131
* @param $filePath
3232
* @param $videoOptions
33+
* @bool $generate
3334
*
3435
* @return string
3536
*/
36-
public function getVideoUrl($filePath, $videoOptions): string
37+
public function getVideoUrl($filePath, $videoOptions, $generate = true): string
3738
{
38-
return Transcoder::$plugin->transcode->getVideoUrl($filePath, $videoOptions);
39+
return Transcoder::$plugin->transcode->getVideoUrl($filePath, $videoOptions, $generate);
3940
}
4041

4142
/**

0 commit comments

Comments
 (0)