Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php ffmpeg exec & shell_exec process stops after few seconds

Tags:

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)"
like image 377
Salem Avatar asked Sep 30 '19 20:09

Salem


People also ask

How can I call FFmpeg from PHP?

To run an FFmpeg video conversion from PHP, use the shell_exec() PHP utility. Pass the FFmpeg command inside "" (double quotes) and any PHP variables needed inside '' (single quotes).

What is FFmpeg command?

Command line tools ffmpeg is a command-line tool that converts audio or video formats.


1 Answers

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 ".

like image 99
Salem Avatar answered Nov 09 '22 13:11

Salem