How to convert video in the background using PHP a

2020-02-16 06:05发布

问题:

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 research with no luck as to where I should get started.

Basically what I want to do is allow the user to upload the video and then display a message that says video is being processed and you will be notified when available. In the meantime I want the video to be converted behind the scenes and allow the user to leave the page or even close the browser. I am using a Windows server.

How can I accomplish this?

回答1:

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.