【FFMPEG应用篇】MP4转YUV存储

extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
}

int main() {
    av_register_all();

    AVFormatContext* formatContext = avformat_alloc_context();
    if (avformat_open_input(&formatContext, "4k.mp4", NULL, NULL) != 0) {
        fprintf(stderr, "Error opening input file.\n");
        return -1;
    }

    if (avformat_find_stream_info(formatContext, NULL) < 0) {
        fprintf(stderr, "Error finding stream information.\n");
        return -1;
    }

    av_dump_format(formatContext, 0, "4k.mp4", 0);

    AVCodec* codec = NULL;
    AVCodecContext* codecContext = NULL;
    int videoStreamIndex = -1;
    for (int i = 0; i < formatContext->nb_streams; i++) {
        if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
            videoStreamIndex = i;
            codec = avcodec_find_decoder(formatContext->streams[i]->codecpar->codec_id);
            codecContext = avcodec_alloc_context3(codec);
            avcodec_parameters_to_context(codecContext, formatContext->streams[i]->codecpar);
            avcodec_open2(codecContext, codec, NULL);
            break;
        }
    }

    AVPacket packet;
    av_init_packet(&packet);

    AVFrame* frame = av_frame_alloc();
    AVFrame* frameRGB = av_frame_alloc();

    struct SwsContext* swsContext = sws_getContext(codecContext->width, codecContext->height, codecContext->pix_fmt, codecContext->width, codecContext->height, AV_PIX_FMT_YUV420P, SWS_BILINEAR, NULL, NULL, NULL);

    while (av_read_frame(formatContext, &packet) >= 0) {
        if (packet.stream_index == videoStreamIndex) {
            int response = avcodec_send_packet(codecContext, &packet);
            if (response < 0) {
                fprintf(stderr, "Error decoding frame.\n");
                return -1;
            }
            response = avcodec_receive_frame(codecContext, frame);
            if (response == AVERROR(EAGAIN) || response == AVERROR_EOF) {
                continue;
            }
            else if (response < 0) {
                fprintf(stderr, "Error decoding frame.\n");
                return -1;
            }

            av_image_alloc(frameRGB->data, frameRGB->linesize, codecContext->width, codecContext->height, AV_PIX_FMT_YUV420P, 1);

            sws_scale(swsContext, (uint8_t const* const*)frame->data, frame->linesize, 0, codecContext->height, frameRGB->data, frameRGB->linesize);

            // Save frame as YUV file
            FILE* yuvFile = fopen("4k.yuv", "wb");
            if (yuvFile) {
                fwrite(frameRGB->data[0], 1, codecContext->width * codecContext->height, yuvFile); // Y
                fwrite(frameRGB->data[1], 1, codecContext->width * codecContext->height / 4, yuvFile); // U
                fwrite(frameRGB->data[2], 1, codecContext->width * codecContext->height / 4, yuvFile); // V
                fclose(yuvFile);
            }

            av_frame_unref(frame);
            av_frame_unref(frameRGB);
        }
        av_packet_unref(&packet);
    }

    av_frame_free(&frame);
    av_frame_free(&frameRGB);
    avcodec_free_context(&codecContext);
    avformat_close_input(&formatContext);
    avformat_free_context(formatContext);
    sws_freeContext(swsContext);

    return 0;
}

相关推荐

  1. FFMPEG应用MP4YUV存储

    2024-05-26 00:20:29       32 阅读
  2. FFMPEG应用】基于FFmpeg应用(FLV MP4)

    2024-05-26 00:20:29       53 阅读
  3. ffmpeg把视频文件码为MP4格式

    2024-05-26 00:20:29       34 阅读
  4. 音视频开发——FFmpeg 实现MP4FLV文件 C语言实现

    2024-05-26 00:20:29       24 阅读
  5. ffmpeg转换MP4为gif命令

    2024-05-26 00:20:29       22 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-26 00:20:29       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-26 00:20:29       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-26 00:20:29       87 阅读
  4. Python语言-面向对象

    2024-05-26 00:20:29       96 阅读

热门阅读

  1. 37、Flink 的窗口函数(Window Functions)详解

    2024-05-26 00:20:29       24 阅读
  2. Amazon Cognito:AWS用户身份管理服务的全面指南

    2024-05-26 00:20:29       37 阅读
  3. FreeRTOS学习 -- 任务 API 函数

    2024-05-26 00:20:29       39 阅读
  4. SSL/TLS协议信息泄露漏洞(CVE-2016-2183)【原理扫描】

    2024-05-26 00:20:29       34 阅读
  5. android ndc firewall 命令type 黑名单 白名单差异

    2024-05-26 00:20:29       33 阅读
  6. 29.修改idea中git的提交记录上的提交名

    2024-05-26 00:20:29       32 阅读