插件窝 干货文章 如何高效地用FFmpeg提取视频关键帧并直接写入内存?

如何高效地用FFmpeg提取视频关键帧并直接写入内存?

frame codec format stream 227    来源:    2025-03-11

要高效地使用FFmpeg提取视频关键帧并直接写入内存,你可以使用FFmpeg的API(如libavformat和libavcodec)来编写自定义程序。以下是一个基本的步骤指南:

1. 安装FFmpeg库

首先,确保你已经安装了FFmpeg的开发库。你可以通过以下命令安装:

sudo apt-get install libavformat-dev libavcodec-dev libavutil-dev libswscale-dev

2. 编写C/C++程序

使用FFmpeg的API编写一个C/C++程序来提取关键帧并写入内存。以下是一个简单的示例代码:

#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/imgutils.h>
#include <stdio.h>
#include <stdlib.h>

void save_frame_to_memory(AVFrame *frame, uint8_t **buffer, int *size) {
    *size = av_image_get_buffer_size(frame->format, frame->width, frame->height, 1);
    *buffer = (uint8_t *)malloc(*size);
    av_image_copy_to_buffer(*buffer, *size, (const uint8_t * const *)frame->data, frame->linesize, frame->format, frame->width, frame->height, 1);
}

int main(int argc, char *argv[]) {
    if (argc < 2) {
        fprintf(stderr, "Usage: %s <input file>\n", argv[0]);
        exit(1);
    }

    const char *filename = argv[1];
    AVFormatContext *format_ctx = NULL;
    AVCodecContext *codec_ctx = NULL;
    AVCodec *codec = NULL;
    AVFrame *frame = NULL;
    AVPacket pkt;
    int video_stream_index = -1;

    av_register_all();

    if (avformat_open_input(&format_ctx, filename, NULL, NULL) != 0) {
        fprintf(stderr, "Could not open file %s\n", filename);
        exit(1);
    }

    if (avformat_find_stream_info(format_ctx, NULL) < 0) {
        fprintf(stderr, "Could not find stream information\n");
        exit(1);
    }

    for (int i = 0; i < format_ctx->nb_streams; i++) {
        if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
            video_stream_index = i;
            break;
        }
    }

    if (video_stream_index == -1) {
        fprintf(stderr, "Could not find video stream\n");
        exit(1);
    }

    codec = avcodec_find_decoder(format_ctx->streams[video_stream_index]->codecpar->codec_id);
    if (!codec) {
        fprintf(stderr, "Unsupported codec\n");
        exit(1);
    }

    codec_ctx = avcodec_alloc_context3(codec);
    avcodec_parameters_to_context(codec_ctx, format_ctx->streams[video_stream_index]->codecpar);

    if (avcodec_open2(codec_ctx, codec, NULL) < 0) {
        fprintf(stderr, "Could not open codec\n");
        exit(1);
    }

    frame = av_frame_alloc();
    if (!frame) {
        fprintf(stderr, "Could not allocate frame\n");
        exit(1);
    }

    while (av_read_frame(format_ctx, &pkt) >= 0) {
        if (pkt.stream_index == video_stream_index) {
            if (avcodec_send_packet(codec_ctx, &pkt) == 0) {
                while (avcodec_receive_frame(codec_ctx, frame) == 0) {
                    if (frame->key_frame) {
                        uint8_t *buffer = NULL;
                        int size = 0;
                        save_frame_to_memory(frame, &buffer, &size);

                        // 在这里处理内存中的帧数据
                        // 例如:将buffer保存到文件或进行其他处理

                        free(buffer);
                    }
                }
            }
        }
        av_packet_unref(&pkt);
    }

    av_frame_free(&frame);
    avcodec_free_context(&codec_ctx);
    avformat_close_input(&format_ctx);

    return 0;
}

3. 编译程序

使用以下命令编译程序:

gcc -o extract_keyframes extract_keyframes.c -lavformat -lavcodec -lavutil -lswscale

4. 运行程序

运行编译后的程序并传入视频文件路径:

./extract_keyframes input_video.mp4

5. 处理内存中的帧数据

save_frame_to_memory函数中,你可以将帧数据保存到内存中,并根据需要进行处理。例如,你可以将帧数据保存到文件、进行图像处理或直接传输到其他系统。

6. 优化

为了提高效率,你可以考虑以下优化措施: - 使用多线程解码。 - 使用硬件加速解码(如CUDA、VAAPI等)。 - 减少内存拷贝操作,直接使用FFmpeg提供的API处理帧数据。

通过以上步骤,你可以高效地提取视频关键帧并直接写入内存。