How to convert video in the background using PHP and ffmpeg

后端 未结 1 1647
余生分开走
余生分开走 2020-12-22 08:59

I allow users to upload videos and then they get converted using ffmpeg. The video takes a really long time to convert which usually results in an error. I have done my rese

相关标签:
1条回答
  • 2020-12-22 10:02

    Here is a basic run-down of how to make your own queue using a scheduling system such as Cron:

    • Create a database table queue containing (id, created_at, file_path, id_user, result, error). The file_path contains the location of the uploaded video to process, the result is null before processing and then true/false afterwards depending on success, and if it failed error contains any messages. The primary key of the user table can be saved here too, if appropriate.
    • Every minute, run a Cron program to check the queue for any unprocessed items.
    • If there are items waiting, loop through a few of them and run your video conversion code. You may wish to limit this so that no more than, say, five items are processed in one go, and any more queued items have to wait for a new cron run.
    • At the start of your cron script you need to exit early if an old copy is already running. You can use the output of ps aux | grep (scriptname) to help here, if you are running in a *nix-like operating system.

    Inside your web application, you need to somewhat modify the workflow - rather than expecting a video to be processed immediately, you need to:

    • Request the video conversion by creating a new database row
    • Redirect to a page that explains the video creation is in progress
    • Periodically recheck the conversion status using web redirects, AJAX or Web Sockets.

    This approach is very useful for shared hosting where you cannot install your own queue processors. However, if you are on a VPS or cloud system, you may wish to look into Gearman or one of many other queueing systems. They are a bit more complex than the above, but have more features for managing queues of work.

    0 讨论(0)
提交回复
热议问题