1717
1818use yii \base \Exception ;
1919
20+ use mikehaertl \shellcommand \Command as ShellCommand ;
21+
2022/**
2123 * @author nystudio107
2224 * @package Transcoder
@@ -178,8 +180,8 @@ public function getVideoUrl($filePath, $videoOptions): string
178180 $ result = Craft::$ app ->config ->get ("transcoderUrl " , "transcoder " ).$ destVideoFile ;
179181 } else {
180182 // Kick off the transcoding
181- $ pid = shell_exec ($ ffmpegCmd );
182- Craft::info ($ ffmpegCmd , __METHOD__ );
183+ $ pid = $ this -> _executeShellCommand ($ ffmpegCmd );
184+ Craft::info ($ ffmpegCmd. "\n ffmpeg PID: " . $ pid , __METHOD__ );
183185
184186 // Create a lockfile in tmp
185187 file_put_contents ($ lockFile , $ pid );
@@ -239,7 +241,7 @@ public function getVideoThumbnailUrl($filePath, $thumbnailOptions): string
239241
240242 // If the thumbnail file already exists, return it. Otherwise, generate it and return it
241243 if (!file_exists ($ destThumbnailPath )) {
242- $ shellOutput = shell_exec ($ ffmpegCmd );
244+ $ shellOutput = $ this -> _executeShellCommand ($ ffmpegCmd );
243245 Craft::info ($ ffmpegCmd , __METHOD__ );
244246 }
245247 $ result = Craft::$ app ->config ->get ("transcoderUrl " , "transcoder " ).$ destThumbnailFile ;
@@ -252,7 +254,7 @@ public function getVideoThumbnailUrl($filePath, $thumbnailOptions): string
252254 * Returns a URL to the transcoded audio file or "" if it doesn't exist
253255 * (at which time it will create it).
254256 *
255- * @param $filePath path to the original audio file -OR- an Asset
257+ * @param $filePath string path to the original audio file -OR- an Asset
256258 * @param $audioOptions array of options for the audio file
257259 *
258260 * @return string URL of the transcoded audio file or ""
@@ -332,8 +334,8 @@ public function getAudioUrl($filePath, $audioOptions): string
332334 $ result = Craft::$ app ->config ->get ("transcoderUrl " , "transcoder " ).$ destAudioFile ;
333335 } else {
334336 // Kick off the transcoding
335- $ pid = shell_exec ($ ffmpegCmd );
336- Craft::info ($ ffmpegCmd , __METHOD__ );
337+ $ pid = $ this -> _executeShellCommand ($ ffmpegCmd );
338+ Craft::info ($ ffmpegCmd. "\n ffmpeg PID: " . $ pid , __METHOD__ );
337339
338340 // Create a lockfile in tmp
339341 file_put_contents ($ lockFile , $ pid );
@@ -364,7 +366,7 @@ public function getFileInfo($filePath, $summary = false): array
364366 .' ' .$ ffprobeOptions
365367 .' ' .escapeshellarg ($ filePath );
366368
367- $ shellOutput = shell_exec ($ ffprobeCmd );
369+ $ shellOutput = $ this -> _executeShellCommand ($ ffprobeCmd );
368370 Craft::info ($ ffprobeCmd , __METHOD__ );
369371 $ result = json_decode ($ shellOutput , true );
370372 Craft::info (print_r ($ result , true ), __METHOD__ );
@@ -600,4 +602,35 @@ protected function coalesceOptions($defaultName, $options): array
600602
601603 return $ options ;
602604 }
605+
606+ // Private Methods
607+ // =========================================================================
608+
609+ /**
610+ * Execute a shell command
611+ *
612+ * @param string $command
613+ *
614+ * @return string
615+ */
616+ private function _executeShellCommand (string $ command ): string
617+ {
618+ // Create the shell command
619+ $ shellCommand = new ShellCommand ();
620+ $ shellCommand ->setCommand ($ command );
621+
622+ // If we don't have proc_open, maybe we've got exec
623+ if (!function_exists ('proc_open ' ) && function_exists ('exec ' )) {
624+ $ shellCommand ->useExec = true ;
625+ }
626+
627+ // Return the result of the command's output or error
628+ if ($ shellCommand ->execute ()) {
629+ $ result = $ shellCommand ->getOutput ();
630+ } else {
631+ $ result = $ shellCommand ->getError ();
632+ }
633+
634+ return $ result ;
635+ }
603636}
0 commit comments