C++如何通过调用ffmpeg接口对H265文件进行编码和解码

news/2024/7/10 21:14:31 标签: c++, ffmpeg, h265, 编码, 解码, demo, 示例

要对H265文件进行编码解码,需要使用FFmpeg库提供的相关API。以下是一个简单的C++程序,演示如何使用FFmpeg进行H265文件的编码解码

编码

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cstdint>

extern "C" {
#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>
}

int main(int argc, char** argv) {
    AVCodec* codec = nullptr;
    AVCodecContext* codec_ctx = nullptr;
    AVFrame* frame = nullptr;
    AVPacket packet;
    FILE* infile, * outfile;

    avcodec_register_all();

    codec = avcodec_find_encoder_by_name("libx265");

    codec_ctx = avcodec_alloc_context3(codec);
    codec_ctx->bit_rate = 4000000;
    codec_ctx->width = 1280;
    codec_ctx->height = 720;
    codec_ctx->gop_size = 10;
    codec_ctx->max_b_frames = 1;
    codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;

    av_opt_set(codec_ctx->priv_data, "preset", "fast", 0);

    if (avcodec_open2(codec_ctx, codec, nullptr) < 0) {
        fprintf(stderr, "Could not open codec\n");
        return EXIT_FAILURE;
    }

    infile = std::fopen("input.yuv", "rb");
    if (!infile) {
        fprintf(stderr, "Could not open input file\n");
        return EXIT_FAILURE;
    }

    outfile = std::fopen("output.h265", "wb");
    if (!outfile) {
        fprintf(stderr, "Could not open output file\n");
        return EXIT_FAILURE;
    }

    frame = av_frame_alloc();
    frame->width = codec_ctx->width;
    frame->height = codec_ctx->height;
    frame->format = codec_ctx->pix_fmt;
    if (av_frame_get_buffer(frame, 32) < 0) {
        fprintf(stderr, "Could not allocate frame buffer\n");
        return EXIT_FAILURE;
    }

    int frame_size = av_image_get_buffer_size(codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, 1);
    uint8_t* frame_buf = (uint8_t*)av_malloc(frame_size);

    while (true) {
        int ret = fread(frame_buf, 1, frame_size, infile);
        if (ret < frame_size) {
            break;
        }

        av_frame_make_writable(frame);

        uint8_t* pY = frame_buf;
        uint8_t* pU = frame_buf + frame_size / 4;
        uint8_t* pV = frame_buf + frame_size * 5 / 16;

        for (int y = 0; y < codec_ctx->height; y++) {
            memcpy(frame->data[0] + y * frame->linesize[0], pY + y * codec_ctx->width, codec_ctx->width);
        }

        for (int y = 0; y < codec_ctx->height / 2; y++) {
            memcpy(frame->data[1] + y * frame->linesize[1], pU + y * codec_ctx->width / 2, codec_ctx->width / 2);
            memcpy(frame->data[2] + y * frame->linesize[2], pV + y * codec_ctx->width / 2, codec_ctx->width / 2);
        }

        avcodec_send_frame(codec_ctx, frame);

        while (true) {
            ret = avcodec_receive_packet(codec_ctx, &packet);
            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
                break;
            } else if (ret < 0) {
                fprintf(stderr, "Error encoding frame\n");
                return EXIT_FAILURE;
            }

            fwrite(packet.data, 1, packet.size, outfile);
            av_packet_unref(&packet);
        }
    }

    avcodec_send_frame(codec_ctx, nullptr);

    while (true) {
        int ret = avcodec_receive_packet(codec_ctx, &packet);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
            break;
        } else if (ret < 0) {
            fprintf(stderr, "Error encoding frame\n");
            return EXIT_FAILURE;
        }

        fwrite(packet.data, 1, packet.size, outfile);
        av_packet_unref(&packet);
    }

    std::fclose(infile);
    std::fclose(outfile);
    av_frame_free(&frame);
    av_free(frame_buf);
    avcodec_free_context(&codec_ctx);

    return EXIT_SUCCESS;
}

解码

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cstdint>

extern "C" {
#include <libavcodec/avcodec.h>
#include <libavutil/frame.h>
#include <libavutil/imgutils.h>
}

