在循环中使用av_read_frame缓存AVFrames只获得最后几帧
Caching AVFrames using av_read_frame in a loop only get last couple of frames
我正在尝试使用opengl逐帧处理视频。我使用ffmpeg从视频文件中读取帧。
在开始处理帧之前,我想先读取一些帧并将其存储在内存中。因此,我尝试在while循环中使用av_read_frame,并将帧数据复制到数组中
但是,当我尝试显示这些帧时,我发现我只得到最后几帧。例如,如果我想缓存50帧,但我只能获得最后几帧(第45帧到第50帧)。
这是我用来缓存帧的代码:
void cacheFrames()
{
AVPacket tempPacket;
av_init_packet(&tempPacket);
int i = 0;
avcodec_flush_buffers(formatContext->streams[streamIndex] ->codec);
codecContext = formatContext->streams[streamIndex] ->codec;
while (av_read_frame(formatContext, &tempPacket) >= 0 && i <NUM_FRAMES)
{
int finished = 0;
if (tempPacket.stream_index == streamIndex)
{
avcodec_decode_video2(
codecContext,
frame,
&finished,
&tempPacket);
if (finished)
{
memcpy(datas[i].datas, frame->data, sizeof(frame->data)); // copy the frame data into an array
i++;
}
}
}
av_free_packet(&tempPacket);
}
那么,我做错了什么?
data
定义为
uint8_t* AVFrame::data[AV_NUM_DATA_POINTERS]
操作
memcpy(datas[i].datas, frame->data, sizeof(frame->data)); // copy the frame data into an array
将AV_NUM_DATA_POINTERS
指针复制到data[i].datas中。这是不正确的,因为您只将引用复制到您自己尚未分配的帧缓冲区。另外,保证在avcodec_decode_video2
之后只有最后一帧缓冲器可用。
只要需要克隆帧,就可以保留数据。
AVFrame* framearray[NUM_FRAMES];
...
if (finished)
{
framearray[i] = av_frame_clone(frame);
i++;
}
相关文章:
- 读取文件的最后一行并输入到链接列表时出错
- 从udp接收帧对于人脸识别来说太慢
- lower_bound()返回最后一个元素
- 从控制台中删除最后打印的元素
- 获取向量C++中第一个值和最后一个值的和
- 在C++中,如何通过几种类型从元组中选择多个元素
- 如何检测是否在缓冲绘画动画中绘制最后一帧?
- 帧提取 使用OpenCV的视频文件开头只有几秒钟
- 为什么除了最后一帧之外,每一帧都没有调用命令
- Opencv 视频帧.我只能看到最后一帧
- 为什么OpenCV程序捕捉视频帧会在几秒钟后冻结
- gstreamermm:如何提取 Gst::P layBin 的最后一帧视频
- 如何在 C++ 中保留 OpenCV 中的最后一帧
- 我似乎无法在qt创建器中打印几行数据。程序覆盖除最后一行之外的所有输出
- 在循环中使用av_read_frame缓存AVFrames只获得最后几帧
- 尝试在C++中使用OpenCV从灰度相机渲染图像.几帧后就挂了
- 我的字符数组只显示最后几个字符.c++
- 让错误输出包括最后几行代码
- 使用Rcpp返回数据帧,其中仅包含例如患者的最后观察结果
- C/C++服务器未发送文件的最后几行