最近接触到FFmpeg,需要实现一个将rtsp协议的码流读取并能显示的程序。在网上搬运代码的同时,也写一些对FFmpeg,Qt这些工具的理解。
准备
首先定义宏,其作用是避免‘UINT64_C’ was not declared in this scope的错误。
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif
加入FFmpeg和C++头文件
extern "C" {
/*Include ffmpeg header file*/
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <libavutil/opt.h>
#include <libavutil/mathematics.h>
#include <libavutil/samplefmt.h>
}
#include <iostream>
using namespace std;
主函数
首先,定义输入输出AVFormatContext结构体,这类结构体存储音视频数据,也就是音视频文件的一种抽象和封装,注意在FFmpeg开发者只能使用指针。随后定义输入输出文件名,输入就是rtsp协议的地址,这里我用的是我自己的海康摄像头地址。输出保存为一个flv文件。avformat_network_init函数顾名思义是初始化网络。
int main(void)
{
AVFormatContext* ifmt_ctx = NULL, * ofmt_ctx = NULL;
const char* in_filename, * out_filename;
in_filename = "rtsp://admin:WY@123456@192.168.0.64/h264/ch1/main/av_stream";
out_filename = "output.flv";
avformat_network_init();
设置一个配置字典,在FFmpeg中我们用AVDictionary结构体配置。
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);
接下来就是打开输入流了,需要使用AV,我们先看一下它的声明:、
int avformat_open_input(AVFormatContext **ps, const char *url, ff_const59 AVInputFormat *fmt, AVDictionary **options);
该函数有四个参数,首先是一个指向AVFormatContext指针的指针,随后是url的指针。AVInputFormat 是 指定输入的封装格式。一般传NULL,由FFmpeg自行探测。AVDictionary **options其它参数设置,它是一个字典,用于参数传递。
打开输入视频流之前我们再定义几个参数:
AVPacket pkt;
AVOutputFormat* ofmt = NULL;
int video_index = -1;
int frame_index = 0;
int i;
AVPacket类保存解复用后,解码之前的数据。至于什么是解复用,我们都知道信号有时分复用,频分复用等,音视频信号中经常将视频音频等进行复用,在接收端就得把他们独立分离出来,即Source->Demux->Stream的变化。详见:AVPacket分析
接下来我们打开输入流