int main(int argc, char** argv) {
    AVCodec* codec = nullptr;
    AVCodecContext* codec_ctx = nullptr;
    AVFrame* frame = nullptr;
    AVPacket packet;
    FILE* infile, * outfile;

    avcodec_register_all();

    codec = avcodec_find_decoder_by_name("libx265");

    codec_ctx = avcodec_alloc_context3(codec);

    if (avcodec_open2(codec_ctx, codec, nullptr) < 0) {
        fprintf(stderr, "Could not open codec\n");
        return EXIT_FAILURE;
    }

    infile = std::fopen("input.h265", "rb");
    if (!infile) {
        fprintf(stderr, "Could not open input file\n");
        return EXIT_FAILURE;
    }

    outfile = std::fopen("output.yuv", "wb");
    if (!outfile) {
        fprintf(stderr, "Could not open output file\n");
        return EXIT_FAILURE;
    }

    frame = av_frame_alloc();

    while (true) {
        int ret = av_read_frame(format_ctx, &packet);
        if (ret < 0) {
            break;
        }

        if (packet.stream_index != video_stream_idx) {
            av_packet_unref(&packet);
            continue;
        }

        avcodec_send_packet(codec_ctx, &packet);

        while (true) {
            ret = avcodec_receive_frame(codec_ctx, frame);
            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
                break;
            } else if (ret < 0) {
                fprintf(stderr, "Error decoding frame\n");
                return EXIT_FAILURE;
            }

            uint8_t* pY = frame->data[0];
            uint8_t* pU = frame->data[1];
            uint8_t* pV = frame->data[2];

            for (int y = 0; y < codec_ctx->height; y++) {
                fwrite(pY + y * frame->linesize[0], 1, codec_ctx->width, outfile);
            }

            for (int y = 0; y < codec_ctx->height / 2; y++) {
                fwrite(pU + y * frame->linesize[1], 1, codec_ctx->width / 2, outfile);
                fwrite(pV + y * frame->linesize[2], 1, codec_ctx->width / 2, outfile);
            }

            av_frame_unref(frame);
        }

        av_packet_unref(&packet);
    }

    std::fclose(infile);
    std::fclose(outfile);
    av_frame_free(&frame);
    avcodec_free_context(&codec_ctx);

    return EXIT_SUCCESS;
}

需要注意的是,以上程序中的一些常量参数可能需要根据实际情况进行调整。


http://www.niftyadmin.cn/n/5249470.html

相关文章

Flask template中使用iframe

Flaks template中使用iframe嵌套新的网页&#xff08;new_page.html&#xff09;的网页到历史网页&#xff08;old_page.html&#xff09;中&#xff08;减少新网页的入口&#xff09; 1,增加iframe tag 在old_page.html中适当位置增加iframe入口标签&#xff1a; <ifram…

C_15练习题

一、单项选择题(本大题共20小题,每小题2分,共40分。在每小题给出的四个备选须中选出一个正确的答案,并将所选项前的字母填写在答题纸的相应位置上。) C程序是由&#xff08;&#xff09;构成的。 A.常量 B.变量 C.运算符 D. 函数 下列合法的标识符是&#xff08;&#xff09; …

YOLOv4 学习笔记

文章目录 前言一、YOLOv4贡献和改进二、YOLOv4核心概念三、YOLOv4网络架构四、YOLOv4数据增强五、YOLOv4的损失函数总结 前言 在近年来的目标检测领域&#xff0c;YOLOv4的出现标志着一个重要的技术突破。YOLOv4不仅继承了YOLO系列快速、高效的特点&#xff0c;还引入了一系列…

使用AWS Glue与AWS Kinesis构建的流式ETL作业(一)——数据实时采集

大纲 1 数据采集准备工作1.1 研究的背景1.2 使用Glue构建流式ETL的原因1.3 无服务器流式ETL架构1.4 架构1.5 AWS Kinesis Data Stream创建1.6 AWS CloudWatch数据筛选1.6.1 AWS IAM角色权限1.6.1.1 可信实体1.6.1.2 策略 1.7 AWS Kinesis中的数据验证1.7.1 验证代码1.7.2 结果 …

自动化操作脚本

文章目录 vbsopenCV pyautogui vbs SSH连接并执行指令操作 Dim WshShell Set WshShellWScript.CreateObject("WScript.Shell") WshShell.Run "cmd.exe" WScript.Sleep 1000 WshShell.SendKeys "ssh xcmg10.27.40.103" WshShell.SendKeys &qu…

IP地址如何用于流量管理?

随着互联网的普及和网络流量的不断增加&#xff0c;流量管理成为了网络运营中至关重要的一环。而IP地址作为互联网中的重要标识符&#xff0c;也可以被广泛应用于流量管理中。 IP地址是互联网协议&#xff08;IP&#xff09;中用于标识和定位网络设备的32位二进制地址。通过IP地…

屏幕分辨率修改工具SwitchResX mac功能特点

SwitchResX mac是可用于修改和管理显示器的分辨率和刷新率。 SwitchResX mac功能和特点 支持多种分辨率和刷新率&#xff1a;SwitchResX可以添加和管理多种分辨率和刷新率&#xff0c;包括自定义分辨率和刷新率。 自动切换分辨率&#xff1a;SwitchResX可以根据应用程序和窗口…

docker 资源控制

Docker的资源控制 对容器使用宿主机的资源进行限制&#xff0c;如cpu&#xff0c;内存&#xff0c;磁盘I/O Docker使用linux自带的功能cgroup(control grouos)是linux内核系统提供的一种可以限制&#xff0c;记录&#xff0c;隔离进程组使用的物理资源 Docker借助这个机制&…