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 @   晴云孤魂  阅读(153)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
点击右上角即可分享
微信分享提示