1.AVStream
ffmpeg中用来描述媒体流(音频流,视频流,字幕流)的结构体,一个媒体流文件中通常包含多个流(如视频流+音频流)
属于AVFormatContext的一部分,存在于AVFormatContext::streams中
2.AVStream常用字段
字段名 | 类型 | 说明 |
---|---|---|
index | int | 当前流的索引(在 streams 数组中的下标) |
codecpar | AVCodecParameters* | 编解码参数(重要!包括宽高、采样率等) |
time_base | AVRational | 时间基准,PTS/DTS单位 |
start_time | int64_t | 流的起始时间戳 |
duration | int64_t | 流的总时长(以 time_base 为单位) |
nb_frames | int64_t | 总帧数(某些格式可能为0) |
avg_frame_rate | AVRational | 平均帧率(视频流特有) |
r_frame_rate | AVRational | 实际帧率 |
metadata | AVDictionary* | 当前流的元数据 |
3.AVCodecParameters
FFmpeg中的核心结构体之一,在新版本的FFmpeg中取代了AVCodecContext的一部分作用,用于描述一个媒体流的编解码参数,如视频的宽高/采样率/编码格式等。
4.AVCodecParameters常用字段
字段名 | 类型 | 说明 |
---|---|---|
codec_type | enum AVMediaType | 媒体类型(视频、音频、字幕等) |
codec_id | enum AVCodecID | 编码器 ID(如 H264, AAC, MP3) |
format | int | 像素/采样格式(音频/视频不同) |
bit_rate | int64_t | 比特率(单位:bps) |
width | int | 视频宽度(仅视频) |
height | int | 视频高度(仅视频) |
sample_rate | int | 音频采样率(仅音频) |
channels | int | 音频声道数(仅音频) |
channel_layout | uint64_t | 声道布局(如立体声、5.1等) |
profile | int | 编码器 profile(如 baseline, main) |
level | int | 编码器 level |
5.常用方法:拷贝参数
avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar);
直接复制流参数,在做转码/封装时使用
6.打印示例
//AVStream
inline void printAVStreamCase(const char* input) {AVFormatContext* fmt_ctx = nullptr;avformat_open_input(&fmt_ctx, input, nullptr, nullptr);if(!fmt_ctx) {fprintf(stderr, "open file failed");return ;}int video_stream_index = 1;for(unsigned int i = 0; i < fmt_ctx->nb_streams; i++) {AVStream *stream = fmt_ctx->streams[i];AVCodecParameters *codecpar = stream->codecpar;if(codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {printf("Video stream: index=%d, width=%d, height=%d\n",stream->index, codecpar->width, codecpar->height);} else if (codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {printf("Audio stream: index=%d, sample_rate=%d",stream->index, codecpar->sample_rate);}}
}