Professional Documents
Culture Documents
com
FFMPEG 学习笔记
目录
FFMPEG 学习笔记 .................................................................................................................... 1
Output_example.c ..................................................................................................................... 3
数据结构 ............................................................................................................................... 3
1. AVFormatContext ....................................................................................................... 3
2. AVStream ................................................................................................................... 4
3. AVOutputFormat ........................................................................................................ 5
4. AVCodecContext ........................................................................................................ 5
5. AVCodec ..................................................................................................................... 6
6. AVFrame ..................................................................................................................... 7
主要流程 ............................................................................................................................... 8
主流程函数 ........................................................................................................................... 8
1. av_register_all ............................................................................................................ 8
2. guess_format.............................................................................................................. 8
3. avformat_alloc_context ............................................................................................. 9
4. add_video_stream ..................................................................................................... 9
5. add_audio_stream ..................................................................................................... 9
6. av_set_parameters .................................................................................................... 9
7. dump_format ............................................................................................................. 9
8. open_video............................................................................................................... 10
9. open_audio .............................................................................................................. 10
12. write_audio_frame............................................................................................... 10
16. close_audio........................................................................................................... 11
17. av_freep................................................................................................................ 11
19. av_free.................................................................................................................. 12
作者:pydle 邮箱:a7758526@gmail.com
Output_example.c
数据结构
1. AVFormatContext
介绍:
AVInputFormat/AVOutputFormat 这些重要的数据结构。它的大部分成员是不允许
重要成员:
.av_class
.iformat:
.oformat:
.nb_streams:
.streams:
.timestamp:
.start_time:
.duration:
.file_size:
.bit_rate:
.video_codec_id:
作者:pydle 邮箱:a7758526@gmail.com
.audio_codec_id:
声明:
AVFormatContext *oc;
初始化:
oc = avformat_alloc_context();
2. AVStream
介绍:
重要成员:
.codec:
.r_frame_rate:真正的流的帧数率,取得是最低值。如果.time_base=1/90000,
.start_time:尽量避免自己设置
.nb_frames:帧的数量
声明:
作者:pydle 邮箱:a7758526@gmail.com
初始化:
3. AVOutputFormat
介绍:
重要成员 :
.audio_codec:
.video_codec:
声明:
AVOutputFormat *fmt;
初始化:
4. AVCodecContext
介绍:
指示了基本上用到的所有参数。
重要成员:
.av_class:
.bit_rate:编码时可以设置,解码的时候由 libavcodec 设置
.time_base:编码时用户设置,解码时 libavcodec 设置
作者:pydle 邮箱:a7758526@gmail.com
.gop_size:编码时用户设置
.frame_size:
.frame_number:
.max_b_frames: 编码时用户设置,解码时无效
.codec:
.codec_id:
声明:
AVCodecContext *c;
AVStream *st;
初始化:
st=av_new_stream(oc,0);
c = st->codec;
c->codec_id = codec_id;
c->codec_type = CODEC_TYPE_VIDEO;
c->bit_rate = 400000;
c->width = 352;
c->height = 288;
c->time_base.den = STREAM_FRAME_RATE;
c->time_base.num = 1;
c->gop_size = 12;
c->pix_fmt = STREAM_PIX_FMT;
5. AVCodec
介绍:
实际使用过程中初始化依赖于 AVCodecContext。
重要成员:
.(*init):
.(*encode):
.(*close):
.(*decode):
声明:
AVCodec *codec;
作者:pydle 邮箱:a7758526@gmail.com
初始化:
codec=avcodec_find_encoder(c->codec_id);
avcodec_open(c,codec);
6. AVFrame
介绍:
AVFrame 由于在具体的编解码工作中会大量使用,为了加速使用宏来定义:
重要成员:
.key_frame:1->keyframe
.coded_picture_number:
.display_picture_number:
声明:
AVFrame *frame;
初始化:
Picture=avcodec_alloc_frame();
有一个自定义的封装函数也可以:
主要流程
1. av_set_parameters(oc, NULL)
2. dump(oc, 0, filename, 1)
3. open_video(oc, video_st)
4. open_audio(oc, audio_st)
5. url_fopen(&oc->pb, filename, URL_WRONLY)
6. av_write_head(oc)
7. write_audio_frame(oc,audio_st)
8. write_video_frame(oc, video_st)
9. av_write_trailer(oc)
10. close_video(oc, video_st)
11. close_audio(oc, audio_st)
12. av_freep(&oc->streams[i]->codec)
13. av_freep(&oc->streams[i])
14. url_fclose(oc->pb)
15. av_free(oc)
主流程函数
1. av_register_all
declare:void av_register_all(void)
usage:初始化必须调用的函数
e.x.: av_register_all();
2. guess_format
Usage:根据后缀名猜测文件格式,也就是判断用什么编码器戒者解码器
3. avformat_alloc_context
declare:AVFormatContext *avformat_alloc_context(void)
e.x.: oc = avformat_alloc_context();
4. add_video_stream
codec_id)
usage:自定义的函数,用以初始化 AVStream
5. add_audio_stream
codec_id)
usage:自定义函数,功能同上
6. av_set_parameters
usage:检查乊前的参数设置是否正确,即使没有设置这步也是必须的
7. dump_format
int is_output)
usage:输出调试诊断信息,最后一项指明是输出还是输入
作者:pydle 邮箱:a7758526@gmail.com
8. open_video
usage:自定义的函数,寻找并打开编码器,并且完成一些图像的转换工作
9. open_audio
usage:分配并打开音频编码器
10. url_fopen
usage:打开文件
11. av_write_header
usage:写一些私有信息,成功则返回 0
e.x.: av_write_header(oc);
12. write_audio_frame
usage:封装的写入音频帧数据的函数
13. write_video_frame
usage:封装的写入视频数据的函数
14. av_write_trailer
usage:封装的在尾部写入一些私有数据
e.x.: av_write_trailer(oc);
15. close_video
usage:做一些清理工作,关闭编解码器,清除图像和视频缓存
16. close_audio
usage:做一些清理工作,关闭编解码器,清除缓存
17. av_freep
e.x.: av_freep(&oc->streams[i]->codec);
av_freep(&oc->streams[i]);
18. url_fclose
usage:关闭输出文件
e.x.: url_fclose(oc->pb);
19. av_free
usage:清除分配的空间,加了个判断
e.x.: av_free(oc);