使用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()的定义:
/**
* 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()。
下面看看把命令
ffplay -rtsp_transport tcp -max_delay 5000000 rtsp://mms.cnr.cn/cnr003?MzE5MTg0IzEjIzI5NjgwOQ==
转化为代码实现的方式:
AVFormatContext *pFormatCtx;
pFormatCtx = avformat_alloc_context();
...代码略
AVDictionary *avdic=NULL;
char option_key[]="rtsp_transport";
char option_value[]="tcp";
av_dict_set(&avdic,option_key,option_value,0);
char option_key2[]="max_delay";
char option_value2[]="5000000";
av_dict_set(&avdic,option_key2,option_value2,0);
char url[]="rtsp://mms.cnr.cn/cnr003?MzE5MTg0IzEjIzI5NjgwOQ==";
avformat_open_input(&pFormatCtx,url,NULL,&avdic);
分享到:
相关推荐
在Ubuntu下编译好的FFmpeg类适用于安卓平台,希望对大家能用
这是在linux环境下编译好的FFmpeg类库和头文件,适用于Android的音视频开发。 在编译过程中,NDK和FFmpeg的版本需要对应起来,否则编译过程中可能失败。这里编译过程中采用的版本为`android-ndk-r15c-linux-x86_64....
很久才找到的,宝贝呀,供大家分享。
编译好的ffmpeg类库文件,32位版本,输出版本号为 ffmpeg version N-99305-gbc0e776 Copyright (c) 2000-2020 the FFmpeg developers built with 用于 x86 的 Microsoft (R) C/C++ 优化编译器 19.27.29112 版 包含...
这是QT+FFMPEG设计的流媒体播放器完整源码 1. 支持RTMP流播放 2. 支持RTSP流播放 3. 支持音频播放 4. 支持视频播放 编译器: MinGW 32位 QT版本: 5.12.6
很多播放器都使用了ffmpeg这个类库来编解码 使用LIB和DLL实现ffmpeg的相关功能函数
C# winform VLC 显示流媒体 及ffmpeg 存储流媒体 demo
运用Qt框架+FFmpeg音视频解码库实现RTMP流媒体播放器,通过实时拉流视频流传到界面进行视频显示
FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。这是java平台的类库包。
C++调用ffmpeg推流到流媒体服务器
本文档记录了利用ffmpeg将摄像机采集的rtsp视频流转换为rtmp视频流,并推送到rtmp服务器red5的详细步骤。最终可以供VLC、web页面访问并展示rtmp视频。
搭建ffmpeg+nginx+yasm 流媒体服务器低延迟,最低延迟两三秒,文档已清晰注明安装部署步骤,基本无需修改,直接可以使用,本文采用rtsp转hls流进行播放
一款基于ffmpeg的android流媒体播放器
基于java搭建的流媒体服务器(nginx+ffmpeg)可运行在linux上,具体文章说明:https://blog.csdn.net/weixin_40263855/article/details/87917484
这是windows下ffmpeg推流桌面与摄像头数据到流媒体服务器源码,该软件里推流和视频保存使用FFMPEG库完成,视频和音频可以同步推流和录制,FFMPEG本身支持跨平台编译开发,QT也支持跨平台,在Android、Linux、windows...
这是 基于FFMPEG设计的流媒体播放器(完整源码),FFMPEG版本是FFMEPG4.2.2。 将工程下载下来之后,要解压到英文目录下打开项目。 (这是最新优化的Demo代码--2023) 这份播放器项目使用场景: 摄像头监控。 设计这份...
基于FFMPEG SDK流媒体开发1---解码媒体文件流信息 没有下载积分了 ,,都施舍点吧
ffmpeg开发流媒体,在线视频用到的插件
http://topic.csdn.net/u/20111014/11/25c37c13-65e3-4f90-be98-feb964934f23.html ffmpeg探测网络流格式方法源码