d盘有一个test.264文件,我们需要从这个h264文件中提取出一帧一帧的数据,所以直接采用ffmpeg来做。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#ifdef __cplusplus
extern "C" {
#endif
#include <libswscale/swscale.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
#include <libavutil/time.h>
#include <libavutil/avutil.h>
#include <libavutil/imgutils.h>
#include <libavutil/hwcontext.h>
#include <libavutil/error.h>
#ifdef __cplusplus
}
#endif

int main(int argc, char *argv[])
{
    AVFormatContext *input_ctx = NULL;
    AVCodecContext *decoder_ctx = NULL;
    AVCodec *decoder = NULL;
    enum AVHWDeviceType type;
    const char *dec_name = "h264_qsv";

    const char *in_file = "d://test.264";
    int video_stream = 0, ret;
    AVPacket packet;

        /* open the input file */
    if (avformat_open_input(&input_ctx, in_file, NULL, NULL) != 0) {
        fprintf(stderr, "Cannot open input file '%s'\n", argv[2]);
        return -1;
    }

    /* find the video stream information */
    ret = av_find_best_stream(input_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
    if (ret < 0) {
        fprintf(stderr, "Cannot find a video stream in the input file\n");
        return -1;
    }

    while (ret >= 0)
    {
        if ( ( ret = av_read_frame(input_ctx,&packet) ) < 0 )//逐个包从文件读取数据
        {
            break;
        }

        if (video_stream == packet.stream_index)//打印帧大小
        {
            printf( " frame size:%d \n" , packet.size );
        }

        av_packet_unref(&packet);
    }
    avformat_close_input(&input_ctx);

}


注意这里提取的h264 packet是包含0x00 00 00 01头部的一个完整的帧

原文链接:https://blog.csdn.net/zhangkai19890929/article/details/95198498

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