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

本文介绍如何使用ffmpeg库从D盘的test.264 H264视频文件中逐帧提取数据,适合处理视频流并提取帧信息的场景。

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.youkuaiyun.com/zhangkai19890929/article/details/95198498

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值