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.pswp.cn/news/209921.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/209921.shtml
英文地址,請注明出處:http://en.pswp.cn/news/209921.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

兩個月軟考-高項上岸

文章目錄 前言結緣軟考功虧一簣有始有終2個月計劃資料部分計劃截圖 總結 前言 我們看小說或者電視劇電影都會看到這樣的情節&#xff0c;主角一開始錦衣玉食&#xff0c;突然家道中落&#xff0c;啥都沒了&#xff0c;主角再一路奮起重新找回了屬于自己的一切&#xff1b;還有…

Vue項目中實現瀏覽器標簽頁名字的動態修改

修改router/index.js文件 路由條目下面添加meta屬性 meta:{title:DevOps運維平臺 }示例 使用Vue的全局守衛函數beforeEach&#xff0c;在路由切換前動態修改瀏覽器標簽頁名字 router.beforeEach((to,from,next) > {document.title to.meta.titlenext() })

Error: Cannot find module ‘E:\Workspace_zwf\mall\build\webpack.dev.conf.js‘

執行&#xff1a;npm run dev E:\Workspace_zwf\zengwenfeng-master>npm run dev> mall-app-web1.0.0 dev E:\Workspace_zwf\zengwenfeng-master > webpack-dev-server --inline --progress --config build/webpack.dev.conf.jsinternal/modules/cjs/loader.js:983thr…

[筆記]ARMv7/ARMv8 交叉編譯器下載

開發 Cortex-A7、Cortex-A72 或其他 ARM 架構 profile 芯片時&#xff0c;經常需要下載對應架構的交叉編譯器&#xff0c;所以寫這篇筆記&#xff0c;用于記錄一下交叉編譯器下載流程&#xff0c;免得搞忘。 編譯環境&#xff1a;ubuntu 虛擬機 下載地址 我們可以從 ARM 官網…

09 視頻分片上傳Minio和播放

文章目錄 一、流程設計1. 分片上傳實現思路2. 文件分片上傳流程3. 視頻播放流程 二、代碼實現1. 后端代碼2. 文件上傳前端代碼3. 視頻播放前端代碼 一、流程設計 1. 分片上傳實現思路 2. 文件分片上傳流程 3. 視頻播放流程 二、代碼實現 1. 后端代碼 pom.xml <dependenc…

多線程案例-單例模式

單例模式 設計模式的概念 設計模式好比象棋中的"棋譜".紅方當頭炮,黑方馬來跳.針對紅方的一些走法,黑方應招的時候有一些固定的套路.按照套路來走局勢就不會吃虧. 軟件開發中也有很多常見的"問題場景".針對這些問題的場景,大佬們總結出了一些固定的套路.按…

vue實現可拖拽列表

直接上代碼 <!-- vue實現可拖拽列表 --> <template><div><button click"logcolig">打印數據</button><TransitionGroup name"list" tag"div" class"container"><divclass"item"v-f…

常見請求頭與響應頭你了解哪些?

常見的 HTTP 請求頭和響應頭包括&#xff1a; 常見的請求頭&#xff1a; User-Agent&#xff1a;標識客戶端代理信息&#xff0c;通常用于識別用戶使用的瀏覽器或設備類型。 Accept&#xff1a;指示客戶端可以接受的內容類型&#xff0c;例如 text/html, application/json 等…

深度學習記錄--激活函數

激活函數的種類 對于激活函數的選擇&#xff0c;通常有以下幾種 sigmoid&#xff0c;tanh&#xff0c;ReLU&#xff0c;leaky ReLU 激活函數的選擇 之前logistic回歸一直使用的激活函數都是sigmoid函數&#xff0c;但一般來說&#xff0c;tanh函數是比sigmoid函數更加好的選…

【Python】 生成二維碼

創建了一個使用 python 創建二維碼的程序。 下面是生成的程序的圖像。 功能描述 輸入網址&#xff08;URL&#xff09;。 輸入二維碼的名稱。 當單擊 QR 碼生成按鈕時&#xff0c;將使用 QRname 中輸入的字符將 QR 碼生成為圖像。 程序代碼 import qrcode import tkinterd…

