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

ffmpeg使用bmp编码器把bgr24编码为bmp图像

version

#define LIBAVCODEC_VERSION_MAJOR  60

#define LIBAVCODEC_VERSION_MINOR  15

#define LIBAVCODEC_VERSION_MICRO 100

note

不使用AVOutputFormat

code

void CFfmpegOps::EncodeBGR24ToBMP(const char* infile, const char* width_str, const char* height_str, const char *outfile)
{if ((!infile) || (!width_str) || (!height_str) || (!outfile)){return;}int width = 0;int height = 0;AVFrame *avframe = nullptr;int frame_bytes = 0;AVPixelFormat src_fmt = AV_PIX_FMT_BGR24;int ret = -1;FILE *in_fp = nullptr;size_t n = 0;AVPacket *avpacket = nullptr;const AVCodec *encoder = nullptr;AVCodecContext *encoder_ctx = nullptr;FILE *out_fp = nullptr;try{width = std::stoi(width_str);height = std::stoi(height_str);}catch (const std::exception &e){return;}avframe = av_frame_alloc();if (!avframe){printf("av_frame_alloc error\n");goto end;}avframe->format = src_fmt;avframe->width = width;avframe->height = height;frame_bytes = av_image_get_buffer_size(src_fmt, width, height, 1);ret = av_frame_get_buffer(avframe, 0);if (ret < 0){printf("av_frame_get_buffer error(%s)\n", GetFfmpegERR(ret));goto end;}in_fp = fopen(infile, "rb");if (!in_fp){printf("fopen error\n");goto end;}n = fread(avframe->data[0], sizeof(uint8_t), frame_bytes, in_fp);if ((int)n != (frame_bytes)){printf("n != (frame_bytes)\n");goto end;}avpacket = av_packet_alloc();if (!avpacket){printf("av_packet_alloc error\n");goto end;}encoder = avcodec_find_encoder(AV_CODEC_ID_BMP);if (!encoder){printf("avcodec_find_encoder error\n");goto end;}encoder_ctx = avcodec_alloc_context3(encoder);if (!encoder_ctx){printf("avcodec_alloc_context3 error\n");goto end;}encoder_ctx->pix_fmt = src_fmt;encoder_ctx->width = width;encoder_ctx->height = height;encoder_ctx->time_base.num = 1;encoder_ctx->time_base.den = 25;encoder_ctx->framerate.num = 25;encoder_ctx->framerate.den = 1;encoder_ctx->bit_rate = frame_bytes * encoder_ctx->framerate.num * 8;ret = avcodec_open2(encoder_ctx, encoder, nullptr);if (ret < 0){printf("avcodec_open2 error(%s)\n", GetFfmpegERR(ret));goto end;}out_fp = fopen(outfile, "wb");if (!out_fp){printf("fopen error\n");goto end;}ret = avcodec_send_frame(encoder_ctx, avframe);if (ret != 0){printf("avcodec_send_frame error(%s)\n", GetFfmpegERR(ret));goto end;}while (1){ret = avcodec_receive_packet(encoder_ctx, avpacket);if (ret != 0){if (ret == AVERROR(EAGAIN)){continue;}printf("avcodec_receive_packet error(%s)\n", GetFfmpegERR(ret));break;}n = fwrite(avpacket->data, avpacket->size, sizeof(uint8_t), out_fp);av_packet_unref(avpacket);break;}end:if (out_fp){fclose(out_fp);out_fp = nullptr;}if (encoder_ctx){avcodec_free_context(&encoder_ctx);encoder_ctx = nullptr;}if (avpacket){av_packet_free(&avpacket);avpacket = nullptr;}if (in_fp){fclose(in_fp);in_fp = nullptr;}if (avframe){av_frame_free(&avframe);avframe = nullptr;}}

performance

http://www.lryc.cn/news/393358.html

相关文章:

  • 基于YOLOv10+YOLOP+PYQT的可视化系统,实现多类别目标检测+可行驶区域分割+车道线分割【附代码】
  • 计算机网络之令牌总线
  • 策略模式的应用
  • 如何使用uer做多分类任务
  • 【HICE】转发服务器实验
  • MATLAB-分类CPO-RF-Adaboost冠豪猪优化器(CPO)优化RF随机森林结合Adaboost分类预测(二分类及多分类)
  • 绝区贰--及时优化降低 LLM 成本和延迟
  • JDBC【封装工具类、SQL注入问题】
  • Windows打开redis以及Springboot整合redis
  • MySQL使用LIKE索引是否失效的验证
  • 封装日历uniapp,只显示年月不显示日
  • golang线程池ants-实现架构
  • Mysql面试合集
  • Android Gradle 开发与应用 (五): 构建变体与自定义任务
  • Django学习第六天
  • docker部署mycat,连接上面一篇的一主二从mysql
  • VUE2拖拽组件:vue-draggable-resizable-gorkys
  • 容器:stack
  • 跨平台Ribbon UI组件QtitanRibbon全新发布v6.7.0——支持Qt 6.6.3
  • (6) 深入探索Python-Pandas库的核心数据结构:DataFrame全面解析
  • 在 Azure 云中开始使用适用于 Ubuntu 的 Grafana
  • 1.Python学习笔记
  • 中英双语介绍百老汇著名歌剧:《猫》(Cats)和《剧院魅影》(The Phantom of the Opera)
  • RpcChannel的调用过程
  • 东芝TB6560AHQ/AFG步进电机驱动IC:解锁卓越的电机控制性能
  • 免杀笔记 ----> DLL注入
  • 奇迹MU 骷髅战士在哪
  • leetcode力扣_贪心思想
  • Vue中Class数据绑定
  • Python数据分析案例49——基于机器学习的垃圾邮件分类系统构建(朴素贝叶斯,支持向量机)