我正在使用 rtmpdump 在 nginx rtmp 服务器上的本地网络上重新播放实时视频。如下所示:
sudo rtmpdump -r "rtmp://123.45.6.7/live/" -a "live/" -f "LNX 14,0,0,125" -W "http://123.45.6.7/jwplayer.flash.swf[1] " -p "http://123.45.6.7/[2] " --live -y "livestream2" | avconv -i pipe:0 -y
-v:v info -vcodec copy -acodec copy -f flv rtmp://localhost:1935/live/
它本身运行良好,没有问题,但偶尔原始源可能会闪烁,这会导致正在运行的命令退出,我必须手动再次运行该命令。有没有办法编写一个脚本,自动检测 rtmpdump 是否退出并且没有僵尸命令运行并重新运行该命令?我想为大约 4 个直播流自动执行此过程。可能吗?
答案1
你可以编写一个包含以下内容的脚本:
#! /bin/bash
function INT_cleanup()
{
kill `jobs -p`
exit
}
trap INT_cleanup INT
# ${VAR-TEXT} means that TEXT is used if VAR is empty.
STREAM_START=$(($1))
STREAM_END=$(($2))
for ((COUNT=STREAM_START; COUNT<=STREAM_END;COUNT++))
do
while true #Infinite loop
do
rtmpdump -r "rtmp://123.45.6.7/live/" -a "live/" -f "LNX 14,0,0,125" -W \
"http://123.45.6.7/jwplayer.flash.swf[1] " \
-p "http://123.45.6.7/[2] " --live -y "livestream$COUNT" |
avconv -i pipe:0 -y -v:v info -vcodec copy \
-acodec copy -f flv rtmp://localhost:1935/live$COUNT/
done &
done
或者
#! /bin/bash
function INT_cleanup()
{
kill `jobs -p`
exit
}
trap INT_cleanup INT
count=0
while [[ $count < 10 ]] # Try 10 times
do
rtmpdump -r "rtmp://123.45.6.7/live/" -a "live/" -f "LNX 14,0,0,125" -W "http://123.45.6.7/jwplayer.flash.swf[1] " -p "http://123.45.6.7/[2] " --live -y "livestream2" | avconv -i pipe:0 -y -v:v info -vcodec copy -acodec copy -f flv rtmp://localhost:1935/live/
count=$((count + 1))
done
在文件中,调用它stream.sh
,并使其可执行:
chmod +x stream.sh
并运行它:
sudo ./stream.sh 1 3
要终止脚本,请按CtrlC。第一个是无限循环,第二个运行 10 次迭代。
答案2
我找到了这个脚本。我用它重生了 AWN dock。它运行正常,但我建议你在大量使用之前先测试一下
#!/bin/bash
# This script is a simple respawn deamon for those of us who dont want
# to deal with the /etc/event.d, monit etc...
# Usage: sh respawn.sh [program] [sleep time]
while [ true ]
do
sleep $2
if ps ax | grep -v grep | $1 > /dev/null
then
echo $1": Stopped. Restarting in "$2" seconds."
else
$1 &
fi
done