c++ - 使用 FFMPEG API 读取特定视频帧

标签 c++ ffmpeg

我使用这个循环从 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 将在给定时间戳处寻找关键帧。搜索后,您可以阅读框架。下面的教程还解释了位置和时间戳之间的转换。

http://dranger.com/ffmpeg/tutorial07.html

关于c++ - 使用 FFMPEG API 读取特定视频帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16163567/

相关文章:

c++ - *((unsigned char*) ... ) 是什么意思?

c++ - 初学者 C++ 继承

amazon-s3 - 将 ffmpeg 转码结果流式传输到 S3

FFMPEG 切出一组帧

node.js - 将 h.264 流包装在 mp.4 容器中并使用 nodejs 流式传输

c++ - 通过函数指针计算的 goto/jump 与 fastcall 哪个成本更高?

c++ - 我的 SFML/C++ 程序按预期工作,但如何使移动变慢?

c++ - C++ 栈中的数组

c++ - 使用 FFMPEG 库到 UDP 流 mpeg2 ts 视频延迟/初始连接问题

android - 使用 FFmpeg 在图像上连续播放 gif