I have tried the following:
$ffmpeg = "/path/to/ffmpeg";
$source = "/path/to/video/source.mp4";
$dest = "/path/to/video/dest.mp4";
$format = "%s > %s 2>&1 & echo $! >> %s";
$a_rate = "44100";
$a_bitrate = "96k";
$v_bitrate = "3000k";
$resolution = "1280x720";
$outputfile = "/path/to/output.txt";
$pidfile = "/path/to/pid.txt";
exec(sprintf($format, $ffmpeg . " -i " . $source . " -vcodec libx264 -s " . $resolution . " -b " . $v_bitrate . " -pass 1 /dev/null && \ " . $ffmpeg . " -i " . $source . " -acodec libfaac -ar " . $a_rate . " -ab " . $a_bitrate . " -vcodec libx264 -s " . $resolution . " -b " . $v_bitrate . " -pass 2 " . $dest, $outputfile, $pidfile));
Everything works perfectly if I do a single pass, but trying to do two passes like that doesn’t work. What is the proper way to do it?
Update:
exec(sprintf($format, $ffmpeg . " -i " . $source . " -pass 1 -vcodec libx264 -s " . $resolution . " -b " . $v_bitrate . " -passlogfile " . $pass . " -f rawvideo -y /dev/null", $outputfile, $pidfile));
exec(sprintf($format, $ffmpeg . " -y -i " . $source . " -pass 2 -acodec libfaac -ar " . $a_rate . " -ab " . $a_bitrate . " -vcodec libx264 -s " . $resolution . " -b " . $v_bitrate . " -passlogfile " . $pass . " " . $dest, $outputfile, $pidfile));
The issue was that putting the ffmpeg commands in the background with
$format = "%s > %s 2>&1 & echo $! >> %s";caused them both to be run at the same time and at that time there is no log file finished for the second pass to use. Backgrounding works fine with single pass because it has no dependencies, but I suppose a more involved solution is required to background two pass conversions. I am planning on using a queuing system.