(转)FFMPEG类库打开流媒体的方法(需要传参数的时候)

news/2024/7/10 20:45:39 标签: ffmpeg, 网络
本文链接: https://blog.csdn.net/leixiaohua1020/article/details/14215393

使用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()的定义:

  1. /**
  2. * Open an input stream and read the header. The codecs are not opened.
  3. * The stream must be closed with av_close_input_file().
  4. *
  5. * @param ps Pointer to user-supplied AVFormatContext (allocated by avformat_alloc_context).
  6. * May be a pointer to NULL, in which case an AVFormatContext is allocated by this
  7. * function and written into ps.
  8. * Note that a user-supplied AVFormatContext will be freed on failure.
  9. * @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);


可以看出avformat_open_input()的第4个参数是一个AVDictionary类型的参数。这个参数就是传入的附加参数。

设置AVDictionary的时候会用到av_dict_set()。

下面看看把命令

ffplay -rtsp_transport tcp -max_delay 5000000 rtsp://mms.cnr.cn/cnr003?MzE5MTg0IzEjIzI5NjgwOQ==

转化为代码实现的方式:

  1. AVFormatContext *pFormatCtx;
  2. pFormatCtx = avformat_alloc_context();
  3. ...代码略
  4. AVDictionary *avdic=NULL;
  5. char option_key[]="rtsp_transport";
  6. char option_value[]="tcp";
  7. av_dict_set(&avdic,option_key,option_value, 0);
  8. char option_key2[]="max_delay";
  9. char option_value2[]="5000000";
  10. av_dict_set(&avdic,option_key2,option_value2, 0);
  11. char url[]="rtsp://mms.cnr.cn/cnr003?MzE5MTg0IzEjIzI5NjgwOQ==";
  12.  
  13. avformat_open_input(&pFormatCtx,url, NULL,&avdic);

转载于:https://www.cnblogs.com/schips/p/11521769.html


http://www.niftyadmin.cn/n/1492923.html

相关文章

Oracle 导入导出dmp文件

EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用。EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用。IMP只适用于EXP导出的文件,不适用于EXPDP导出文件&#xff1b…

FFplay源代码分析:整体流程图(仅供参考)

转载于:https://www.cnblogs.com/schips/p/11521774.html

海思SDK 与各芯片对照

本文链接:https://blog.csdn.net/liuxizhen2009/article/details/84261221海思SDK不对外开放。海思35系列sdk Hi3507 Hi3507V100R001C01 Hi3511 Hi3507V100R001C01 Hi3515 Hi3515 V100R001C01SPC042 Hi3515 V100R001C01SPC050 Hi3515 V100R001C01SPC090 H…

FFMPEG - ffplay源代码分析

FFmpeg是一个开源,免费,跨平台的视频和音频流方案,它提供了一套完整的录制、转换以及流化音视频的解决方案。而ffplay是有ffmpeg官方提供的一个基于ffmpeg的简单播放器。学习ffplay对于播放器流程、ffmpeg的调用等等是一个非常好的例子。 1.例…

vue-router之过渡动画(六)

1、代码示例&#xff08;1&#xff09;在<router-view>标签的外部添加<transition>标签&#xff0c;标签还需要一个name属性&#xff0c;代码如下&#xff1a;<transition name"fade" mode"out-in"><router-view /></transitio…

(转)基于FFPMEG2.0版本的ffplay代码分析

ref&#xff1a;http://zzhhui.blog.sohu.com/304810230.html背景说明 FFmpeg是一个开源&#xff0c;免费&#xff0c;跨平台的视频和音频流方案&#xff0c;它提供了一套完整的录制、转换以及流化音视频的解决方案。而ffplay是有ffmpeg官方提供的一个基于ffmpeg的简单播放器。…

JAVA对象的finalize()方法

2019独角兽企业重金招聘Python工程师标准>>> finalize java的GC只负责内存相关的清理&#xff0c;所有其它资源的清理必须由程序员手工完成。要不然会引起资源泄露&#xff0c;有可能导致程序崩溃。 调用GC并不保证GC实际执行。 finalize抛出的未捕获异常只会导致…

(未完成)ARM-linux 移植 SDL

ref : https://blog.csdn.net/u012075739/article/details/248776392. 交叉编译SDL 编译SDL前先要编译其依赖库 tslib、libiconv Tslib $ cd tslib-1.4 $ sudo apt-get install libtool automake autoconf $ ./autogen.sh $ echo "ac_cv_func_malloc_0_nonnullyes&qu…