我在 Bash 脚本中创建了一个无限循环以重新开始流式传输。
#!/bin/bash
while true
do
ffmpeg -re -rtsp_transport tcp -i "rtsp://{address:port}/{key}" -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -c:a libmp3lame -ab 128k -ar 44100 -c:v copy -threads 2 -bufsize 512k -f flv "rtmps://{address:port}/live/{key}" &
PID=$!
sleep 12h
kill $PID
sleep 2
done
但有时会出现错误……在控制台中以如下形式显示:rtsp://{address:port}/{key}: Invalid date found when processing input
而这一次流不起作用。如何补充我的脚本,它会在控制台中运行命令输出后以及何时出现错误 时进行跟踪处理输入时发现无效日期将结束该过程并立即重新启动流?
最佳答案
如果 ffmpeg 没有在后台启动,这会容易得多:您只需要检查退出状态。
但是因为我们要处理多个进程,所以我们必须合并一些进程间通信。
使用信号:这是 未经测试 :
#!/bin/bash
# set up a signal handler for SIGUSR1
trap 'echo "ffmpeg died!"; exit 1' USR1
while true; do
( # start a subshell
# if ffmpeg exits non-zero, signal the parent shell
ffmpeg -options... || kill -s USR1 $$
) &
# rest of loop is the same
done
关于Bash 输出跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68470315/