ffmpeg封装和解封装介绍-(5)解封装常用接口函数

avformat_open_input

int avformat_open_input(AVFormatContext **ps,

const char *filename,

AVInputFormat *fmt,

AVDictionary **options);

avformat_open_input 是 FFmpeg 库中的一个函数,用于打开一个输入文件并初始化格式上下文。该函数会根据文件头信息自动检测输入文件的格式,并准备好读取操作。 

参数详解

  1. **AVFormatContext ps

    • 类型: AVFormatContext**
    • 解释: 指向 AVFormatContext 结构体指针的指针。
    • 用途: 用于返回初始化的格式上下文。调用函数前应将其指针置为 NULL。
  2. *const char filename

    • 类型: const char*
    • 解释: 输入文件名或 URL。
    • 用途: 指定要打开的输入文件或流的路径。
  3. *AVInputFormat fmt

    • 类型: AVInputFormat*
    • 解释: 输入文件的格式。
    • 用途: 通常为 NULL,让库自动检测输入文件的格式。如果知道具体格式,可以指定相应的 AVInputFormat
  4. **AVDictionary options

    • 类型: AVDictionary**
    • 解释: 选项字典。
    • 用途: 用于传递特定格式或协议的附加选项。可以为 NULL。如果不为 NULL,函数返回时,包含未识别的选项。

avformat_find_stream_info

avformat_find_stream_info 是 FFmpeg 库中的一个函数,用于读取媒体文件或流的详细信息,并填充 AVFormatContext 结构体中的流信息。该函数会尝试读取并解析文件的头部数据,以确定文件中的每个流的编解码器类型、时基、帧率等信息。

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

参数详解

  1. *AVFormatContext ic

    • 类型: AVFormatContext*
    • 解释: 格式上下文。
    • 用途: 指向已经打开并初始化的 AVFormatContext 结构体,表示要读取信息的输入文件或流。
  2. **AVDictionary options

    • 类型: AVDictionary**
    • 解释: 选项字典。
    • 用途: 用于传递给解码器的附加选项。可以为 NULL。如果不为 NULL,函数返回时包含未识别的选项。

av_dump_format

av_dump_format 是 FFmpeg 库中的一个函数,用于打印格式上下文的详细信息,包括文件名、格式、时长、比特率以及每个流的相关信息。这个函数对于调试和了解媒体文件的内容非常有用。

void av_dump_format(AVFormatContext *ic, int index, const char *url, int is_output);

参数详解

  1. *AVFormatContext ic

    • 类型: AVFormatContext*
    • 解释: 格式上下文。
    • 用途: 指向包含媒体文件或流信息的 AVFormatContext 结构体。
  2. int index

    • 类型: int
    • 解释: 文件索引。
    • 用途: 通常为0,用于指定要打印信息的文件索引。在有多个文件的情况下使用。
  3. *const char url

    • 类型: const char*
    • 解释: 文件名或 URL。
    • 用途: 用于指定媒体文件的名称或 URL。
  4. int is_output

    • 类型: int
    • 解释: 输出标志。
    • 用途: 指定这是一个输入还是输出格式上下文。0表示输入,非0表示输出。

avformat_alloc_output_context2

avformat_alloc_output_context2 是 FFmpeg 库中的一个函数,用于为输出文件或流分配并初始化一个 AVFormatContext 结构体。该函数会根据指定的格式自动初始化相应的字段,使得后续的输出操作更加简便。

int avformat_alloc_output_context2(AVFormatContext **ctx,

AVOutputFormat *oformat,

const char *format_name,

const char *filename);

参数详解

  1. **AVFormatContext ctx

    • 类型: AVFormatContext**
    • 解释: 指向 AVFormatContext 结构体指针的指针。
    • 用途: 用于返回初始化的格式上下文。调用函数前应将其指针置为 NULL。
  2. *AVOutputFormat oformat

    • 类型: AVOutputFormat*
    • 解释: 输出文件的格式。
    • 用途: 指定输出文件的格式。如果为 NULL,函数将根据 format_namefilename 自动推断格式。
  3. *const char format_name

    • 类型: const char*
    • 解释: 输出文件格式的名称。
    • 用途: 指定输出文件的格式名称,如 "mp4"。可以为 NULL,此时将根据 filename 自动推断格式。
  4. *const char filename

    • 类型: const char*
    • 解释: 输出文件名或 URL。
    • 用途: 用于指定输出文件的名称或 URL。可以为 NULL,此时必须指定 format_name

 

avformat_new_stream

avformat_new_stream 是 FFmpeg 库中的一个函数,用于在给定的 AVFormatContext 中添加一个新的流(AVStream。这个新流通常用来表示即将编码的数据(音频、视频、字幕等)。该函数会自动分配并初始化 AVStream 结构体,并将其添加到 AVFormatContext 的流列表中。

AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c);

