@@ -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
0 commit comments