本文共 2418 字,大约阅读时间需要 8 分钟。
使用ffmpeg类库进行开发的时候,打开流媒体(或本地文件)的函数是avformat_open_input()。
其中打开网络流的话,前面要加上函数avformat_network_init()。
一般情况下,只要传入流媒体的url就可以了。但是在打开某些流媒体的时候,可能需要附加一些参数。
例如在播放中央人民广播电台的声音信号的时候,其url为“rtsp://mms.cnr.cn/cnr003?MzE5MTg0IzEjIzI5NjgwOQ==”
如果直接进行打开是不会成功的,我们可以使用ffplay做一下实验:
1 | ffplay |
Invalid data found when processing input
这时候我们需要指定其传输方式为TCP,需要将命令改为如下形式:
1 | ffplay -rtsp_transport tcp |
此外还可以附加一些参数,比如
1 | ffplay -rtsp_transport tcp -max_delay 5000000 |
看一下avformat_open_input()的定义:
01 | /** |
02 |
* Open an input stream and read the header. The codecs are not opened. |
03 |
* The stream must be closed with av_close_input_file(). |
04 |
* |
05 |
* @param ps Pointer to user-supplied AVFormatContext (allocated by avformat_alloc_context). |
06 |
* May be a pointer to NULL, in which case an AVFormatContext is allocated by this |
07 |
* function and written into ps. |
08 |
* Note that a user-supplied AVFormatContext will be freed on failure. |
09 |
* @param filename Name of the stream to open. |
10 |
* @param fmt If non-NULL, this parameter forces a specific input format. |
11 |
* Otherwise the format is autodetected. |
12 |
* @param options A dictionary filled with AVFormatContext and demuxer-private options. |
13 |
* On return this parameter will be destroyed and replaced with a dict containing |
14 |
* options that were not found. May be NULL. |
15 |
* |
16 |
* @return 0 on success, a negative AVERROR on failure. |
17 |
* |
18 |
* @note If you want to use custom IO, preallocate the format context and set its pb field. |
19 |
*/ |
20 | int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options); |
设置AVDictionary的时候会用到av_dict_set()。
下面看看把命令
1 | ffplay -rtsp_transport tcp -max_delay 5000000 |
转化为代码实现的方式:
01 | AVFormatContext *pFormatCtx; |
02 | pFormatCtx = avformat_alloc_context(); |
03 | ...代码略 |
04 | AVDictionary *avdic=NULL; |
05 | char option_key[]= "rtsp_transport" ; |
06 | char option_value[]= "tcp" ; |
07 | av_dict_set(&avdic,option_key,option_value,0); |
08 | char option_key2[]= "max_delay" ; |
09 | char option_value2[]= "5000000" ; |
10 | av_dict_set(&avdic,option_key2,option_value2,0); |
11 | char url[]= "" ; |
12 |
13 | avformat_open_input(&pFormatCtx,url,NULL,&avdic); |
ffplay rtsp://218.204.223.237:554/live/1/66251FC11353191F/e7ooqwcfbqjoo80j.sdp
转载地址:http://cifhj.baihongyu.com/