我使用这个循环从 FFMPEG 中的视频流中读取帧:
while(av_read_frame(pFormatCtx, &packet)>=0) {
// Is this a packet from the video stream?
if(packet.stream_index==videoStream) {
// Decode video frame
avcodec_decode_video2(pCodecCtx,pFrame,&frameFinished,&packet);
// Did we get a video frame?
if(frameFinished) {
sws_scale(img_convert_context ,pFrame->data,pFrame->linesize,0,
pCodecCtx->height, pFrameRGBA->data, pFrameRGBA->linesize);
printf("%s\n","Frame read finished ");
ExportFrame(pFrameRGBA->data[0]);
break;
}
}
// Save the frame to disk
}
printf("%s\n","Read next frame ");
// Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
}
所以以这种方式顺序读取流。我想要的是随机访问帧以便能够读取特定帧(按帧编号)。它是如何完成的?
最佳答案
你可能想看看
int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp,
int flags);
上述 api 将在给定时间戳处寻找关键帧。搜索后,您可以阅读框架。下面的教程还解释了位置和时间戳之间的转换。
关于c++ - 使用 FFMPEG API 读取特定视频帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16163567/