从格式上下文中读取

格式包含一个或多个编码和多路复用流。我们通常以块的形式读取它们,块通常称为帧(尽管在某些情况下,FFmpeg 仅将解码的原始媒体块称为帧,将编码块称为数据包,这可能会造成混淆)。要从格式中读取单个帧,请使用以下命令:

// A Format Context - see other examples on how to create it
AVFormatContext *formatContext;

// Initialize the AVPacket manually
AVPacket avPacket;
av_init_packet(&avPacket); // set fields of avPacket to default.
avPacket.data = NULL;
avPacket.size = 0;

// Read from the context into the packet
if(av_read_frame(formatContext, &avPacket) == 0){
    // nothing read
}

// Use the packet (such as decoding it and playing it)

// Free packet
av_packet_unref(&avPacket);