You are on page 1of 12

作者:pydle 邮箱:a7758526@gmail.

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

10. url_fopen .............................................................................................................. 10

11. av_write_header .................................................................................................. 10

12. write_audio_frame............................................................................................... 10

13. write_video_frame ............................................................................................... 11

14. av_write_trailer .................................................................................................... 11

15. close_video ........................................................................................................... 11


作者:pydle 邮箱:a7758526@gmail.com

16. close_audio........................................................................................................... 11

17. av_freep................................................................................................................ 11

18. url_fclose .............................................................................................................. 12

19. av_free.................................................................................................................. 12
作者:pydle 邮箱:a7758526@gmail.com

Output_example.c

数据结构

1. AVFormatContext

 介绍:

作为贯穿始终的数据结构,它包含了 AVStream, AVPacket,

AVInputFormat/AVOutputFormat 这些重要的数据结构。它的大部分成员是不允许

直接设置的,而是通过 AVStream 来设置。

 重要成员:

.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();

oc->oformat = fmt; //AVOutputFormat *fmt

oc->iformat = fmt; //AVInputFormat *fmt

snprintf(oc->filename, sizeof(filename) , ”%s”, filename); //char *filename

2. AVStream

 介绍:

成员 codec 包含了 AVCodecContext 这个起到大部分参数设置作用的数据结构。

 重要成员:

.time_base:timestamps 的基准,对于 fps 固定的内容,timebase=1/framereate

.codec:

.r_frame_rate:真正的流的帧数率,取得是最低值。如果.time_base=1/90000,

time ticks 为 3600 戒 1800,则.r_frame_rate = 50/1

.start_time:尽量避免自己设置

.duration:如果未设置,这个值将由 bitrate 和 file size 指定

.nb_frames:帧的数量

 声明:
作者:pydle 邮箱:a7758526@gmail.com

AVStream *audio_st, *video_st;

 初始化:

video_st = add_video_stream(oc, fmt->video_codec);


audio_st = add_audio_stream(oc, fmt->audio_codec);

3. AVOutputFormat

 介绍:

AVOutputFormat 最重要的成员就是指示视频和音频的 codec:

enum CodecID video_codec;


enum CodecID audio_codec;

 重要成员 :

.audio_codec:
.video_codec:

 声明:

AVOutputFormat *fmt;

 初始化:

fmt=guess_format(NULL, filename, NULL);

4. AVCodecContext

 介绍:

AVCodecContext 作为 AVStream 的成员必须在 AVStream 初始化后初始化。它的成员

指示了基本上用到的所有参数。

 重要成员:

.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

 介绍:

AVCodec 关键是包含了几个比较重要的函数指针,init, encode,close,decode,在

实际使用过程中初始化依赖于 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 由于在具体的编解码工作中会大量使用,为了加速使用宏来定义:

typedef struct AVFrame {


FF_COMMON_FRAME
} AVFrame;

 重要成员:

.key_frame:1->keyframe
.coded_picture_number:
.display_picture_number:

.pts: presentation time stamp, 当 用 avcodec_decode_video() 解 出 一 个

AVFrame 时包含的.pts 值不一定真正是我们需要的。

 声明:

AVFrame *frame;

 初始化:

Picture=avcodec_alloc_frame();

有一个自定义的封装函数也可以:

/* allocate the encoded raw picture */


picture = alloc_picture(c->pix_fmt, c->width, c->height);
作者:pydle 邮箱:a7758526@gmail.com

主要流程

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

declare:AVOutputFormat *guess_format(const char *short_name, const char

*filename, const char *mime_type)

Usage:根据后缀名猜测文件格式,也就是判断用什么编码器戒者解码器

e.x.: fmt = guess_format("mpeg", NULL, NULL);


作者:pydle 邮箱:a7758526@gmail.com

3. avformat_alloc_context

declare:AVFormatContext *avformat_alloc_context(void)

Usage:初始化 AVFormatContext, 实际是初始化 av_class 成员

e.x.: oc = avformat_alloc_context();

4. add_video_stream

declare : static AVStream *add_video_stream(AVFormatContext *oc, int

codec_id)

usage:自定义的函数,用以初始化 AVStream

e.x.: video_st = add_video_stream(oc, fmt->video_codec);

5. add_audio_stream

Declare : static AVStream *add_audio_stream(AVFormatContext *oc, int

codec_id)

usage:自定义函数,功能同上

e.x.: audio_st = add_audio_stream(oc, fmt->audio_codec);

6. av_set_parameters

Declare:int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap)

usage:检查乊前的参数设置是否正确,即使没有设置这步也是必须的

e.x.: av_set_parameters(oc, NULL)

7. dump_format

Declare:void dump_format(AVFormatContext *ic, int index, const char *url,

int is_output)

usage:输出调试诊断信息,最后一项指明是输出还是输入
作者:pydle 邮箱:a7758526@gmail.com

e.x.: dump_format(oc, 0, filename, 1);

8. open_video

Declare:static void open_video(AVFormatContext *oc, AVStream *st)

usage:自定义的函数,寻找并打开编码器,并且完成一些图像的转换工作

e.x.: open_video(oc, video_st);

9. open_audio

Declare:static void open_audio(AVFormatContext *oc, AVStream *st)

usage:分配并打开音频编码器

e.x.: open_audio(oc, audio_st);

10. url_fopen

Declare:int url_fopen(ByteIOContext **s, const char *filename, int flags)

usage:打开文件

e.x.: url_fopen(&oc->pb, filename, URL_WRONLY)

11. av_write_header

Declare:int av_write_header(AVFormatContext *s)

usage:写一些私有信息,成功则返回 0

e.x.: av_write_header(oc);

12. write_audio_frame

Declare:static void write_audio_frame(AVFormatContext *oc, AVStream *st)

usage:封装的写入音频帧数据的函数

e.x.: write_audio_frame(oc, audio_st);


作者:pydle 邮箱:a7758526@gmail.com

13. write_video_frame

Declare:static void write_video_frame(AVFormatContext *oc, AVStream *st)

usage:封装的写入视频数据的函数

e.x.: write_video_frame(oc, video_st);

14. av_write_trailer

Declare:int av_write_trailer(AVFormatContext *s)

usage:封装的在尾部写入一些私有数据

e.x.: av_write_trailer(oc);

15. close_video

Declare:static void close_video(AVFormatContext *oc, AVStream *st)

usage:做一些清理工作,关闭编解码器,清除图像和视频缓存

e.x.: close_video(oc, video_st);

16. close_audio

Declare:static void close_audio(AVFormatContext *oc, AVStream *st)

usage:做一些清理工作,关闭编解码器,清除缓存

e.x.: close_audio(oc, audio_st);

17. av_freep

Declare:void av_freep(void *arg)


作者:pydle 邮箱:a7758526@gmail.com

usage:清除 arg,并且置 NULL

e.x.: av_freep(&oc->streams[i]->codec);
av_freep(&oc->streams[i]);

18. url_fclose

Declare:int url_fclose(ByteIOContext *s)

usage:关闭输出文件

e.x.: url_fclose(oc->pb);

19. av_free

Declare:void av_free(void *ptr)

usage:清除分配的空间,加了个判断

e.x.: av_free(oc);

You might also like