ffmpeg设置avformat_open_input( )超时 -stimeout

ffmpeg用avformat_open_input()解析网络流时,默认是阻塞的。

当遇到解析错误的网络流时,会导致该函数长时间不返回。

为此可以设置ffmpeg的-stimeout 的参数,要注意 -stimeout的单位是us 微妙。

用法就是设置在解析的 url 之前 (这里设置超时为5秒)即:

 “ffmpeg -stimeout 5000000  -i rtsp://admin:admin@192.168.7.102:554/h264...........”

或者:

“ffmpeg -stimeout 5000000 -rtsp_transport tcp  -i rtsp://admin:12345@172.16.7.166:554/h264.....”

 

备注:-stimeout 设置在你要解析的url之后是没有作用的。

=============================================

从网络上搜索到的有关资料:

一:

去看ffmpeg的tcp.c的源代码(会阻塞的网络地址基本都是tcp协议),搜索上面两个关键字,就明白是如何退出了。我之前的文章只说了open的时候阻塞,其实网络环境复杂,有可能在read或write时候断网或阻塞异常。但根基却是在tcp.c这个文件里。把interrupt_callback和timeout设置好了,就可以解决这些问题。

二:

ffmpeg的avformat_open_input()默认是阻塞的,用户可以通过设置“ic->flags |= AVFMT_FLAG_NONBLOCK;”设置成非阻塞(通常是不推荐的);或者是设置timeout设置超时时间;或者是设置interrupt_callback定义返回机制。

附一段代码参考下吧。

    ic = avformat_alloc_context();
    if(ic == NULL){
        ERR("error avformat_alloc_context \n");
        return -1;
    }
    ic->interrupt_callback.callback = tvie_decode_interrupt_cb;
    ic->interrupt_callback.opaque = handle;


    //ic->flags |= AVFMT_FLAG_NONBLOCK;


    if(strncmp(handle->params.path, "rtmp:", sizeof("rtmp:")) == 0){
        av_dict_set(&opts, "timeout", "6", 0); // in secs


    }
    else if(strncmp(handle->params.path, "http:", sizeof("http:")) == 0){
        av_dict_set(&opts, "timeout", "6000", 0); // in ms
    }

    err = avformat_open_input(&ic, handle->params.path, NULL, &opts);

转载于:https://www.cnblogs.com/wainiwann/p/4218341.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值