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

ffmpeg使用mjpeg把yuvj420p编码为jpg图像

version

#define LIBAVUTIL_VERSION_MAJOR  58

#define LIBAVUTIL_VERSION_MINOR  12

#define LIBAVUTIL_VERSION_MICRO 100

node

不使用AVOutputFormat

code

void CFfmpegOps::EncodeYUVJ420pToMJPEG(const char* infile, const char* width_str, const char* height_str, const char* outfile)
{if ((!infile) || (!outfile)){return;}int32_t width = 0;int32_t height = 0;FILE *in_fp = nullptr;size_t n = 0;AVCodecContext *encoder_ctx = nullptr;const AVCodec *encoder = nullptr;FILE* out_fp = nullptr;int ret = -1;AVFrame *avframe = nullptr;AVPacket *avpacket = nullptr;int frame_bytes = 0;try{width = std::stoi(width_str);height = std::stoi(height_str);}catch (std::exception &e){return;}in_fp = fopen(infile, "rb");if (!in_fp){printf("fopen error\n");goto end;}avframe = av_frame_alloc();if (!avframe){printf("av_frame_alloc error\n");goto end;}avframe->width = width;avframe->height = height;avframe->format = AV_PIX_FMT_YUVJ420P;avframe->pts = 0;// 获取单帧yuvj420p的字节数frame_bytes = av_image_get_buffer_size((AVPixelFormat)(avframe->format), avframe->width, avframe->height, 1);ret = av_frame_get_buffer(avframe, 0);if (ret < 0){printf("av_frame_get_buffer error:%s\n", GetFfmpegERR(ret));goto end;}// 读取y分量n = fread(avframe->data[0], sizeof(uint8_t), avframe->width * avframe->height, in_fp);if ((int)n != (avframe->width * avframe->height)){printf("n != (avframe->width * avframe->height)\n");goto end;}// 读取u分量n = fread(avframe->data[1], sizeof(uint8_t), avframe->width * avframe->height / 4, in_fp);if ((int)n != (avframe->width * avframe->height / 4)){printf("n != (avframe->width * avframe->height / 4)\n");goto end;}// 读取v分量n = fread(avframe->data[2], sizeof(uint8_t), avframe->width * avframe->height / 4, in_fp);if ((int)n != (avframe->width * avframe->height / 4)){printf("n != (avframe->width * avframe->height / 4)\n");goto end;}avpacket = av_packet_alloc();if (!avpacket){printf("av_packet_alloc error\n");goto end;}encoder = avcodec_find_encoder(AV_CODEC_ID_MJPEG);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->colorspace = ;// encoder_ctx->color_range = ;encoder_ctx->pix_fmt = AV_PIX_FMT_YUVJ420P;encoder_ctx->width = width;encoder_ctx->height = height;encoder_ctx->framerate.num = 25;encoder_ctx->framerate.den = 1;encoder_ctx->time_base.num = 1;encoder_ctx->time_base.den = 25;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("avcodec_alloc_context3 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){printf("avcodec_receive_packet error:%s\n", GetFfmpegERR(ret));break;}n = fwrite(avpacket->data, sizeof(uint8_t), avpacket->size, out_fp);}end:if (avpacket){av_packet_free(&avpacket);avpacket = nullptr;}if (avframe){av_frame_free(&avframe);avframe = nullptr;}if (out_fp){fclose(out_fp);out_fp = nullptr;}if (encoder_ctx){avcodec_free_context(&encoder_ctx);encoder_ctx = nullptr;}if (in_fp){fclose(in_fp);in_fp = nullptr;}
}

performance

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

相关文章:

  • 龙迅#LT6911GXC支持HDMI2.1转MIPI/4PORT LVDS应用功能,分辨率高达8K30HZ/4K120HZ压缩格式。
  • .NET 6.0 Web API项目中实现基于Token的身份验证
  • Java常用对象的快速初始化
  • 逻辑回归模型模拟实现:从零开始
  • Docker基本使用和认识
  • Halcon 文本文件操作,形态学
  • 【鸿蒙】稍微理解一下Stage模型
  • 毕业答辩制作PPT【攻略】
  • 深入解析npm install --save-dev:开发依赖管理的艺术
  • 福布斯 AI 50 榜单中唯一开源向量数据库:Weaviate
  • 信息学奥赛初赛天天练-38-CSP-J2021阅读程序-约数个数、约数和、埃氏筛法、欧拉筛法筛素数应用
  • 第100+13步 ChatGPT学习:R实现决策树分类
  • Hi3861 OpenHarmony嵌入式应用入门--LiteOS MessageQueue
  • ffmpeg编码图象时报错Invalid buffer size, packet size * < expected frame_size *
  • 解决类重复的问题
  • 使用 shell 脚本 统计app冷启动耗时
  • 使用容器部署redis_设置配置文件映射到本地_设置存储数据映射到本地_并开发java应用_连接redis---分布式云原生部署架构搭建011
  • 第五节:如何使用其他注解方式从IOC中获取bean(自学Spring boot 3.x的第一天)
  • Paragon NTFS与Tuxera NTFS有何区别 Mac NTFS 磁盘读写工具选哪个好
  • EtherCAT主站IGH-- 2 -- IGH之coe_emerg_ring.h/c文件解析
  • psensor 的手势功能
  • 使用 nvm 管理 Node 版本及 pnpm 安装
  • uni-appx使用form表单页面初始化报错
  • TiDB-从0到1-数据导出导入
  • 动手学深度学习(Pytorch版)代码实践 -卷积神经网络-16自定义层
  • 树莓派4设置
  • 44.商城系统(二十五):k8s基本操作,ingress域名访问,kubeSphere可视化安装
  • MySQL高级查询
  • 聊聊啥项目适合做自动化测试
  • ROS2开发机器人移动