FFMPEG类库打开流媒体的方法(传参数)

使用ffmpeg类库进行开发的时候,打开流媒体(或本地文件)的函数是avformat_open_input()。

其中打开网络流的话,前面要加上函数avformat_network_init()。

一般情况下,只要传入流媒体的url就可以了。但是在打开某些流媒体的时候,可能需要附加一些参数。

例如在播放中央人民广播电台的声音信号的时候,其url为“rtsp://mms.cnr.cn/cnr003?MzE5MTg0IzEjIzI5NjgwOQ==”

如果直接进行打开是不会成功的,我们可以使用ffplay做一下实验:

  1. ffplay rtsp://mms.cnr.cn/cnr003?MzE5MTg0IzEjIzI5NjgwOQ==
会出现错误:

Invalid data found when processing input

这时候我们需要指定其传输方式为TCP,需要将命令改为如下形式:

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

附加了参数以后,发现就可以正常播放了。

此外还可以附加一些参数,比如

  1. 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);
/**
 * 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()。

下面看看把命令

  1. 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. avformat_open_input(&pFormatCtx,url,NULL,&avdic);
时间: 2024-08-30 11:49:02

FFMPEG类库打开流媒体的方法(传参数)的相关文章

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

本文链接:https://blog.csdn.net/leixiaohua1020/article/details/14215393 使用ffmpeg类库进行开发的时候,打开流媒体(或本地文件)的函数是avformat_open_input(). 其中打开网络流的话,前面要加上函数avformat_network_init(). 一般情况下,只要传入流媒体的url就可以了.但是在打开某些流媒体的时候,可能需要附加一些参数. 例如在播放中央人民广播电台的声音信号的时候,其url为“rtsp://m

http delete 方法传参数遇到java.net.ProtocolException: DELETE does not support writing的问题

最近在测试通过http delete method传参数调用服务端方法,遇到了java.net.ProtocolException: DELETE does not support writing try { url = new URL(path); HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); httpCon.setDoOutput(true); httpCon.setRequestMethod(me

Eclipse如何给main方法传参数

import java.util.Arrays; /**  * 这是一个测试类,用来研究main方法的传值问题  * @author HHB  */ public class Test { /**  * 这是类的主方法,用来接受用户的输入,并将输入数据保存到一个String类型的数组里  * @param args  */ public static void main(String[] args) { System.out.println(Arrays.toString(args));//打印

.net中以传引用的方式 向方法中传参数

CLR(CommonLanguageRuntime)公共语言运行时,允许以传引用而非传值的方式传递参数.在C#中,这是用关键字 out 和ref来做到的. 从CLR角度来看,这两个关键字没什么区别,生成的IL代码都是一样的.但是C#编译器是将这两个关键字区别对待的,而且这个区别决定了由哪个方法负责初始化所引用的对.象.如果方法的参数用out关键字来标记,表明不指望调用者在调用方法之前初始化对象.被调用的方法不能够读取out标记的参数的值,而且在函数返回前必须给该参数写入值. 相反,使用ref标记

C#,往线程里传参数的方法总结

C#,往线程里传参数的方法总结 Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托.   Thread (ThreadStart) 初始化 Thread 类的新实例.  由 .NET Compact Framework 支持.  Thread (ParameterizedThreadStart, Int32) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托,并指定线程的最大堆栈

IOS调用WCF提供的服务方法,但是方法的参数是WCF那边自定义的对象,这样有办法调用么,如果可以IOS应该怎么传参呢?请问有了解的么,

最近做一个项目后端使用WCF接收Android手机拍照并带其它参数保存到服务器里:刚好把最近学习的WCF利用上,本以为是个比较简单的功能应该很好实现,没想到其中碰到不少问题,在网上搜索很久一直没有想到的解决方案,最后实现对数据流的分段写入然后后端再来解析流实现的此功能:后端运用WCF中的REST来接收数据:REST还是比较简单的知识,若是不懂可以简单网上了解一下:下面我们先了解一些本次运用到的理论知识: 一:理论知识 由于低层协议特性限制,WCF的流模式只支持如下四种:1:BasicHttpBi

Java:方法的参数是传值还是传引用

Java中方法的参数总是采用传值的方式. 下列方法欲实现对象的交换,但实际上是不能实现的. public void swap(simpleClass a,simpleClass b){ simpleClass temp=a; a=b; b=a; } 因为传入swap的参数实际是对象a和b的一个拷贝(假设为aa,bb). 在方法中虽然交换了aa和bb,但方法结束后它们不再存在. a和b仍然引用调用swap之前的对象. -------------------------分割线 -----------

ffmpeg+ffserver搭建流媒体服务器

http://blog.chinaunix.net/uid-9688646-id-3399113.html ffmpeg和ffserver配合使用可以实现实时的流媒体服务. 一.理解 里边主要有如下四个东西,搞清楚他们之间的关系就差不多明白了. 1. ffmpeg 2. ffserver 3. ffserver.conf 4. feed1.ffm 1. ffmpeg,负责媒体文件的transcode工作,把你服务器上的源媒体文件转换成要发送出去的流媒体文件. 2. ffserver,负责响应客户

U方法传参

U方法用于完成对URL地址的组装,特点在于可以自动根据当前的URL模式和设置生成对应的URL地址,格式为: U('地址','参数','伪静态','是否跳转','显示域名'); 首先在控制器中测试U方法的使用,如U('Home/Index/index'),生成Home模块下Index控制器,index方法的路径.一般不传参数,是没有问题的. 打开模板文件测试,接下来在模板文件中使用U方法,使用的基本规则是这样的, {:U('Home/Index/index')} {:U('Home/Index/i