1.AVPacket结构体
FFmpeg中用于封装一帧的编码数据的结构体(比如H264视频帧或者AAC音频帧),主要用于编解码过程中数据的载体,使用av_read_frame()读取获得,或者使用avcodec_send_packet()进行解码,与AVFrame一起构成FFmpeg开发中的核心数据流结构:
AVPacket->编码后的数据(如H264视频帧)
AVFrame->解码后的数据(YUV帧,PCM等)
2.核心字段
字段名 | 类型 | 说明 |
---|---|---|
data | uint8_t* | 数据指针(压缩的音视频数据) |
size | int | 数据大小(字节) |
pts | int64_t | 显示时间戳(presentation timestamp) |
dts | int64_t | 解码时间戳(decode timestamp) |
duration | int | 该帧持续时长(以 time_base 为单位) |
stream_index | int | 属于哪个 AVStream 的下标 |
flags | int | 包含关键帧标志 AV_PKT_FLAG_KEY 等 |
pos | int64_t | 在文件中的偏移(字节) |
3.打印相关信息示例
inline void printAVPacketCase(const char* input) {AVFormatContext* fmt_ctx = nullptr;AVPacket pkt;avformat_open_input(&fmt_ctx, input, nullptr, nullptr);if(!fmt_ctx) {fprintf(stderr, "open file failed");}while(av_read_frame(fmt_ctx, &pkt) >= 0) {printf("-------AVPacket Info------\n");printf("stream idx : %d\n", pkt.stream_index);printf("PTS : %" PRId64 "\n", pkt.pts);printf("DTS : %" PRId64 "\n", pkt.dts);printf("Duration: %d\n", pkt.duration);printf("Size: %d bytes\n", pkt.size);printf("Position: %" PRId64 "\n", pkt.pos);if (pkt.flags & AV_PKT_FLAG_KEY)printf("Flags: KEY FRAME\n");elseprintf("Flags: NON-KEY FRAME\n");printf("--------------------------\n");av_packet_unref(&pkt);}
}