Salem
Salem

Reputation: 774

php ffmpeg exec & shell_exec process stops after few seconds

I'm using PHP script file with simple html interface to control FFMPEG process start and stop from the browser , the script goal is start live streaming on my server that usually runs for hours without stop (using ffmpeg and nginx-rtmp ) my script were working perfectly until I notice recently This is strange behaviors here is my php script variables

$cast =" /usr/sbin/ffmpeg -loglevel 0 -thread_queue_size 32768 -re -i '".$src."' -i /var/www/example/logo.png -r 23.976 -strict -2 480x360 -aspect 16:9 -filter_complex 'overlay=x=(main_w-overlay_w)/2:y=(main_h-overlay_h)-23' -vcodec libx264 -x264opts colormatrix=bt709 -profile:v high444 4 -b:v 290k -maxrate 290k -bufsize 250k  -af "aresample=async=1:min_hard_comp=0.100000:first_pts=0" -acodec libfdk_aac -profile:a aac_he_v2 -b:a 16k -map_metadata -1 -f flv  rtmp://localhost/hls/live 2>/dev/null >/dev/null  & " ; 
$output =  shell_exec( $cast   )    ;

It's like FFMPEG process continue until original php process ( that call it ) die , at first I thought this issue with the sorce or ffmpeg command but I test the same command on the sell and it works perfectly . My suspicion are with on STDIO etc were not redirected right . even when I excute the same php script from the shell it's do the same stops after few seconds .

=Edit=

Even when I tried to run ffmpeg from the command line and make it run on the background , I got same behavior the process stop after few seconds , ffmpeg continue running only if I wait for it output .

Here my OS details :-

DISTRIB_DESCRIPTION="Ubuntu 18.04.3 LTS"
NAME="Ubuntu"
VERSION="18.04.3 LTS (Bionic Beaver)"

Upvotes: 5

Views: 1745

Answers (2)

Salem
Salem

Reputation: 774

Alright alright after days of trying and invetstigation the issue looks like the main issue were with ffpmeg for unowen reason for me refuse to run without defining any output pipe , my guessing is the input stram had some frames dropped from time to time . anyway this is what I add to end of ffmpeg command

$cast ="< /dev/null /usr/sbin/ffmpeg -loglevel verbose -thread_queue_size 12768 -re -i \"$link/$chnl\" -r 23.976 -s 480x360  -vcodec libx264 -b:v $bitrate -minrate $bitrate -maxrate $bitrate -bufsize $bitrate -acodec aac -b:a 29k -map_metadata -1 -f flv rtmp://localhost/hls/live  </dev/null >/dev/null 2>/var/www/vlc10/ffmpeg.log & " ;
exec(   $cast   ) ; 

I was have to redirect ffmpeg to log file like this </dev/null >/dev/null 2>/var/www/vlc10/ffmpeg.log & eve ffmpeg had silent mode but it wont work "or at least crashed after few seconds ".

Upvotes: 4

DragonSGA
DragonSGA

Reputation: 340

PHP on CLI and PHP as FPM or Apache Module have different configs (php.ini)

So in your case you need to modify the max_execution_time for the right environment. You can set it in the corresponding php.ini or with ini_set() or with set_time_limit()

EDIT: Maybe you want to switch to exec() But please keep in mind:

If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

And please use escapeshellarg()

Upvotes: 0

Related Questions