FFmpeg works on terminal not with PHP exec
Matthew Harrington
If I execute a Ffmpeg command from terminal, I get the desired result:
ffmpeg -i src.mp4 -ar 22050 -ab 32 -f flv -s 320x240 video.flvTerminal's output:
...
video:3404kB audio:1038kB global headers:0kB muxing overhead 2.966904%And video.flv is created correctly.
Then, if called via PHP exec:
exec("ffmpeg -i src.mp4 -ar 22050 -ab 32 -f flv -s 320x240 video.flv", $o, $v);
var_dump($o);
var_dump($v);The output is this:
array(0) { } int(1)And no file is created. Any thoughts on how to approach this?
I can exec('whoami') with no problems and I have used the FFmpeg full path as well: /usr/local/bin/ffmpeg
4 Answers
The problem is that you use exec instead of shell_execthe point is that environement of exec don't know about any FFmpeg executable, but shell_exec* does, becuase it uses env. of the bash/shell
Ths solution is to use the full path to FFmpeg executable, eg. /usr/bin/ffmpeg
add 2>&1 to the end of the command and it will work:
exec("ffmpeg -i src.mp4 -ar 22050 -ab 32 -f flv -s 320x240 video.flv 2>&1", $o, $v); In addition to using the full path for ffmpeg, use /full/path/to/src.mp4 and /full/path/to/video.flv
I has the same problem, first make sure that the ffmpeg.exe or directory is in the windows path, then restart your apache server to ensure that php found the ffmpeg command. After this i can use ffmpeg with both options exec and shell_exec