0

I am currently trying to convert various video files using FFmpeg with the help of my server (Windows Server 2016). PHP is supposed to initiate the process.

Whenever I start FFmpeg via CMD, everything works fine and the video file converts perfectly. But when I trigger the same process with PHP, FFmpeg aborts the conversion after a while and exits by itself.

I neither get an error in the log nor can I recognize system errors from Windows Server. It doesn't matter if I run the command through shell_exec, exec, or system.

For converting I actually use this code:

ffmpeg -loglevel error -i $video -vf scale=1920:1080 -crf 10 -c:v libx264 -preset veryfast -threads 2 $output > NUL 2>&1 < NUL 

This is my full php code:

    ignore_user_abort(true);
    set_time_limit(0);
    
    error_reporting( E_ALL | E_STRICT );
    
    $path    = 'C:/inetpub/vhosts/confident-tharp.xx-xx-xxx-xxx.plesk.page/httpdocs';
    
    $ffmpeg  = $path . '/ffmpeg/ffmpeg.exe';
    $video   = $path . '/convert/myoldfile.mp4';
    $output  = $path . '/convertedfile.mp4';
    
    $dimension = 'scale=1920:1080';
    
    $command = "ffmpeg -loglevel error -i $video -vf scale=1920:1080 -crf 10 -c:v libx264 -preset veryfast -threads 2 $output > NUL 2>&1 < NUL";

    shell_exec( $command );

I'm currently using FFmpeg 5, but there was no change with FFmpeg 4 either. The use of other codecs or file formats (e.g. .avi) also has no influence on the behavior of FFmpeg. Previously I tried to use the php-ffmpeg library. Again, I get the same "error".

Hope you guys can help.

  • Are you using PHP through your browser or the CLI? My gut is that you are reaching PHP’s (or similar) timeout. If that’s not the case, maybe checkout `proc_open`, not to fix your problem, but it will allow you to capture stderr and stdout for debugging. Also, do you know if your command is properly escaped and therefore valid? Is it possible that FFMpeg is being invoked, but because of escaping it is terminating on its own? Your CLI is correct, just the reinterpretation in PHP might be wrong. – Chris Haas Jan 22 '22 at 12:22
  • Hey Chris, thanks for your reply. I use PHP via my browser because the script has to be started later via Ajax via another server. Unfortunately I don't understand much about proc_open, stderr and stdout. Can you tell me how to use the code? The command should be valid. At least the same command can be used 1:1 via CMD. From there it works perfectly again. – rumpetroll Jan 22 '22 at 12:31
  • So, a short edit from me. When I run PHP with CLI it works perfectely. :/ – rumpetroll Jan 22 '22 at 13:15
  • Maybe checkout your server timeout? You say windows, but I don’t know your specific server. If it is Apache, see this: https://httpd.apache.org/docs/2.4/mod/core.html#timeout – Chris Haas Jan 22 '22 at 13:18
  • Since it is working through the CLI, you don’t necessarily need proc_open, although it is my personally preferred method for executing system code. The [syntax](https://www.php.net/manual/en/function.proc-open.php) is a little ugly and weird, but you get used to it. – Chris Haas Jan 22 '22 at 13:21
  • Hey Chris, thanks again for your reply. If CLI works, does it make more sense to increase the timeout limit in Apache or run the script exclusively via CLI? – rumpetroll Jan 22 '22 at 13:59
  • If this were me, I’d have the client-side submit to the server (AJAX or whatever, doesn’t matter), and have the server submit to a queue (could just be a boring database). Then I’d have a daemon (cron, systemd, service, etc.) read from the queue continuously, process jobs, and write the status back. The client then can report on the queue status and is detached from the commands themselves. If the command break, you can report that back to the client, too. It is a little more work, but honestly not a whole lot, and you get a clean separation of responsibilities. – Chris Haas Jan 22 '22 at 15:54

0 Answers0