FFMPEG类库打开流媒体的方法(传参数)
使用ffmpeg类库进行开发的时候,打开流媒体(或本地文件)的函数是avformat_open_input()。
其中打开网络流的话,前面要加上函数avformat_network_init()。
一般情况下,只要传入流媒体的url就可以了。但是在打开某些流媒体的时候,可能需要附加一些参数。
例如在播放中央人民广播电台的声音信号的时候,其url为“rtsp://mms.cnr.cn/cnr003?MzE5MTg0IzEjIzI5NjgwOQ==”
如果直接进行打开是不会成功的,我们可以使用ffplay做一下实验:
- ffplay rtsp://mms.cnr.cn/cnr003?MzE5MTg0IzEjIzI5NjgwOQ==
会出现错误:
Invalid data found when processing input
这时候我们需要指定其传输方式为TCP,需要将命令改为如下形式:
- ffplay -rtsp_transport tcp rtsp://mms.cnr.cn/cnr003?MzE5MTg0IzEjIzI5NjgwOQ==
附加了参数以后,发现就可以正常播放了。
此外还可以附加一些参数,比如
- ffplay -rtsp_transport tcp -max_delay 5000000 rtsp://mms.cnr.cn/cnr003?MzE5MTg0IzEjIzI5NjgwOQ==
在使用FFMPEG类库进行编程的时候,如何将这些附加的参数传递给avformat_open_input()呢?经过研究后发现,可以通过AVDictionary把参数传给avformat_open_input()。
看一下avformat_open_input()的定义:
- /**
- * Open an input stream and read the header. The codecs are not opened.
- * The stream must be closed with av_close_input_file().
- *
- * @param ps Pointer to user-supplied AVFormatContext (allocated by avformat_alloc_context).
- * May be a pointer to NULL, in which case an AVFormatContext is allocated by this
- * function and written into ps.
- * Note that a user-supplied AVFormatContext will be freed on failure.
- * @param filename Name of the stream to open.
- * @param fmt If non-NULL, this parameter forces a specific input format.
- * Otherwise the format is autodetected.
- * @param options A dictionary filled with AVFormatContext and demuxer-private options.
- * On return this parameter will be destroyed and replaced with a dict containing
- * options that were not found. May be NULL.
- *
- * @return 0 on success, a negative AVERROR on failure.
- *
- * @note If you want to use custom IO, preallocate the format context and set its pb field.
- */
- int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options);
/**
* Open an input stream and read the header. The codecs are not opened.
* The stream must be closed with av_close_input_file().
*
* @param ps Pointer to user-supplied AVFormatContext (allocated by avformat_alloc_context).
* May be a pointer to NULL, in which case an AVFormatContext is allocated by this
* function and written into ps.
* Note that a user-supplied AVFormatContext will be freed on failure.
* @param filename Name of the stream to open.
* @param fmt If non-NULL, this parameter forces a specific input format.
* Otherwise the format is autodetected.
* @param options A dictionary filled with AVFormatContext and demuxer-private options.
* On return this parameter will be destroyed and replaced with a dict containing
* options that were not found. May be NULL.
*
* @return 0 on success, a negative AVERROR on failure.
*
* @note If you want to use custom IO, preallocate the format context and set its pb field.
*/
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options);
可以看出avformat_open_input()的第4个参数是一个AVDictionary类型的参数。这个参数就是传入的附加参数。
设置AVDictionary的时候会用到av_dict_set()。
下面看看把命令
- ffplay -rtsp_transport tcp -max_delay 5000000 rtsp://mms.cnr.cn/cnr003?MzE5MTg0IzEjIzI5NjgwOQ==
转化为代码实现的方式:
- AVFormatContext *pFormatCtx;
- pFormatCtx = avformat_alloc_context();
- ...代码略
- AVDictionary *avdic=NULL;
- char option_key[]="rtsp_transport";
- char option_value[]="tcp";
- av_dict_set(&avdic,option_key,option_value,0);
- char option_key2[]="max_delay";
- char option_value2[]="5000000";
- av_dict_set(&avdic,option_key2,option_value2,0);
- char url[]="rtsp://mms.cnr.cn/cnr003?MzE5MTg0IzEjIzI5NjgwOQ==";
- avformat_open_input(&pFormatCtx,url,NULL,&avdic);
FFMPEG类库打开流媒体的方法(传参数)的更多相关文章
- FFMPEG类库打开流媒体的方法(需要传参数的时候)
使用ffmpeg类库进行开发的时候,打开流媒体(或本地文件)的函数是avformat_open_input(). 其中打开网络流的话,前面要加上函数avformat_network_init(). 一 ...
- (转)FFMPEG类库打开流媒体的方法(需要传参数的时候)
本文链接:https://blog.csdn.net/leixiaohua1020/article/details/14215393 使用ffmpeg类库进行开发的时候,打开流媒体(或本地文件)的函数 ...
- 【FFMPEG】使用ffmpeg类库打开流媒体
版权声明:本文为博主原创文章,未经博主允许不得转载. 使用ffmpeg类库进行开发的时候,打开流媒体(或本地文件)的函数是avformat_open_input(). 其中打开网络流的话,前面要加上函 ...
- Java 常用类库一,main方法传参String[] args;获取输入Scanner ;hasNext();hasNextInt()
1. main方法传参 package com.zmd.common_class_libraries; /** 给mian方法传参测试 */ public class MainArgsTest { p ...
- javascript 利用匿名函数对象给你异步回调方法传参数
先来创建一个匿名函数对象: /*** * 匿名函数 */ var callChangeBtn=new function(bugBtn){ this.chage=function(json){ bugB ...
- 如何给run()方法传参数
实现的方式主要有三种 1.构造函数传参 2.成员变量传参 3.回调函数传参 问题:如何实现处理线程的返回值? 1.主线程等待法(优点:实现起来简单,缺点:需要等待的变量一多的话,代码就变的非常臃肿.而 ...
- XMLHTTP中setRequestHeader方法和参数
注意:在FF里面需要将open方法放在setRequestHeader之前 一.为何要用到setRequestHeader 通 常在HTTP协议里,客户端像服务器取得某个网页的时候,必须发送一个HTT ...
- ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml) 用javascript在客户端删除某一个cookie键值对 input点击链接另一个页面,各种操作。 C# 往线程里传参数的方法总结 TCP/IP 协议 用C#+Selenium+ChromeDriver 生成我的咕咚跑步路线地图 (转)值得学习百度开源70+项目
ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml) 我们都知道在使用WebApi的时候Controller会自动将Action的返回值自动进行各种序列化处理(序列化为 ...
- 【ASP.NET Core】给中间件传参数的方法
最近博客更新频率慢了些,原因有三: 其一,最近老周每星期六都录 ASP.NET Core 的直播,有些内容在视频里讲过,就不太想在博客里面重复.有兴趣的话可以去老周的微博看,或者去一直播,直播帐号与微 ...
随机推荐
- CSU 1102 多连块拼图
多连块拼图 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 多连块是指由多个等大正方形边与边连接而成的平面连通图形. ———— 维基百科 ...
- 用户代码未处理EntityCommandExecutionmException报错解决方案
原因可能是(1)没有编译好,清理解决方案,重新生成解决方案. (2)可能是WebSiteConfiguration.DbProviderName;中为DbProviderName属性 ...
- javascript copy text to clipboard
本段代码摘自微软docs网站上,目前需要解决在IE浏览器中触发copy事件的方法,也可以直接调用jquery. <!DOCTYPE html> <html> <head& ...
- spring_150908_hibernate_id_sequence
1.新建java工程:spring_150908_hibernate_id_sequence,添加相关jar包(spring.hibernate.ibatis)如下图所示: 2.实现实体类DogPet ...
- laravel windows安装(composer)
1.安装composer参考windows 安装tp5 composer方式 2.先配置好本地虚拟域名,在cmd里面切换到网站根目录 ... 3.安装成功之后,在浏览器输入已配置的虚拟域名我的是,la ...
- html5一些容易忽略的细节
最近由于经常写前端,所以系统性的看了一下html5页面的基础信息,虽然以前写了很久的html代码,但是其中的一些细节还是容易被忽略,所以这里一起整理一下. 在html5中,空元素结尾处的空格和斜杠是可 ...
- TensorFlow——module 'tensorflow' has no attribute 'xxx'
tf.sub()更改为tf.subtract() tf.mul()更改为tf.multiply() tf.types.float32更改为tf.float32 tf.pact()更改为tf.stact ...
- /etc/default/useradd配置文件详解
/etc/default/useradd文件内容如下: [xf@xuexi ~]$ cat /etc/default/useradd # useradd defaults file GROUP=100 ...
- Cable master POJ - 1064
Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Commi ...
- 初始Hibernate4
Hibernate是一个ORM的轻量级框架,解决持久化操作,使得程序员可以从编写繁复的jdbc的工作中解放出来,专注与业务,提高我们的开发效率.移植性. 1.持久化 a) 侠义概念:数据存储在物 ...