1. 简介

应用FFmpeg对音视频流进行解封装是解决音视频输出的初始步骤,FFmpeg提供了丰盛的接口函数进行该操作,上面对一些重要函数性能进行具体介绍。

2. 根本函数介绍

1. av_register_all()

函数原型

void av_register_all(void);

函数性能
初始化所有组件,在函数外部会对编解码器和复用器进行注册。

2. avformat_network_init()

函数原型

int avformat_network_init(void);

函数性能
加载socket库以及网络加密协议相干的库,为后续应用网络相干性能提供反对

3. avformat_open_input()

函数原型

int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)

函数性能
用于关上多媒体数据并且获取相干信息
参数阐明
AVFormatContext **ps:函数如果调用胜利,会将获取到的音视频信息存入AVFormatContext构造体ps中。
const char *filename:关上的音视频流的URL。
AVInputFormat *fmt:能够强制指定AVFormatContext中的AVInputFormat。该参数个别状况都设置为NULL,示意FFmpeg能够自动检测AVInputFormat。
AVDictionary **options:附件的一些选项,个别也设置为NULL。
返回值
调用胜利返回值>=0

4. avformat_find_stream_info()

函数原型

int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);

函数性能
读取一部分音视频数据并由此取得一些该视频的相干信息。
参数阐明
AVFormatContext *ic:将获取到的流信息存入ic中供后续应用。
AVDictionary **options:额定的选项,个别设置为0。
返回值
调用胜利返回值>=0

5. av_find_best_stream()

函数原型

int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type, int wanted_stream_nb, int related_stream,                        AVCodec **decoder_ret, int flags)

函数性能
用于获取音频流、视频流索引。
参数阐明
AVFormatContext* ic:获取到的AVFormatContext。
enum AVMediaType type:输出对应要寻找的具体类型流信息(音频、视频)。
int wanted_stream_nb:个别设置为-1。
related_stream:个别设置为-1。
AVCodec** decoder_ret:个别设置为NULL。
int flags:个别设置为0。
返回值
调用胜利返回值>=0

6. av_read_frame()

函数原型

int av_read_frame(AVFormatContext *s, AVPacket *pkt);

函数性能
用于获取一帧残缺的图像数据,或者取得多帧残缺的音频数据。
参数阐明
AVFormatContext *s:获取到的形态格局上下文AVFormatContext。
AVPacket *pkt:这个值不能为NULL,必须是一个开拓好的空间,用于接管AVPacket数据
返回值
调用胜利返回值>=0

7. av_seek_frame()

函数原型

int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp,                   int flags);

函数性能
用于寻找某个特定帧。
参数阐明
AVFormatContext *s:获取到的形态格局上下文AVFormatContext。
int stream_index:根本流索引,示意seek是针对哪一类流数据(音频或视频)。
int64_t timestamp:要seek的工夫点,以time_base为单位。
int flags:标记位,用于设置seek的形式。
返回值
调用胜利返回值>=0

3. 根底类型介绍

1. AVFormatContext

在应用FFMPEG进行开发的时候,AVFormatContext是一个贯通始终的数据结构,很多函数都要用到它作为参数。它是FFMPEG解封装(flv,mp4,rmvb,avi)性能的构造体。
重要变量阐明
AVIOContext *pb:输出数据的缓存
unsigned int nb_stream:音视频流的个数
AVStream **streams:音视频流
char filename[1024]:文件名
int64_t duration:时长(单位为微秒us)
int bit_rate:比特率(单位为bps)
AVDictionary *metadata:元数据

2. AVStream

AVStream是在FFmpeg应用过程中对于编解码至关重要的构造体之一,是对流(Stream)的封装和形象,形容了视频、音频等流的编码格局等根本流信息。此外也是音频、视频、字母数据流的重要载体。
对于一个典型的mp4格局的媒体源来说,须要通过解封装(解复用),解码而后输入的过程。而解封装从容器中分离出来的流,在FFmpeg中对应的对象就是AVStream。解复用解进去几条AVStream,就会在AVFormateContext中的nb_streams+1(总流数+1),并且将AVStream保留在streams数组中。
重要变量阐明
AVRational time_base:示意工夫基数,AVRational是一个分数类型构造体,有两个整形参数示意分子和分母,用于批示下一个参数int64_t类型中的duration示意多少分之一秒
int64_t duration:将duration单位转换成秒:duration*(time_base.num/time_base.den)
AVRational avg_frame_rate:帧率
AVCodecParameters *codecpar:音视频参数,用于代替AVCodecContext

3. AVCodecParameters

enum AVMediaType code_type:类型参数,示意媒体流是音频还是视频
enum AVCodecID codec_id:示意编码格局,h264,MPEG4, MJPEG
uint32_t codec_tag:个别不必
int format:示意视频的像素格局(YUV420,YUV422...)或者音频的采样格局
int width; int height:视频的宽、高(只有视频有)
uint64_t channel_layout:个别取默认值
int channels:声道数
int sample_rate:采样率
int frame_size:只针对音频,一帧音频的大小

4. AVPacket

AVBufferRef *buf:用于存储援用计数
int64_t pts:示意显示工夫,通过pts*(num/den)换算
int64_t dts:示意解码工夫
uint8_t *data
int size
AVPacket *av_packet_alloc(void):创立AVPacket对象并初始化
AVPacket *av_packet_clone(const AVPacket *src):通过复制创建对象,援用计数+1
int av_packet_ref(AVPacket *dst,const AVPacket *src):作用于函数2相似
av_packet_unref(AVPacket *pkt)
void av_packet_free(AVPacket **pkt):清空对象,援用计数-1
void av_init_packet(AVPacket *pkt):手动初始化对象
int av_packet_from_data(AVPacket *pkt,uint8_t *data, int size):创立AVPacket对象并初始化