java泛型:泛型類,泛型方法

今日記錄我的泛型使用&#xff0c;供后期查閱。 主要包含泛型類&#xff0c;泛型屬性&#xff0c;泛型方法&#xff0c;靜態方法中使用泛型。 public class GenericOperationResultRep<T> {private boolean success; // 是否操作成功。true&#xff0c;成功&#xff1b;f…

Oracle的錯誤信息幫助:Error Help

今天看手冊時&#xff0c;發現上面有個提示&#xff1a; Error messages are now available in Error Help. 點擊 View Error Help&#xff0c;顯示如下&#xff0c;其實就是oerr命令的圖形化版本&#xff1a; 點擊Database Error Message Index&#xff0c;以下界面等同于命令…

[Kadane算法,前綴和思想]元素和最大的子矩陣

元素和最大的子矩陣 題目描述 輸入一個n級方陣&#xff0c;請找到此矩陣的一個子矩陣&#xff0c;此子矩陣的各個元素的和是所有子矩陣中最大的&#xff0c;輸出這個子矩陣及這個最大的和。 關于輸入 首先輸入方陣的級數n&#xff0c; 然后輸入方陣中各個元素。 關于輸出 …

車載藍牙音樂流程簡單分析

關鍵類&#xff1a; /packages/apps/Bluetooth/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerStateMachine.java /packages/apps/Bluetooth/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerService.java 一、音樂播放狀態 CPP中通過JNI接口將接從…

Python中利用遺傳算法探索迷宮出路

更多資料獲取 &#x1f4da; 個人網站&#xff1a;ipengtao.com 當處理迷宮問題時&#xff0c;遺傳算法提供了一種創新的解決方案。本文將深入探討如何運用Python和遺傳算法來解決迷宮問題。迷宮問題是一個經典的尋路問題&#xff0c;尋找從起點到終點的最佳路徑。遺傳算法是一…

ActiveMQ斷線重連技巧,即通信高可用的配置

最近在做一個內部應用的時候&#xff0c;應用到了ActiveMQ作為服務之間消息傳遞&#xff0c;解耦服務之間的關聯&#xff0c;但是在應用的過程中遇到了連接斷線無法重連的問題&#xff0c;下面基于這個問題&#xff0c;深入了解一下ActiveMQ的一些相關原理和知識。 一、前置知…

springboot2 在Java項目中你們是如何配置時間格式響應給前端呢

在 Spring Boot 2 項目中配置時間格式&#xff0c;通常可以通過配置文件&#xff08;application.properties 或 application.yml&#xff09;或者通過 Java 代碼進行配置。以下是兩種常見的配置方式&#xff1a; 1. 通過配置文件配置時間格式&#xff1a; 在 application.pr…

mybaties plus插入數據,自動回顯 機制

結論&#xff1a;mybaties plus會將庫里數據自動回顯到 要插入的數據上 測試表格 SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS 0;-- 表結構 DROP TABLE IF EXISTS t_stu; CREATE TABLE t_stu (id int NOT NULL COMMENT id,name varchar(255) CHARACTER SET utf8mb4 COLLATE…

【PyTorch】計算設備

文章目錄 1. 介紹2. 查詢和使用 1. 介紹 CPU設備意味著所有物理CPU和內存&#xff0c; 這意味著PyTorch的計算將嘗試使用所有CPU核心。可以用以下方式表示&#xff1a; torch.device(cpu) GPU設備只代表一個GPU和相應的顯存。 torch.device(cuda)如果有多個GPU&#xff0c;我們…

Java解決矩陣對角線元素的和問題

Java解決矩陣對角線元素的和問題 01 題目 給你一個正方形矩陣 mat&#xff0c;請你返回矩陣對角線元素的和。 請你返回在矩陣主對角線上的元素和副對角線上且不在主對角線上元素的和。 示例 1&#xff1a; 輸入&#xff1a;mat [[1,2,3],[4,5,6],[7,8,9]] 輸出&#xff1a…