php ffmpeg exec & shell_exec 进程在几秒后停止
php ffmpeg exec & shell_exec process stops after few seconds
我正在使用 PHP 带有简单 html 界面的脚本文件来控制 FFMPEG 进程从浏览器启动和停止,脚本目标是在我的服务器上开始直播,通常 运行s 几个小时不停(使用 ffmpeg 和 nginx-rtmp )
我的脚本运行良好,直到我最近注意到这是奇怪的行为
这是我的 php 脚本变量
$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 ) ;
这就像 FFMPEG 进程一直持续到原始 php 进程(称为它)死掉,起初我认为这个问题与 sorce 或 ffmpeg 命令有关,但我在 sell 上测试了相同的命令并且它工作得很好.
我怀疑 STDIO 等没有正确重定向。即使我从 shell 执行相同的 php 脚本,它也会在几秒钟后停止。
=编辑=
即使我尝试从命令行 运行 ffmpeg
并使其在后台 运行 ,我也有同样的行为,进程在几秒钟后停止,ffmpeg 继续 运行只有在我等待它输出时才会出现。
这是我的 OS 详细信息:-
DISTRIB_DESCRIPTION="Ubuntu 18.04.3 LTS"
NAME="Ubuntu"
VERSION="18.04.3 LTS (Bionic Beaver)"
CLI 上的 PHP 和 FPM 或 Apache 模块中的 PHP 具有不同的配置 (php.ini)
因此,在您的情况下,您需要针对正确的环境修改 max_execution_time。
您可以在相应的 php.ini 或 ini_set() 或 set_time_limit()
中设置它
编辑:
也许您想切换到 exec()
但请记住:
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.
好吧好吧,经过几天的尝试和调查,这个问题看起来主要问题出在 ffpmeg
上,出于未知的原因,我拒绝 运行 而不定义任何输出管道,我猜是输入Stram 有时会掉帧。无论如何,这是我添加到 ffmpeg 命令末尾的内容
$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 ) ;
我不得不像这样重定向 ffmpeg
到日志文件 </dev/null >/dev/null 2>/var/www/vlc10/ffmpeg.log &
虽然 ffmpeg
有静默模式,但它不会工作 "or at least crashed after few seconds "。
我正在使用 PHP 带有简单 html 界面的脚本文件来控制 FFMPEG 进程从浏览器启动和停止,脚本目标是在我的服务器上开始直播,通常 运行s 几个小时不停(使用 ffmpeg 和 nginx-rtmp ) 我的脚本运行良好,直到我最近注意到这是奇怪的行为 这是我的 php 脚本变量
$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 ) ;
这就像 FFMPEG 进程一直持续到原始 php 进程(称为它)死掉,起初我认为这个问题与 sorce 或 ffmpeg 命令有关,但我在 sell 上测试了相同的命令并且它工作得很好. 我怀疑 STDIO 等没有正确重定向。即使我从 shell 执行相同的 php 脚本,它也会在几秒钟后停止。
=编辑=
即使我尝试从命令行 运行 ffmpeg
并使其在后台 运行 ,我也有同样的行为,进程在几秒钟后停止,ffmpeg 继续 运行只有在我等待它输出时才会出现。
这是我的 OS 详细信息:-
DISTRIB_DESCRIPTION="Ubuntu 18.04.3 LTS"
NAME="Ubuntu"
VERSION="18.04.3 LTS (Bionic Beaver)"
PHP 和 FPM 或 Apache 模块中的 PHP 具有不同的配置 (php.ini)
因此,在您的情况下,您需要针对正确的环境修改 max_execution_time。 您可以在相应的 php.ini 或 ini_set() 或 set_time_limit()
中设置它编辑: 也许您想切换到 exec() 但请记住:
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.
好吧好吧,经过几天的尝试和调查,这个问题看起来主要问题出在 ffpmeg
上,出于未知的原因,我拒绝 运行 而不定义任何输出管道,我猜是输入Stram 有时会掉帧。无论如何,这是我添加到 ffmpeg 命令末尾的内容
$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 ) ;
我不得不像这样重定向 ffmpeg
到日志文件 </dev/null >/dev/null 2>/var/www/vlc10/ffmpeg.log &
虽然 ffmpeg
有静默模式,但它不会工作 "or at least crashed after few seconds "。