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

使用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. 状压dp做题笔记

    CodeChef Factorial to Square (分块决策) Description 给定一个n,要求在[1,n]中删除一些数,并使剩下的数的乘积是一个完全平方数,同时要求乘积最大,求删除方 ...

  2. sql server 遍历表成一棵树结构

    一棵树的层次结构都在一张表内,当有这样的需要的时候.. 可以这样玩: <!-- DepartmentDTO 对象对应 department表_查询sql --> <sql id=&q ...

  3. ibatis查询列表跟总记录,都引用相同SQL

    在查询记录集合跟查询记录总记录数的时候,我们需要所写的SQL要一样,那么可以都引用同一个SQL.写法如下: <sqlMap namespace="Server"> &l ...

  4. SQL server 自定义函数FUNCTION的使用

    原文链接:https://blog.csdn.net/lanxingbudui/article/details/81736402 前言:        在SQL server中不仅可以可以使用系统自带 ...

  5. Python基础--01小项目体现的基础知识

    part1:猜拳游戏 #coding=utf-8 #当有汉语时可能编译器不认识,需要定义代码 ''' 多行注释 写这个程序是为了熟悉python的基本语法 这是第一个小例子包含简单的if判断,循环和输 ...

  6. Mac出现程序闪退的解决方案

    重置PRAM 1. 关机 2.按下电源键后,立即按下command + option + P +R 3.等到电脑出现4次重启的声音后,放开按键,重置成功 4.正常使用

  7. js+下载文件夹

    一.此方法火狐有些版本是不支持的 window.location.href = 'https://*****.oss-cn-**.aliyuncs.com/*********'; 二.为了解决火狐有些 ...

  8. jQuery系列(二):jQuery的选择器

    css中的选择器有:

  9. Django-CRM后台管理系统

    crm整体流程 表结构 from django.db import models # Create your models here. from django.contrib.auth.models ...

  10. iOS (APP)进程间8中常用通信方式总结

    1 URL Scheme 2 Keychain 3 UIPasteboard 4 UIDocumentInteractionController 5 local socket 6 AirDrop 7 ...