整理 ffmpeg 录制用到的一些 API,以及一些理解
API调用
常用API
AVFormatContext *avformat_alloc_context(void); // 创建 avformat 上下文结构体
void avformat_free_context(AVFormatContext *s);//
int avformat_alloc_output_context2(AVFormatContext **ctx, const AVOutputFormat *oformat, const char *format_name, const char *filename); // 创建 avformat 上下文结构体,并设置输出格式,这边的 filename 主要用来推测 oformat 的格式,并设置 url 参数
AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c); // 向 format 上下文,插入新的 stream,索引号为之前的 stream 个数
int avformat_write_header(AVFormatContext *s, AVDictionary **options); // 写头信息 s->oformat->write_header,具体函数到 muxer_list 查找
int av_write_frame(AVFormatContext *s, AVPacket *pkt); // 写数据 s->oformat->write_packet, 需要外部释放,应该是因为这个接口调用之后 Pkt 数据就同步写入了,所以可以外部控制释放
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt); // 交织写入数据,内部释放 pkt,这个接口把 pkt 传入不一定马上写入,可能会缓存到堆栈中,等交织之后才写入,所以外部控制不了什么时候释放
int av_write_trailer(AVFormatContext *s); // 把缓存的交织数据都写入文件,并写文件尾 s->oformat->write_trailer
void av_dump_format(AVFormatContext *ic, int index, const char *url, int is_output); // 打印输入和输出的详细格式信息
void avformat_close_input(AVFormatContext **s); // 释放 avformat 上下文结构体 和 调用 avio_close 关闭 AVIO
int avio_open(AVIOContext **s, const char *url, int flags); // 创建并初始化 AVIO 上下文
int avio_closep(AVIOContext **s); // 关闭 AVIO 上下文,并设置指针为 NULL
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec); // 将 codec 的参数拷贝到 par
class
static const AVClass av_format_context_class = {
.class_name = "AVFormatContext",
.item_name = format_to_name,
.option = avformat_options,
.version = LIBAVUTIL_VERSION_INT,
.child_next = format_child_next,
.child_class_iterate = format_child_class_iterate,
.category = AV_CLASS_CATEGORY_MUXER,
.get_category = get_category,
};
AVOutputFormat
const AVOutputFormat ff_mp4_muxer = {
.name = "mp4",
.long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
.mime_type = "video/mp4",
.extensions = "mp4",
.priv_data_size = sizeof(MOVMuxContext),
.audio_codec = AV_CODEC_ID_AAC,
.video_codec = CONFIG_LIBX264_ENCODER ?
AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
.init = mov_init,
.write_header = mov_write_header,
.write_packet = mov_write_packet,
.write_trailer = mov_write_trailer,
.deinit = mov_free,
.flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
.codec_tag = mp4_codec_tags_list,
.check_bitstream = mov_check_bitstream,
.priv_class = &mov_isobmff_muxer_class,
};
demo
avformat_alloc_output_context2(&m_ic, NULL, NULL, file);
m_vs = avformat_new_stream(m_ic, NULL);
m_vs->codecpar->codec_tag = 0;
avcodec_parameters_from_context(m_vs->codecpar, m_vc);
av_dump_format(m_ic, 0, m_filename.c_str(), 1);
m_as = avformat_new_stream(m_ic, NULL);
m_as->codecpar->codec_tag = 0;
avcodec_parameters_from_context(m_as->codecpar, m_ac);
av_dump_format(m_ic, 0, m_filename.c_str(), 1);
int ret = avio_open(&m_ic->pb, m_filename.c_str(), AVIO_FLAG_WRITE);
ret = avformat_write_header(m_ic, NULL);
av_interleaved_write_frame(m_ic, pkt);
av_write_trailer(m_ic);
avio_closep(&m_ic->pb);
avformat_close_input(&m_ic);
其他
ffmpeg 习惯记录
muxer_list.c 有个结构体为 muxer_list 是ffmpeg 支持的所有 AVOutputFormat。其他的类似,比如 codec_list。
allformat.c 是所有支持的 AVOutputFormat 定义的 extern。其他类似 alldevices,allcodecs,allfilters
avio.h 所有函数
const char *avio_find_protocol_name(const char *url);
int avio_check(const char *url, int flags);
int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options);
int avio_read_dir(AVIODirContext *s, AVIODirEntry **next);
int avio_close_dir(AVIODirContext **s);
void avio_free_directory_entry(AVIODirEntry **entry);
AVIOContext *avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int (*read_packet)(void *opaque, uint8_t *buf, int buf_size), int (*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t (*seek)(void *opaque, int64_t offset, int whence));
void avio_context_free(AVIOContext **s);
void avio_w8(AVIOContext *s, int b);
void avio_write(AVIOContext *s, const unsigned char *buf, int size);
void avio_wl64(AVIOContext *s, uint64_t val);
void avio_wb64(AVIOContext *s, uint64_t val);
void avio_wl32(AVIOContext *s, unsigned int val);
void avio_wb32(AVIOContext *s, unsigned int val);
void avio_wl24(AVIOContext *s, unsigned int val);
void avio_wb24(AVIOContext *s, unsigned int val);
void avio_wl16(AVIOContext *s, unsigned int val);
void avio_wb16(AVIOContext *s, unsigned int val);
int avio_put_str(AVIOContext *s, const char *str);
int avio_put_str16le(AVIOContext *s, const char *str);
int avio_put_str16be(AVIOContext *s, const char *str);
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type);
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence);
int64_t avio_skip(AVIOContext *s, int64_t offset);
static av_always_inline int64_t avio_tell(AVIOContext *s){return avio_seek(s, 0, SEEK_CUR);}
int64_t avio_size(AVIOContext *s);
int avio_feof(AVIOContext *s);
int avio_printf(AVIOContext *s, const char *fmt, ...) av_printf_format(2, 3);
void avio_print_string_array(AVIOContext *s, const char *strings[]);
void avio_flush(AVIOContext *s);
int avio_read(AVIOContext *s, unsigned char *buf, int size);
int avio_read_partial(AVIOContext *s, unsigned char *buf, int size);
int avio_r8 (AVIOContext *s);
unsigned int avio_rl16(AVIOContext *s);
unsigned int avio_rl24(AVIOContext *s);
unsigned int avio_rl32(AVIOContext *s);
uint64_t avio_rl64(AVIOContext *s);
unsigned int avio_rb16(AVIOContext *s);
unsigned int avio_rb24(AVIOContext *s);
unsigned int avio_rb32(AVIOContext *s);
uint64_t avio_rb64(AVIOContext *s);
int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen);
int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen);
int avio_open(AVIOContext **s, const char *url, int flags);
int avio_open2(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options);
int avio_close(AVIOContext *s);
int avio_closep(AVIOContext **s);
int avio_open_dyn_buf(AVIOContext **s);
int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer);
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer);
const char *avio_enum_protocols(void **opaque, int output);
const AVClass *avio_protocol_get_class(const char *name);
int avio_pause(AVIOContext *h, int pause);
int64_t avio_seek_time(AVIOContext *h, int stream_index, int64_t timestamp, int flags);
int avio_read_to_bprint(AVIOContext *h, struct AVBPrint *pb, size_t max_size);
int avio_accept(AVIOContext *s, AVIOContext **c);
int avio_handshake(AVIOContext *c);
avformat.h 所有函数
void av_format_inject_global_side_data(AVFormatContext *s);
enum AVDurationEstimationMethod av_fmt_ctx_get_duration_estimation_method(const AVFormatContext* ctx);
unsigned avformat_version(void);
const char *avformat_configuration(void);
const char *avformat_license(void);
int avformat_network_init(void);
int avformat_network_deinit(void);
const AVOutputFormat *av_muxer_iterate(void **opaque);
const AVInputFormat *av_demuxer_iterate(void **opaque);
AVFormatContext *avformat_alloc_context(void);
void avformat_free_context(AVFormatContext *s);
const AVClass *avformat_get_class(void);
AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c);
int av_stream_add_side_data(AVStream *st, enum AVPacketSideDataType type, uint8_t *data, size_t size);
uint8_t *av_stream_new_side_data(AVStream *stream, enum AVPacketSideDataType type, size_t size);
uint8_t *av_stream_get_side_data(const AVStream *stream, enum AVPacketSideDataType type, size_t *size);
AVProgram *av_new_program(AVFormatContext *s, int id);
int avformat_alloc_output_context2(AVFormatContext **ctx, const AVOutputFormat *oformat, const char *format_name, const char *filename);
const AVInputFormat *av_find_input_format(const char *short_name);
const AVInputFormat *av_probe_input_format(const AVProbeData *pd, int is_opened);
const AVInputFormat *av_probe_input_format2(const AVProbeData *pd, int is_opened, int *score_max);
const AVInputFormat *av_probe_input_format3(const AVProbeData *pd, int is_opened, int *score_ret);
int av_probe_input_buffer2(AVIOContext *pb, const AVInputFormat **fmt, const char *url, void *logctx, unsigned int offset, unsigned int max_probe_size);
int av_probe_input_buffer(AVIOContext *pb, const AVInputFormat **fmt, const char *url, void *logctx, unsigned int offset, unsigned int max_probe_size);
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options);
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);
AVProgram *av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s);
void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned int idx);
int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type, int wanted_stream_nb, int related_stream, const AVCodec **decoder_ret, int flags);
int av_read_frame(AVFormatContext *s, AVPacket *pkt);
int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags);
int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags);
int avformat_flush(AVFormatContext *s);
int av_read_play(AVFormatContext *s);
int av_read_pause(AVFormatContext *s);
void avformat_close_input(AVFormatContext **s);
int avformat_write_header(AVFormatContext *s, AVDictionary **options);
int avformat_init_output(AVFormatContext *s, AVDictionary **options);
int av_write_frame(AVFormatContext *s, AVPacket *pkt);
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt);
int av_write_uncoded_frame(AVFormatContext *s, int stream_index, AVFrame *frame);
int av_interleaved_write_uncoded_frame(AVFormatContext *s, int stream_index, AVFrame *frame);
int av_write_uncoded_frame_query(AVFormatContext *s, int stream_index);
int av_write_trailer(AVFormatContext *s);
const AVOutputFormat *av_guess_format(const char *short_name, const char *filename, onst char *mime_type);
enum AVCodecID av_guess_codec(const AVOutputFormat *fmt, const char *short_name, const char *filename, const char *mime_type, enum AVMediaType type);
int av_get_output_timestamp(struct AVFormatContext *s, int stream, int64_t *dts, int64_t *wall);
void av_hex_dump(FILE *f, const uint8_t *buf, int size);
void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size);
void av_pkt_dump2(FILE *f, const AVPacket *pkt, int dump_payload, const AVStream *st);
void av_pkt_dump_log2(void *avcl, int level, const AVPacket *pkt, int dump_payload, const AVStream *st);
enum AVCodecID av_codec_get_id(const struct AVCodecTag * const *tags, unsigned int tag);
unsigned int av_codec_get_tag(const struct AVCodecTag * const *tags, enum AVCodecID id);
int av_codec_get_tag2(const struct AVCodecTag * const *tags, enum AVCodecID id, unsigned int *tag);
int av_find_default_stream_index(AVFormatContext *s);
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags);
const AVIndexEntry *avformat_index_get_entry(AVStream *st, int idx);
const AVIndexEntry *avformat_index_get_entry_from_timestamp(AVStream *st, int64_t wanted_timestamp, int flags);
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags);
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url);
void av_dump_format(AVFormatContext *ic, int index, const char *url, int is_output);
int av_get_frame_filename2(char *buf, int buf_size, const char *path, int number, int flags);
int av_get_frame_filename(char *buf, int buf_size, const char *path, int number);
int av_filename_number_test(const char *filename);
int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size);
int av_match_ext(const char *filename, const char *extensions);
int avformat_query_codec(const AVOutputFormat *ofmt, enum AVCodecID codec_id, int std_compliance);
const struct AVCodecTag *avformat_get_riff_video_tags(void);
const struct AVCodecTag *avformat_get_riff_audio_tags(void);
const struct AVCodecTag *avformat_get_mov_video_tags(void);
const struct AVCodecTag *avformat_get_mov_audio_tags(void);
AVRational av_guess_sample_aspect_ratio(AVFormatContext *format, AVStream *stream, AVFrame *frame);
AVRational av_guess_frame_rate(AVFormatContext *ctx, AVStream *stream, AVFrame *frame);
int avformat_match_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec);
int avformat_queue_attached_pictures(AVFormatContext *s);
int avformat_transfer_internal_stream_timing_info(const AVOutputFormat *ofmt, AVStream *ost, const AVStream *ist, enum AVTimebaseSource copy_tb);
AVRational av_stream_get_codec_timebase(const AVStream *st);
标签:AVIOContext,const,ffmpeg,int,avio,录制,char,av
From: https://blog.csdn.net/dss875914213/article/details/142314344