[英]using metadata or side_data in AVFrame / AVPacket to store a number
如果您能夠使用 MKV 作為容器格式,則可以將數字作為BlockAdditional邊數據附加到AVPacket
,並從播放期間從容器讀取的AVPacket
中檢索它。 這是一個粗略的示例,使用字符串因為它們易於閱讀。
注意:如果您想將邊數據與AVFrame
相關聯,您可能需要進行一些額外的跟蹤以將每個AVPacket
與相應的AVFrame
相匹配。 在FFmpeg中,編解碼器不會通過解碼保留side-data。
AVPacket* packet; // from avcodec_receive_packet
int64_t frame_number; // number you want to attach
// Allocate the side data on the packet.
size_t side_data_size = 256;
uint8_t* side_data = av_packet_new_side_data(
packet, AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL, side_data_size);
// Set the BlockAddID. Currently only 1 is supported.
// BlockAdditional data with BlockAddID==1 is intended to store
// data for the codec to use.
uint64_t additional_id = 1;
AV_WB64(side_data, additional_id);
// Put your data in the side_data after the ID.
std::string msg = "frame_number=" + std::to_string(frame_number);
std::strcpy(reinterpret_cast<char*>(side_data + 8), msg.c_str());
然后在播放期間,您可以像這樣檢索邊數據:
AVPacket* packet; // from av_read_frame
// Get side-data from packet.
int side_data_size;
uint8_t* side_data = av_packet_get_side_data(
&packet, AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL, &side_data_size);
// Print it, or parse it how you want.
std::string msg = (char*)(side_data + 8);
std::cout << msg << std::endl;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.