6

I am working on developing a video streaming site where users can upload videos to the site (multiple videos at once using the uploadify jquery plugin).

Now, I am faced with the question of encoding the videos to FLV for streaming them online.

When should the video encoding process take place ? Should it take place immediately after uploads have finished (i.e redirect the user to upload success page, and then start encoding in the background using exec command for ffmpeg ?) However, using this approach, how do i determine if the encoding has finished successfully ? What if users upload a corrupt video and ffmpeg fails to encode it ? How do i handle this in PHP ?

How do i queue encoding of videos since multiple users can upload videos at the same ? Does FFMpeg has its own encoding queue ?

I also read about gearman and message queueing options such as redis and AMQP in another related SO thread. Are these one of the potential solutions ?

I would really appreciate if someone could give answers to my questions.

Emii Khaos
  • 9,983
  • 3
  • 34
  • 57
YD8877
  • 10,401
  • 20
  • 64
  • 92

4 Answers4

3

You should use a software called gearman. It is a job server and you can call functions using PHP. You can transfer the process to background and it automatically handles queuing. Many processes can be run parallel also.

I've used it and it is very easy to install and operate.

For your use case,

Wrap the ffmpeg process in a php file with exec.
Save the uploaded file to db and give it an id. 
Call the encode function after a user uploads file.
As soon as the encode function starts, 
update the db to reflect that the file was "picked".
First run the ffmpeg info on the file to see if it is fine.
Then encode the file and after it is done, update db flag to "done".

After the process is done, you can call another function to send an email, tweet etc to the user that the encoding is complete.

Mridul Kashatria
  • 4,157
  • 2
  • 18
  • 15
  • Thanks for your answer mridkash. However, as you said "Then encode the file and after it is done, update db flag to 'done'.", how do i know that the encoding is "done" ? – YD8877 Jun 18 '11 at 19:04
  • when the php exec(ffmpeg) call ends there are 2 possibilities, either the processing is complete, or it failed. So after the exec call you can check for existence of encoded file, maybe also run ffmpeg info on it and then update the db flag to "done". – Mridul Kashatria Jun 19 '11 at 02:40
  • according to your comment, "when the php exec(ffmpeg) call ends". How do i determine when the ffmpeg calls has ended. I could always do a periodic check on the file but it can be present there even before the encoding has ended. – YD8877 Jun 20 '11 at 06:28
  • php is synchronous, so when you call exec() with a command, it will stop there till the command ends. A quick example is `` – Mridul Kashatria Jun 20 '11 at 09:19
2

Since encoding may take some time you may want to add the input files in a folder and add an entry in a database. Then you have a script that runs either constantly or each x minutes that convert the pending videos from the database to FLV format.

To queue them you will need to make a custom script that re-run FFMpeg for each files.

maaudet
  • 2,338
  • 4
  • 20
  • 28
2

You could use a cron job on the server, or use something like beanstalkd (or gearman as mentioned in other answers to this question).

Gaz
  • 1,570
  • 2
  • 17
  • 22
1

FFMpeg is just a command line utility. It doesn't have any queue and you will need to build your own queuing system to perform the work asynchronously.

For PHP queues, I had a good experience with the beanstalkd server, using the pheanstalk PHP client. Your upload script should then insert items to the queue for encoding, and return a response to the user saying the video will be processed shortly. Your workers should fetch items from the queue and run FFMpeg on them. You can read the FFMpeg status code to figure whether the encoding was successfully completed.

sagi
  • 5,619
  • 1
  • 30
  • 31