Exec in background with ffmpeg

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
misteryyy
Forum Newbie
Posts: 5
Joined: Mon Mar 03, 2008 11:45 am
Location: Czech republic

Exec in background with ffmpeg

Post by misteryyy »

Hi, I am trying to encode video files with ffmpeg. I have made ajax progress bar which read data from ffmpeg output.
I have problem that I can't find the way for run cmd in background in web browser.

This cmd creates two files, one is ffmpeg log and one is file with PID. Could you help me to find out how I can run this process in background?
I am doing it whole day and I am little crazy from that, because it's part of my Bachelor project :)
Thanks for help Misteryyy.

Code: Select all

 $cmd = "ffmpeg -i $dir -b 1000k -vcodec libtheora -acodec libvorbis -ab 160000 -g 30 -s 640x360 -aspect 16:9 $this->dir/ogg/19_ogg_360.ogv";
  exec(sprintf("%s 2> %s & echo $! >> %s", $cmd, $this->ffmpeg_log , $this->ffmpeg_log.'.pid'));
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Exec in background with ffmpeg

Post by Christopher »

What OS is the server running? It appears to be Unix/Linux. If you have access to the server then either a cron reading a job queue or something like Gearman.
(#10850)
misteryyy
Forum Newbie
Posts: 5
Joined: Mon Mar 03, 2008 11:45 am
Location: Czech republic

Re: Exec in background with ffmpeg

Post by misteryyy »

I am testing it on Ubuntu. Production server will be Gentoo. I don't know why the web browser is waiting, because i use &.

edit : I am not good at Linux. But I think that cron is for start process at exact time. I need to create this workflow.
1.User upload video file
2. Choose output video format
3. Click on button a the decode proccess will start
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Exec in background with ffmpeg

Post by Christopher »

I was proposing creating an asynchronous implementation. With either a cron or Gearman the user would upload the file and it would be put on a queue. A cron could run every minute, read the queue to check for pending jobs, and process accordingly. Gearman would be a more elegant solution. Either way the user would receive a notification, showing the status of the video on their account or via email, once the file was processed.
(#10850)
misteryyy
Forum Newbie
Posts: 5
Joined: Mon Mar 03, 2008 11:45 am
Location: Czech republic

Re: Exec in background with ffmpeg

Post by misteryyy »

In the end I am using

Code: Select all

exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $this->ffmpeg_log, $this->ffmpeg_log . '.pid')); 
.
I have Zend Controller which comunnitates through Ajax requests and every 5sec reads ffmpeg_log and from values creates progress bar.
I need to decode 3 format (ogv,webm,mp4) at one time. So it has delay about 1-2 sec per request but it works :). I have 14 days to finish my bachelour thesis so I don't have enought time for other experiments but cron seems like reasonable solution but I am not very good with linux. Thanks for help.
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: Exec in background with ffmpeg

Post by incubi »

Just a quick note here's another way to do it.

http://nsaunders.wordpress.com/2007/01/ ... ss-in-php/

Seems to work well.
Post Reply