参数详解

  1. *AVFormatContext s

    • 类型: AVFormatContext*
    • 解释: 格式上下文。
    • 用途: 指向包含媒体文件或流信息的 AVFormatContext 结构体,表示要添加新流的上下文。
  2. *const AVCodec c

    • 类型: const AVCodec*
    • 解释: 编解码器。
    • 用途: 指定要使用的编解码器。可以为 NULL,此时不会自动设置编解码器。

avio_open

avio_open 是 FFmpeg 库中的一个函数,用于打开一个 URL(文件或流)并为其分配并初始化一个 AVIOContext 结构体。这个 AVIOContext 结构体随后可以用于读写操作。该函数通常在设置输出文件或流时使用。

int avio_open(AVIOContext **s, const char *url, int flags);

参数详解

  1. **AVIOContext s

    • 类型: AVIOContext**
    • 解释: 指向 AVIOContext 结构体指针的指针。
    • 用途: 用于返回初始化的 AVIOContext。调用函数前应将其指针置为 NULL。
  2. *const char url

    • 类型: const char*
    • 解释: URL(文件名或流地址)。
    • 用途: 用于指定要打开的文件或流的名称或 URL。
  3. int flags

    • 类型: int
    • 解释: 标志。
    • 用途: 指定打开文件或流的模式。可以是以下常量的组合:
      • AVIO_FLAG_READ:只读模式。
      • AVIO_FLAG_WRITE:只写模式。
      • AVIO_FLAG_READ_WRITE:读写模式。

 

avcodec_parameters_copy

avcodec_parameters_copy 是 FFmpeg 库中的一个函数,用于将一个 AVCodecParameters 结构体中的编解码器参数复制到另一个 AVCodecParameters 结构体中。这在设置输出流参数或进行转码时非常有用。

int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src);

参数详解

  1. *AVCodecParameters dst

    • 类型: AVCodecParameters*
    • 解释: 目标编解码器参数结构体。
    • 用途: 指向要复制参数到的目标 AVCodecParameters 结构体。
  2. *const AVCodecParameters src

    • 类型: const AVCodecParameters*
    • 解释: 源编解码器参数结构体。
    • 用途: 指向要从中复制参数的源 AVCodecParameters 结构体。

avformat_write_header

avformat_write_header 是 FFmpeg 库中的一个函数,用于写入输出文件的头部信息。在初始化输出格式上下文并添加所有需要的流之后,必须调用此函数来写入文件头。这样后续的帧写入操作才能进行。

int avformat_write_header(AVFormatContext *s, AVDictionary **options);

参数详解:

  • *AVFormatContext s

    • 类型: AVFormatContext*
    • 解释: 格式上下文。
    • 用途: 指向包含输出文件或流信息的 AVFormatContext 结构体。它包含了所有流的信息以及输出文件的格式。
  • **AVDictionary options

    • 类型: AVDictionary**
    • 解释: 选项字典。
    • 用途: 传递格式化选项。可以为 NULL。如果有特定的选项需要传递给 muxer,可以使用这个参数。例如,设置 bit_ratecodec 等选项。

av_seek_frame

av_seek_frame 是 FFmpeg 库中用于在多媒体文件中执行寻帧操作的函数。它允许你跳转到文件中的特定位置,通常基于时间戳(如 PTS)。这是在处理音视频文件时常用的操作,例如在媒体播放器中实现快进、快退等功能

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

参数详解

  • AVFormatContext *s:指向打开的多媒体文件的格式上下文(通过 avformat_open_input 打开)。
  • int stream_index:指定需要进行寻帧操作的流索引。如果设为 -1,FFmpeg 将会选择一个合适的流进行寻帧操作。
  • int64_t timestamp:目标时间戳,单位是流的时间基(time_base)。可以使用 av_rescale_q 将秒或其他单位的时间转换为目标流的时间基单位。
  • int flags:寻帧标志,指定如何进行寻帧操作。常用标志包括:
    • AVSEEK_FLAG_BACKWARD:寻找到小于等于目标时间戳的关键帧。
    • AVSEEK_FLAG_BYTE:基于字节位置寻帧,而不是基于时间戳。
    • AVSEEK_FLAG_ANY:可以寻找到任意帧,而不仅仅是关键帧。
    • AVSEEK_FLAG_FRAME:基于帧数寻帧。

 

av_read_frame

av_read_frame 从输入文件中读取每一帧数据,并存储在 pkt

av_rescale_q_rnd

av_rescale_q_rnd 是 FFmpeg 库中的一个函数,用于在不同时间基之间进行时间戳的缩放和转换,并且支持指定舍入模式。这在处理音视频同步和时间戳转换时非常有用。

int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, enum AVRounding rnd);

