從格式上下文中讀取

格式包含一個或多個編碼和多路複用流。我們通常以塊的形式讀取它們,塊通常稱為幀(儘管在某些情況下,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);