FFmpeg分离出YUV数据

本文将使用FFmpeg从mp4文件中提取YUV数据保存到本地,主要步骤如下:
1、使用avformat_open_input函数打开输入文件获取AVFormatContext上下文ifmt_ctx;
2、使用avformat_find_stream_info函数查找流信息;
3、使用av_find_best_stream函数得到视频流的索引stream_video_index和解码器video_codec;
4、通过上面得到的video_codec使用avcodec_alloc_context3函数初始化解码器,得到解码器上下文AVCodecContext video_codec_ctx;
5、通过avcodec_parameters_to_context函数把视频流的参数信息赋值给video_codec_ctx;
6、使用avcodec_open2函数打开编码器;
7、使用av_read_frame函数从ifmt_ctx上下文中读取数据包;
8、使用avcodec_send_packetavcodec_receive_frame函数解码,得到原始数据AVFrame,这就是咱们需要的YUV数据,保存到本地就行了。
代码如下:

    AVFormatContext *ifmt_ctx = NULL;
    int stream_video_index = -1;
    AVCodec *video_codec = NULL;
    AVCodecContext *video_codec_ctx = NULL;
    AVPacket *pkt = av_packet_alloc();
    AVFrame *yuv_frame = av_frame_alloc();
    int ret = 0;
    const char *input_file = "./bb1.mp4";
    const char *output_file = "./bb1_test.yuv";
    
    FILE *f = fopen(output_file, "wb");
    ret = avformat_open_input(&ifmt_ctx, input_file, NULL, NULL);
    if (ret<0) {
        av_log(NULL, AV_LOG_ERROR, "打开输入文件报错");
        return -1;
    }
    
    avformat_find_stream_info(ifmt_ctx, NULL);
    stream_video_index = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &video_codec, 0);

    video_codec_ctx = avcodec_alloc_context3(video_codec);
    avcodec_parameters_to_context(video_codec_ctx, ifmt_ctx->streams[stream_video_index]->codecpar);
    avcodec_open2(video_codec_ctx, video_codec, NULL);
    
    AVFrame *yuv420_frame = av_frame_alloc();
    yuv420_frame->format = AV_PIX_FMT_YUV420P;
    yuv420_frame->width = video_codec_ctx->width;
    yuv420_frame->height = video_codec_ctx->height;
    av_frame_get_buffer(yuv420_frame, 1);
    
    struct SwsContext *sws_ctx = sws_getContext(video_codec_ctx->width, video_codec_ctx->height, video_codec_ctx->pix_fmt, yuv420_frame->width, yuv420_frame->height, yuv420_frame->format, SWS_BICUBIC, NULL, NULL, NULL);
    
    
    while (1) {
        ret = av_read_frame(ifmt_ctx, pkt);
        if (ret<0) {
            break;
        }
        if (pkt->stream_index!=stream_video_index) {
            continue;
        }
        avcodec_send_packet(video_codec_ctx, pkt);
        while (1) {
            ret = avcodec_receive_frame(video_codec_ctx, yuv_frame);
            if (ret==AVERROR(EAGAIN) || ret==AVERROR_EOF) {
                break;
            }
            sws_scale(sws_ctx, yuv_frame->data, yuv_frame->linesize, 0, yuv_frame->height, yuv420_frame->data, yuv420_frame->linesize);
            
            size_t size = yuv420_frame->width * yuv420_frame->height;
            
            ret = fwrite(yuv420_frame->data[0], 1, size, f);
            fwrite(yuv420_frame->data[1], 1, size, f);
            fwrite(yuv420_frame->data[2], 1, size, f);
            av_frame_unref(yuv_frame);
        }
        av_packet_unref(pkt);
    }
    
        
    avcodec_send_packet(video_codec_ctx, NULL);
    while (1) {
        ret = avcodec_receive_frame(video_codec_ctx, yuv_frame);
        if (ret==AVERROR(EAGAIN) || ret==AVERROR_EOF) {
            break;
        }
        sws_scale(sws_ctx, yuv_frame->data, yuv_frame->linesize, 0, yuv_frame->height, yuv420_frame->data, yuv420_frame->linesize);
        
        size_t size = yuv420_frame->width * yuv420_frame->height;
        
        ret = fwrite(yuv420_frame->data[0], 1, size, f);
        fwrite(yuv420_frame->data[1], 1, size/4, f);
        fwrite(yuv420_frame->data[2], 1, size/4, f);
        av_frame_unref(yuv_frame);
    }
    av_packet_unref(pkt);
    av_frame_unref(yuv420_frame);
    avcodec_close(video_codec_ctx);
    avformat_close_input(&ifmt_ctx);
    avformat_free_context(ifmt_ctx);
    fclose(f);

这样咱们就把mp4文件中的视频流取出来保存到本地了,通过ffplay -s WxH -pix_fmt yuv420p input.yuv命令就可以播放了

你可能感兴趣的:(FFmpeg分离出YUV数据)