参数详解

  • int64_t a:需要转换的值(例如,时间戳)。
  • AVRational bq:原时间基。
  • AVRational cq:目标时间基。
  • enum AVRounding rnd:舍入模式,决定转换时的舍入策略。

返回值

返回值是以目标时间基 cq 表示的转换后的值。

舍入模式 AVRounding

AVRounding 是一个枚举,定义了舍入的不同模式:

  • AV_ROUND_ZERO:向零舍入。
  • AV_ROUND_INF:向无穷远舍入。
  • AV_ROUND_DOWN:向负无穷舍入。
  • AV_ROUND_UP:向正无穷舍入。
  • AV_ROUND_NEAR_INF:四舍五入

av_rescale_q

av_rescale_q 是 FFmpeg 库中的一个函数,用于在不同时间基之间进行时间戳的缩放和转换。它允许你将一个时间戳从一个时间基转换为另一个时间基,而不需要指定舍入模式。通常用于处理音视频同步和时间戳转换。

int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq);

参数详解

  • int64_t a:需要转换的值(例如,时间戳)。
  • AVRational bq:原时间基。
  • AVRational cq:目标时间基。

av_interleaved_write_frame

av_interleaved_write_frame 函数是用于向多媒体文件写入一个交错(interleaved)帧的函数。在音视频文件中,交错写入指的是将音频和视频帧交错地写入文件,以便于播放器读取。

int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)

参数详解

  • AVFormatContext *s: 输出格式上下文。它是一个存储有关输出文件格式的信息的结构体,包括文件名、编解码器信息等。

  • AVPacket *pkt: 要写入的数据包(包含音频或视频帧的数据)。

av_write_trailer

av_write_trailer 函数用于在编写音视频流数据到输出文件后,向输出文件写入文件尾。

int av_write_trailer(AVFormatContext *s)

参数详解:

  • AVFormatContext *s:输出格式上下文。包含了有关输出文件的信息,如文件名、编解码器信息等。

avformat_close_input

avformat_close_input 函数通常与 avformat_open_input 函数成对使用。

 avio_closep

avio_closep 函数通常与 avio_open 函数成对使用

avformat_free_context

avformat_free_context 函数通常与 avformat_alloc_context(或者avformat_alloc_output_context2

  • 16
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常抱歉,我之前的回答有误。对于FFmpeg版本6.0,确实不再需要调用`av_register_all()`函数。以下是修改后的代码,适用于FFmpeg 6.0版本: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <libavformat/avformat.h> int main(int argc, char *argv[]) { avformat_network_init(); // 初始化网络模块 AVFormatContext *outputContext = NULL; AVOutputFormat *outputFormat = NULL; AVPacket packet; const char *inputFileName = "in.h264"; const char *outputUrl = "rtsp://output_stream_url"; // 打开输出流 avformat_alloc_output_context2(&outputContext, NULL, "rtsp", outputUrl); if (!outputContext) { printf("无法创建输出流上下文\n"); return -1; } outputFormat = outputContext->oformat; // 添加视频流 AVStream *outStream = avformat_new_stream(outputContext, NULL); if (!outStream) { printf("无法创建输出流\n"); return -1; } // 设置流参数 outStream->codecpar->codec_id = AV_CODEC_ID_H264; outStream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; // 打开输出流 if (!(outputFormat->flags & AVFMT_NOFILE)) { if (avio_open(&outputContext->pb, outputUrl, AVIO_FLAG_WRITE) < 0) { printf("无法打开输出流\n"); return -1; } } // 写入流头信息 if (avformat_write_header(outputContext, NULL) < 0) { printf("无法写入流头信息\n"); return -1; } // 打开输入文件 FILE *inputFile = fopen(inputFileName, "rb"); if (!inputFile) { printf("无法打开输入文件\n"); return -1; } // 读取输入文件的数据帧并写入输出流 while (1) { int ret = fread(packet.data, 1, sizeof(packet.data), inputFile); if (ret <= 0) break; packet.size = ret; packet.pts = packet.dts = AV_NOPTS_VALUE; packet.stream_index = outStream->index; // 写入数据包 if (av_interleaved_write_frame(outputContext, &packet) < 0) { printf("无法写入数据包\n"); break; } } // 写入流尾信息 av_write_trailer(outputContext); // 关闭输入文件 fclose(inputFile); // 释放资源 if (outputContext && !(outputFormat->flags & AVFMT_NOFILE)) avio_closep(&outputContext->pb); avformat_free_context(outputContext); return 0; } ``` 请确保将`output_stream_url`替换为要推送到的RTSP流的URL,并将`in.h264`文件放在可执行文件的同一目录下。编译并运行此代码,它将从`in.h264`文件中读取H.264原始数据帧并将其推送到指定的RTSP流中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值