代码片段---使用ffmpeg从h264文件中提取出一帧一帧数据

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

#include 
#include 
#include 
#include 

#ifdef __cplusplus
extern "C" {
#endif
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#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

你可能感兴趣的:(数据处理,ffmpeg)