Linux、Cygwin/MSYS2利用ffmpeg测试流媒体链接可用性

以下函数为Cygwin或MSYS2下调用Windows版本的ffmpeg,故使用了cygpath转换Windows和Linux二者的路径参数,如果需要在其他平台下运行,需要调整相关路径转换规则

支持测试 http、https、rtmp、rtsp、rtp格式的连接地址;

ffmpeg-test函数代码:

ffmpeg-test() {
    ## 利用ffmpeg测试流媒体链接可用性
	if [ $# -eq 1 ] && [[ "$1" =~ ^(http|https|rtmp|rtsp|rtp):// ]];then
		local playURL="$1"
	else
		print_color 40 "参数无效,\$1 请传递有效的媒体流链接(http/https/rtmp/rstp)!"
		return
	fi
	local tmpMP4="$(cygpath -aw /tmp/ffmpeg-test.mp4)"
	print_color "测试媒体流,请稍等..."
	#3秒超时
	ffmpeg -rw_timeout 3000000 -i "$playURL" -t 1 -y "$tmpMP4" &>/dev/null
	local retCode=$?
	[ $retCode -eq 0 ] && print_color 33 "流媒体链接测试成功!"
	[ -f "$tmpMP4" ] && rm -f "$tmpMP4"
	return $retCode
}

调用示例:

在终端中调用

ffmpeg-test rtmp://xxxx.eample.com/live/xxxx

在脚本中调用

ffmpeg-test rtmp://xxxx.eample.com/live/xxxx && echo "测试成功" || echo "测试失败"
#---- OR ----
ffmpeg-test rtmp://xxxx.eample.com/live/xxxx &>/dev/null
if [ $? -eq 0 ];then
    echo "测试成功"
else
    echo "测试失败"
fi

使用截图:

posted @ 2022-09-29 12:52  晴云孤魂  阅读(136)  评论(0编辑  收藏  举报