views:

361

answers:

4

hy,

in my script i run a exec() function to make a movie file with ffmpeg.

the problem is ffmpeg can run only 1 time on the server,

if 2 people are online on server and first one already run ffmpeg i want the second to wait until the first end the process

how to code this?

thank you

+1  A: 

For the love of Jebus, don't implement your own queue. Write a daemon that picks up messages and processes the files.

Ignacio Vazquez-Abrams
+1 for dragging Jebus into this :)
Traveling Tech Guy
i can't compile and install librabbitmq! i don't have access to root server
robert
+2  A: 

Set a lock somewhere.

When ffmpeg starts, set a flag in a file or a database table to mark it as in progress. Every time ffmpeg starts, have it check if the flag value is '1', and if so, wait and retry in 5 or 10 seconds. Then when the process ends, set the flag to '0'. Make sure if it crashes or your script has an error that the flag is set back to '0'.

Or, you could likely run the ffmpeg process as a different user, and have more than one running at once.

Alex JL
how to run a process as a different user from php?
robert
This can work and is quick, but way not efficient. On shared hosting you will get banned for sure when doing stuff like this.
Alfred
@robert if you're on a shared host, most likely you can't run something as another user unless you have multiple accounts.
Alex JL
@Alfred I agree it's quick and drity and not exactly an enterprise level solution. Unless you have 10 people waiting in your queue I don't see what they're going to ban you for. Most likely your host would set resource limits of some sort instead? Anyhow I think if you were at that point it would be best to get a virtual server, yes. Most likely a shared host wouldn't like non-stop video encoding occurring.
Alex JL
A: 

You can use an asynchronous queue. Every new request is added to the queue as a message. Your process reads one message at a time and handles the convert request.

For a PHP queue, you might try Dropr or similar.

Traveling Tech Guy
A: 
  1. Way aren't you using video hosting service(free) like for example youtube/vimeo for this which are way better prepared for handling this then you could ever be. Youtube has an API for all kinds of stuff.
  2. Are you running exec from shared hosting? If so be warned to get banned because shared hosting does not like you to utilize the CPU that way. They want short Http requests(within 30 seconds). The rest does not apply to your hosting you should upgrade first.
  3. Like Ignacio said you should install a message queue(need linux box with shell access) like for example:

    1. redis If you have a linux box with ssh access the way easiest to install would be redis. Then your long-running fffmpeg process(php script) should connect to redis and fetch messages from queue one by one when ready with processing of video. The client should just simply put message on queue. I would advise to watch this quick video to get the basics of redis.
    2. beanstalkd
    3. gearman

Hopefully this helps you a little bit. I also am writing a simple task queue in java, but I am not completely finished yet. hopefully in a about a week I can submit my first version of it on github.

Alfred
thanks and good luck
robert