1212
1313use Craft ;
1414use craft \base \Component ;
15+ use craft \console \Request ;
1516use craft \elements \Asset ;
1617use 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