Search code examples
phpffmpeg

Php exec ffmpeg not running in background


I am trying to start a ffmpeg process from a php script and I know it has been asked a lot of times but I tried many solutions and none of them seem to work, each time the php script never finishes unless I kill the ffmpeg process. At the moment I am using this script which indeed starts ffmpeg and writes info in the designated files but the php script is loading forever.

What am I missing?

$cmd = 'cd cache && ffmpeg -y -i "rtsp://stream" -r 20 -f image2 a%6d.jpg >/dev/null 2>/dev/null &';

exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, 'log.txt', 'error.txt' . '.pid')); 

A little more info: I am running FFMpeg 0.6.5, PHP 5.3.3 on CentOS 6.5

Thank you for your time!


Solution

  • You can use > /dev/null & instead of 2>&1 &

    This will execute $cmd in the background without PHP waiting for it to finish.

    Ref: http://php.net/manual/en/function.exec.php