版权声明:本文为博主原创文章,未经博主允许不得转载。

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

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

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

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

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

[plain] view
plain
 copy

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

会出现错误:

Invalid data found when processing input

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

[plain] view
plain
 copy

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

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

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

[plain] view
plain
 copy

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

[cpp] view
plain
 copy

  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()。

下面看看把命令

[plain] view
plain
 copy

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

转化为代码实现的方式:

[cpp] view
plain
 copy

  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);

【FFMPEG】使用ffmpeg类库打开流媒体的更多相关文章

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

    使用ffmpeg类库进行开发的时候,打开流媒体(或本地文件)的函数是avformat_open_input(). 其中打开网络流的话,前面要加上函数avformat_network_init(). 一 ...

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

    使用ffmpeg类库进行开发的时候,打开流媒体(或本地文件)的函数是avformat_open_input(). 其中打开网络流的话,前面要加上函数avformat_network_init(). 一 ...

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

    本文链接:https://blog.csdn.net/leixiaohua1020/article/details/14215393 使用ffmpeg类库进行开发的时候,打开流媒体(或本地文件)的函数 ...

  4. Linux下源码安装ffmpeg及ffmpeg的简单使用说明

    一.编译安装 ffmpeg在安装时依赖的包和版本都很让人头疼,不同编译环境也各不相同.公司之前封装了一个又各种出错. 其实办法很简单,就是到官网一步一步按着做就行了:http://trac.ffmpe ...

  5. (转载)[FFmpeg]使用ffmpeg从各种视频文件中直接截取视频图片

    你曾想过从一个视频文件中提取图片吗?在Linux下就可以,在这个教程中我将使用ffmpeg来从视频中获取图片. 什么是ffmpeg?What is ffmpeg? ffmpeg是一个非常有用的命令行程 ...

  6. 项目实战:Qt+Ffmpeg+OpenCV相机程序(打开摄像头、支持多种摄像头、分辨率调整、翻转、旋转、亮度调整、拍照、录像、回放图片、回放录像)

    若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...

  7. EasyDarwin+ffmpeg进行PC(摄像头+麦克风)流媒体直播服务

    上一回我们描述了用EasyDarwin+ffmpeg进行摄像机直播的过程:ffmpeg推送,EasyDarwin转发,vlc播放 实现整个RTSP直播 我们再进行一个方面的描述,那就是pc摄像头+麦克 ...

  8. nginx::基于Nginx+nginx-rtmp-module+ffmpeg搭建rtmp、hls流媒体服务器

    待续 ffmpeg -re -i "/home/bk/hello.mp4" -vcodec libx264 -vprofile baseline -acodec aac -ar 4 ...

  9. 基于Nginx+nginx-rtmp-module+ffmpeg搭建rtmp、hls流媒体服务器(二)

    前言 Nginx-rtmp-module插件针对RTMP协议中一些命令,实现了事件通知和exec外部脚本处理.这里我通过一个简单的SpringBoot项目和Python代码,快速搭建一个HTTP服务来 ...

随机推荐

  1. laravel Passport - Dingo/Api v2.0+Passport 实现 api 认证

    第一部分: 安装passport 使⽤ Composer 依赖包管理器安装 Passport : composer require laravel/passport 接下来,将 Passport 的服 ...

  2. 使用ImageProcessor、CodeCarvings.Piczard组件生成缩略图和添加水印

    技术栈: 1.ImageProcessor(专业图像处理,不能合成水印,NetCore中有它的升级版ImageSharp目前是预览包) 2.CodeCarvings.Piczard(缩略图,水印都能搞 ...

  3. CSS高级学习-1

    优先级 权值 标签权值为1,类权值为10,ID权值最高为100. p{color:red;} /*权值为1*/ p span{color:green;} /*权值为1+1=2*/ .warning{c ...

  4. 栈(Java实现)

    栈是最基本的数据结构之一,其特点是先进后出. 1.基于数组的可动态调节大小的栈 public class ResizingArrayStack<Item> { private Item[] ...

  5. linux服务器上生成CSR和KEY

    CSR表示“ 证书签名请求 ”,这个该证书将用于服务器上.一个CSR包含有关您的组织和域名,地方和国家,将包含在证书中的公钥信息. 本文有3个简单的步骤来创建CSR上的Linux系统(证书签名请求). ...

  6. LeetCode687----最长同值路径

    给定一个二叉树,找到最长的路径,这个路径中的每个节点具有相同值. 这条路径可以经过也可以不经过根节点. 注意:两个节点之间的路径长度由它们之间的边数表示. 示例 1: 输入: 5 / \ 4 5 / ...

  7. HashMap简单介绍

    哈希表(hash table)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希表. 一.什么是哈希表 在讨论哈希表之 ...

  8. [MyBatis]查询SQL得到一个数字

    Mapper文件中写: <select id="findExpiredCount" resultType="integer" parameterType= ...

  9. 前端知识点回顾之重点篇——ES6的Iterator和Generator

    Iterator 迭代器是一种接口.是一种机制. 为各种不同的数据结构提供统一的访问机制.任何数据结构只要部署 Iterator 接口,就可以完成遍历操作(即依次处理该数据结构的所有成员). Iter ...

  10. LC 970. Powerful Integers

    Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some ...