Wolfie
Wolfie

Reputation: 1381

FFmpeg not working

I own the website www.goploom.com which similarly to youtube, allows users to upload and view videos. The videos that are uploaded are converted to the mp4 format for compatibility with our site's html5 video player. My problem is that while FFmpeg successfully generates video thumbnails from the uploaded video and stores the image in the proper directory, The video conversion aspect of my code does not produce any output. I have tweaked the code for a couple hours, and I have not been able to resolve this issue, however, I am told that I can perform video conversion with html, php, and ffmpeg.

My path to ffmpeg is correct as the thumbnail generation works perfectly.

**Information collected from the uploaded video upon upload. $name = $_FILES['video']['name']; $title = $_POST['videotitle']; $type = explode('.', $name);
$type = end($type); $size = $_FILES['video']['size']; $random_name = rand(); $tmp = $_FILES['video']['tmp_name'];

                if($type != 'mp4' && $type != 'MP4' && $type != 'flv' && $type != 'FLV' && $type != 'mov' && $type != 'MOV'){
                    $message = "Video Format Not Supported<br>";
                } else {
            $username_query = mysql_query("SELECT `username` FROM `users` WHERE `user_id` = $session_user_id");
                    $username = mysql_fetch_assoc($username_query); 
                    $username = $username['username'];

**Thumbnail generation that works perfectly. $ffmpeg = "/usr/local/bin/ffmpeg"; $videoFile = $_FILES['video']['tmp_name']; $imageFile = "uploads/".$username."/thumbnails/".$random_name.".jpg"; $size = "160x90"; $getFromSecond = 5; $cmd = "$ffmpeg -i $videoFile -an -ss $getFromSecond -s $size $imageFile"; if(!shell_exec($cmd)){ echo "Thumbnail Created!
";
} else { echo "Error Creating Thumbnail!"; }

**Moves the uploaded file to its permanent directory for video conversion. $newFileLocation = "uploads/".$username."/temp".$random_name.".".$type; move_uploaded_file($tmp, $newFileLocation);

*Video Conversion code. $outputFile = "uploads/".$username."/".$random_name.".mp4"; $databaseStoredVideoUrl = "uploads/".$username."/".$random_name.".mp4"; $video_cmd = "$ffmpeg -i $newFileLocation $outputFile"; echo $video_cmd; if(!shell_exec($video_cmd)){ echo "Video Converted!
"; } else { echo "Error Creating Thumbnail!";
}

**Properly stores the output src link in the database, but the video cannot be viewed because it does not exist. // Video Storage into database under correct username and user directory mysql_query("INSERT INTO videos VALUES('$session_user_id' ,'', '$name', '$outputFile', '$title', '', '$imageFile')"); $message = "Video Upload Successful.
";

Any help or insight would be brilliant. Thanks.

**EDIT: Fixed by using the following line. $video_cmd = "$ffmpeg -i $newFileLocation -vcodec libx264 -vpre normal -acodec libfaac $outputFile";

and then inputting $video_cmd into shell_exec

Upvotes: 0

Views: 5505

Answers (1)

cristobal
cristobal

Reputation: 462

Dunno. Worked myself with FFMPEG and PHP a couple of times, some issues you can ran into are the following:

  1. Memory limit since its seems you are running the video encoding server side, which i would rather not recommend.
  2. Secondly the ffmpeg process itself can take too long since how long the execution of the video enconding can take longer that what the maximum script execution is permitted by your web server Apache/Nginx.

If you are running the encoding as web request you should rather refrain from this, and instead make a cron job or a separate service that checks if there are any new videos that should be processed and encoded (video queue service).

Steps to ensure this roughly make:

  1. First test your encoding directly from the cmdline.
  2. Update your video table and flag those that should be processsed, or make an video queue table.
  3. Have a cron job or a service that checks for new videos to be processed, and then processed them one by one.

If you are most comfortable with PHP, make a php script with ini_set("set_time_limit", 0) to make sure you script does not timeout. Other options could be a bash script, or for instance use node.js along with forever an ffmpeg library to make the processing service.

Upvotes: 1

Related Questions