ffmpeg保存YUV

#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"avutil.lib")
#pragma comment(lib,"avdevice.lib")
#pragma comment(lib,"swresample.lib")
#pragma comment(lib,"postproc.lib")
#pragma comment(lib,"swscale.lib")

#include <iostream>
#include <fstream>
extern "C" {
#include <libavformat/avformat.h>
}

using std::cin;
using std::cout;
using std::endl;
using std::ofstream;
using std::ios_base;

int main() {
    AVFormatContext* fmt_ctx = nullptr;
    const char* in_path = "./两只老虎.mp4";
    const char* out_path = "./out.yuv";
    if (avformat_open_input(&fmt_ctx, in_path, nullptr, nullptr) != 0) {
        cout << "avformat_open_input错误" << endl;
        system("pause");
        return 1;
    }

    //打印出视频信息
    //av_dump_format(fmt_ctx,0,in_path,0);

    if (avformat_find_stream_info(fmt_ctx, nullptr) < 0) {
        avformat_free_context(fmt_ctx);
        cout << "avformat_find_stream_info错误" << endl;
        system("pause");
        return 1;
    }

    AVCodec* codec;
    AVCodecContext* cd_ctx;
    unsigned video_index = 0;

    for (unsigned i = 0; fmt_ctx->nb_streams; i++) {
        //寻找到视频流
        if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
            video_index = i;

            //获取到视频流
            AVStream* streams = fmt_ctx->streams[i];
            //streams->r_frame_rate
            cout << ((float)streams->avg_frame_rate.num) / streams->avg_frame_rate.den << "fps,";

            //视频编码类型
            cout << avcodec_get_name(streams->codecpar->codec_id) << endl;

            codec = avcodec_find_decoder(streams->codecpar->codec_id);
            if (!codec) {
                cout << "没有找到解码器" << endl;
                system("pause");
                return 1;
            }

            //分配AVCodecContext空间
            cd_ctx = avcodec_alloc_context3(codec);

            //填充数据
            avcodec_parameters_to_context(cd_ctx, streams->codecpar);

            //视频尺寸
            cout << streams->codecpar->width << 'x' << streams->codecpar->height << endl;
            break;
        }
    }

    /* open it */

    if (avcodec_open2(cd_ctx, codec, NULL) < 0) {
        cout << "avcodec_open2错误" << endl;
        system("pause");
        return 1;
    }

    AVPacket* pkt = av_packet_alloc();
    if (!pkt) {
        cout << "av_packet_alloc错误" << endl;
        system("pause");
        return 1;
    }

    AVFrame* frame_yuv = av_frame_alloc();
    if (!frame_yuv) {
        cout << "av_frame_alloc错误" << endl;
        system("pause");
        return 1;
    }

    //打开输出视频的文件
    ofstream out = ofstream(out_path, ios_base::binary);

    int frame_count = 0;//记录获取的帧数

    while (av_read_frame(fmt_ctx, pkt) >= 0 && frame_count < 1000) {//这里只获取100帧了

        //找到视频流
        if (pkt->stream_index == video_index) {
            if (avcodec_send_packet(cd_ctx, pkt) != 0) {
                cout << "avcodec_send_packet错误" << endl;
                break;
            }
            while (avcodec_receive_frame(cd_ctx, frame_yuv) == 0) {

                frame_count++;

                //AV_PIX_FMT_YUV420P,  ///< planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
                cout << "解码到第" << cd_ctx->frame_number << "帧" << endl;
                for (int i = 0; i < frame_yuv->height; i++) {
                    out.write((char*)(frame_yuv->data[0] + i * frame_yuv->linesize[0]), frame_yuv->width);
                }

                int loop = frame_yuv->height / 2;
                int len_uv = frame_yuv->width / 2;

                for (int i = 0; i < loop; i++) {
                    out.write((char*)(frame_yuv->data[1] + i * frame_yuv->linesize[1]), len_uv);
                }
                for (int i = 0; i < loop; i++) {
                    out.write((char*)(frame_yuv->data[2] + i * frame_yuv->linesize[2]), len_uv);
                }
            }
        }
    }

    cout << "解码完毕,文件保存在" << out_path << endl;

    out.close();

    av_packet_free(&pkt);
    avcodec_close(cd_ctx);
    avcodec_free_context(&cd_ctx);
    av_frame_free(&frame_yuv);
    avformat_close_input(&fmt_ctx);

    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值