当前位置: 首页 > news >正文

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

要对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.lryc.cn/news/260544.html

相关文章:

  • 8位LED流水灯设计
  • eclipse连接mysql数据库(下载eclipse,下载安装mysql,下载mysql驱动)
  • 【信息学奥赛】拼在起跑线上,想入道就别落下自己!
  • Python 进程池Pool Queue,运行不出来结果!
  • yolov8实战第二天——yolov8训练结果分析(保姆式解读)
  • ​urllib.request --- 用于打开 URL 的可扩展库​
  • 【Docker】进阶之路:(十二)Docker Composer
  • MES安灯管理:优化生产监控的重要工具
  • Unity中URP Shader 的 SRP Batcher
  • 十四 动手学深度学习v2计算机视觉 ——转置矩阵
  • Spark-Streaming+Kafka+mysql实战示例
  • C++改写为C
  • 抖去推--短视频剪辑、矩阵无人直播saas营销工具一站式开发
  • HBase 详细图文介绍
  • Hanlp自然语言处理如何再Spring Boot中使用
  • MySQL 是什么?
  • yarn link使用(npm link)
  • Docker容器讲解
  • three.js模拟太阳系
  • WPF仿网易云搭建笔记(1):项目搭建
  • DDOS 攻击是什么?有哪些常见的DDOS攻击?
  • 未来应用从何而来:认知力延伸、边界突破、回归云与产业
  • vue零基础
  • html中一个div中平均一行分配四个盒子,可展开与收起所有的盒子
  • Python虚拟环境指南:告别依赖地狱
  • 【Jeecg Boot 3 - 第二天】第2节 前后端docker部署云服务器
  • 2020年第九届数学建模国际赛小美赛A题自由泳解题全过程文档及程序
  • 双端队列和优先级队列
  • c#读取CSV文件跟Excel导入成DataTble
  • Python编程技巧 – 单字符函数