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

[FFmpeg学习]从视频中获取图片

从视频中获取图片是一个比较直观的例子,这里从一个基础的例子来查看FFmpeg相关api的使用,从mp4文件中获取一帧图像,保存为jpeg格式图片,mp4文件比较好准备,一般手机录屏文件就是mp4格式。

原理还是比较清楚,得到一个AVFrame后,再使用jpeg的编码器来转换

int getpic() {std::string filename = "test.mp4";     // 输入MP4文件名std::string outputFilename = "output.jpg";  // 输出图片文件名int targetSecond = 1;    // 目标秒数AVFormatContext* formatContext = nullptr;if (avformat_open_input(&formatContext, filename.c_str(), nullptr, nullptr) != 0) {std::cerr << "Error opening input file" << std::endl;return -1;}if (avformat_find_stream_info(formatContext, nullptr) < 0) {std::cerr << "Error finding stream information" << std::endl;avformat_close_input(&formatContext);return -1;}const AVCodec* codec = nullptr;int videoStreamIndex = -1;for (unsigned int i = 0; i < formatContext->nb_streams; i++) {if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {videoStreamIndex = i;codec = avcodec_find_decoder(formatContext->streams[i]->codecpar->codec_id);break;}}if (videoStreamIndex == -1 || codec == nullptr) {std::cerr << "Error finding video stream or decoder" << std::endl;avformat_close_input(&formatContext);return -1;}AVCodecContext* codecContext = avcodec_alloc_context3(codec);if (codecContext == nullptr) {std::cerr << "Error allocating codec context" << std::endl;avformat_close_input(&formatContext);return -1;}if (avcodec_parameters_to_context(codecContext, formatContext->streams[videoStreamIndex]->codecpar) < 0) {std::cerr << "Error setting codec parameters" << std::endl;avcodec_free_context(&codecContext);avformat_close_input(&formatContext);return -1;}if (avcodec_open2(codecContext, codec, nullptr) < 0) {std::cerr << "Error opening codec" << std::endl;avcodec_free_context(&codecContext);avformat_close_input(&formatContext);return -1;}AVPacket packet;av_init_packet(&packet);// 计算目标时间戳int64_t targetTimestamp = targetSecond * AV_TIME_BASE;// 查找目标时间戳所对应的帧AVFrame* frame = av_frame_alloc();bool foundTargetFrame = false;int count = 0;while (av_read_frame(formatContext, &packet) >= 0) {if (packet.stream_index == videoStreamIndex) {int response = avcodec_send_packet(codecContext, &packet);if (response < 0) {std::cerr << "Error sending packet to decoder" << std::endl;break;}count++;response = avcodec_receive_frame(codecContext, frame);if (response == AVERROR(EAGAIN) || response == AVERROR_EOF) {continue;}else if (response < 0) {std::cerr << "Error receiving frame from decoder" << std::endl;break;}// 检查帧的时间戳是否接近目标时间戳/*if (frame->pts >= targetTimestamp - (AV_TIME_BASE / 2) && frame->pts <= targetTimestamp + (AV_TIME_BASE / 2)) {foundTargetFrame = true;break;}*/if (count == 20) {foundTargetFrame = true;char outname[] = "out.jpg";
//                savePicture(frame, outname);break;}}av_packet_unref(&packet);}if (!foundTargetFrame) {std::cerr << "Target frame not found" << std::endl;av_frame_free(&frame);avcodec_free_context(&codecContext);avformat_close_input(&formatContext);return -1;}// 将帧的数据保存为JPEG图片AVFrame* rgbFrame = av_frame_alloc();if (rgbFrame == nullptr) {std::cerr << "Error allocating RGB frame" << std::endl;av_frame_free(&frame);avcodec_free_context(&codecContext);avformat_close_input(&formatContext);return -1;}/*struct SwsContext* swsContext= sws_getContext(codecContext->width, codecContext->height, codecContext->pix_fmt,codecContext->width, codecContext->height, AV_PIX_FMT_RGB24,SWS_BILINEAR, nullptr, nullptr, nullptr);if (swsContext == nullptr) {std::cerr << "Error creating SwsContext" << std::endl;av_frame_free(&frame);av_frame_free(&rgbFrame);avcodec_free_context(&codecContext);avformat_close_input(&formatContext);return -1;}// 分配RGB帧的缓冲区int numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, codecContext->width, codecContext->height, 1);uint8_t* buffer = (uint8_t*)av_malloc(numBytes * sizeof(uint8_t));av_image_fill_arrays(rgbFrame->data, rgbFrame->linesize, buffer, AV_PIX_FMT_RGB24, codecContext->width, codecContext->height, 1);// 将解码后的帧转换为RGB格式sws_scale(swsContext, frame->data, frame->linesize, 0, codecContext->height, rgbFrame->data, rgbFrame->linesize);*/// 保存RGB帧为JPEG图片const AVCodec* jpegCodec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);if (jpegCodec == nullptr) {std::cerr << "Error finding JPEG encoder" << std::endl;av_frame_free(&frame);av_frame_free(&rgbFrame);avcodec_free_context(&codecContext);avformat_close_input(&formatContext);return -1;}AVCodecContext* jpegCodecContext = avcodec_alloc_context3(jpegCodec);if (jpegCodecContext == nullptr) {std::cerr << "Error allocating JPEG codec context" << std::endl;av_frame_free(&frame);av_frame_free(&rgbFrame);avcodec_free_context(&codecContext);avformat_close_input(&formatContext);return -1;}jpegCodecContext->pix_fmt = AV_PIX_FMT_YUVJ420P;jpegCodecContext->width = codecContext->width;jpegCodecContext->height = codecContext->height;// 设置编码器时间基jpegCodecContext->time_base = { 1, 25 };//formatContext->streams[videoStreamIndex]->time_base;if (avcodec_open2(jpegCodecContext, jpegCodec, nullptr) < 0) {std::cerr << "Error opening JPEG codec" << std::endl;av_frame_free(&frame);av_frame_free(&rgbFrame);avcodec_free_context(&codecContext);avcodec_free_context(&jpegCodecContext);avformat_close_input(&formatContext);return -1;}AVPacket jpegPacket;av_init_packet(&jpegPacket);jpegPacket.data = nullptr;jpegPacket.size = 0;if (avcodec_send_frame(jpegCodecContext, frame) < 0) {//rgbFramestd::cerr << "Error sending frame to JPEG encoder" << std::endl;av_frame_free(&frame);av_frame_free(&rgbFrame);avcodec_free_context(&codecContext);avcodec_free_context(&jpegCodecContext);avformat_close_input(&formatContext);return -1;}if (avcodec_receive_packet(jpegCodecContext, &jpegPacket) < 0) {std::cerr << "Error receiving packet from JPEG encoder" << std::endl;av_frame_free(&frame);av_frame_free(&rgbFrame);avcodec_free_context(&codecContext);avcodec_free_context(&jpegCodecContext);avformat_close_input(&formatContext);return -1;}// 保存JPEG图像到文件FILE* outputFile = fopen(outputFilename.c_str(), "wb");if (outputFile == nullptr) {std::cerr << "Error opening output file" << std::endl;av_frame_free(&frame);av_frame_free(&rgbFrame);avcodec_free_context(&codecContext);avcodec_free_context(&jpegCodecContext);avformat_close_input(&formatContext);return -1;}fwrite(jpegPacket.data, 1, jpegPacket.size, outputFile);fclose(outputFile);// 清理资源av_frame_free(&frame);av_frame_free(&rgbFrame);av_packet_unref(&packet);av_packet_unref(&jpegPacket);avcodec_free_context(&codecContext);return 1;
}

获取的图片看上去不是太清晰,字有些糊掉了

从AVFrame保存为jpg图片的处理可以有另外的一个方式,有些差异,

int savePicture(AVFrame* pFrame, char* out_name) {//编码保存图片int width = pFrame->width;int height = pFrame->height;AVCodecContext* pCodeCtx = NULL;AVFormatContext* pFormatCtx = avformat_alloc_context();// 设置输出文件格式pFormatCtx->oformat = av_guess_format("mjpeg", NULL, NULL);// 创建并初始化输出AVIOContextif (avio_open(&pFormatCtx->pb, out_name, AVIO_FLAG_READ_WRITE) < 0) {printf("Couldn't open output file.");return -1;}// 构建一个新streamAVStream* pAVStream = avformat_new_stream(pFormatCtx, 0);if (pAVStream == NULL) {return -1;}AVCodecParameters* parameters = pAVStream->codecpar;parameters->codec_id = pFormatCtx->oformat->video_codec;parameters->codec_type = AVMEDIA_TYPE_VIDEO;parameters->format = AV_PIX_FMT_YUVJ420P;parameters->width = pFrame->width;parameters->height = pFrame->height;const AVCodec* pCodec = avcodec_find_encoder(pAVStream->codecpar->codec_id);  //查找编码器if (!pCodec) {printf("Could not find encoder\n");return -1;}pCodeCtx = avcodec_alloc_context3(pCodec);   //为AVCodecContext分配内存if (!pCodeCtx) {fprintf(stderr, "Could not allocate video codec context\n");exit(1);}if ((avcodec_parameters_to_context(pCodeCtx, pAVStream->codecpar)) < 0) {fprintf(stderr, "Failed to copy %s codec parameters to decoder context\n",av_get_media_type_string(AVMEDIA_TYPE_VIDEO));return -1;}//  AVRational tmp = { 1, 25 };pCodeCtx->time_base = { 1, 25 };if (avcodec_open2(pCodeCtx, pCodec, NULL) < 0) {   //打开编码器printf("Could not open codec.");return -1;}int ret = avformat_write_header(pFormatCtx, NULL);if (ret < 0) {printf("write_header fail\n");return -1;}int y_size = width * height;//Encode// 给AVPacket分配足够大的空间AVPacket pkt;av_new_packet(&pkt, y_size * 3);// 编码数据ret = avcodec_send_frame(pCodeCtx, pFrame);if (ret < 0) {printf("Could not avcodec_send_frame.");return -1;}// 得到编码后数据ret = avcodec_receive_packet(pCodeCtx, &pkt);if (ret < 0) {printf("Could not avcodec_receive_packet");return -1;}ret = av_write_frame(pFormatCtx, &pkt);if (ret < 0) {printf("Could not av_write_frame");return -1;}av_packet_unref(&pkt);//Write Trailerav_write_trailer(pFormatCtx);avcodec_close(pCodeCtx);avio_close(pFormatCtx->pb);avformat_free_context(pFormatCtx);return 0;
}

参考资料

FFmpeg将视频转换成一帧一帧的jpeg图片(代码实现)_ffmpeg把视频转为一帧帧图片-CSDN博客

相关文章:

[FFmpeg学习]从视频中获取图片

从视频中获取图片是一个比较直观的例子&#xff0c;这里从一个基础的例子来查看FFmpeg相关api的使用&#xff0c;从mp4文件中获取一帧图像&#xff0c;保存为jpeg格式图片&#xff0c;mp4文件比较好准备&#xff0c;一般手机录屏文件就是mp4格式。 原理还是比较清楚&#xff0…...

Redis集中管理Session和系统初始化参数详解

Redis 是一个开源的、基于内存的键值存储系统&#xff0c;通常用作数据库、缓存或消息传递系统。在 Web 应用程序中&#xff0c;Redis 常用于集中管理 Session 数据和系统初始化参数。 Redis 管理 Session Session 是 Web 应用程序中用于保持用户状态的一种机制…...

[网鼎杯 2020 朱雀组]phpweb

抓包发现两个参数&#xff0c;结合报文返回的warning猜测两个参数一个传函数名&#xff0c;另一个传函数参数 尝试直接system(ls /)&#xff0c;发现被过滤了 file_get_contents获取index.php的源码&#xff0c;发现可以反序列化实现RCE 这里复现的时候不知道为什么显示不全…...

情人节html代码

一、一个带有心形和祝福消息的页面 如果想在网页上创建一个简单的情人节祝福&#xff0c;可以使用HTML和CSS。以下是一个简单的例子&#xff0c;它创建了一个带有心形和祝福消息的页面&#xff1a; <!DOCTYPE html> <html> <head> <title>情人节…...

键盘重映射禁用 CtrlAltDel 键的利弊

目录 前言 一、Scancode Map 的规范 二、禁用 CtrlAltDel 的方法及其缺陷 三、编程实现和测试 3.1 C 实现的简易修改工具 3.2 C# 实现的窗口工具 四、总结 本文属于原创文章&#xff0c;转载请注明出处&#xff1a; https://blog.csdn.net/qq_59075481/article/details…...

【网工】华为设备命令学习(综合实验一)

实验要求和实验成果如图所示。 LSW2不需要其他配置&#xff0c;其下就一台设备&#xff0c;不需要区分。 LSW3配置如下&#xff1a; <Huawei>sy Enter system view, return user view with CtrlZ. [Huawei]un in en //关闭系统提示信息 Info: Information …...

JavaScript中的常见算法

一.排序算法 1.冒泡排序 冒泡排序比较所有相邻的两个项&#xff0c;如果第一个比第二个大&#xff0c;则交换它们。元素项向上移动至 正确的顺序&#xff0c;就好像气泡升至表面一样。 function bubbleSort(arr) {const { length } arrfor (let i 0; i < length - 1; i)…...

桥接模式:连接抽象与实现的设计艺术

桥接模式&#xff1a;连接抽象与实现的设计艺术 在软件开发中&#xff0c;设计模式是帮助我们以优雅的方式解决问题的模板。桥接模式&#xff08;Bridge Pattern&#xff09;是一种结构型设计模式&#xff0c;它的主要目标是将抽象部分与实现部分分离&#xff0c;这样两者可以…...

C语言——oj刷题——字符串左旋

问题&#xff1a; 实现一个函数&#xff0c;可以左旋字符串中的k个字符。 例如&#xff1a; ABCD左旋一个字符得到BCDA ABCD左旋两个字符得到CDAB 实现&#xff1a; 当我们谈到字符串左旋时&#xff0c;我们指的是将字符串中的字符向左移动一定数量的位置。这个问题在编程中…...

神经网络(Nature Network)

最近接触目标检测较多&#xff0c;再此对最基本的神经网络知识进行补充&#xff0c;本博客适合想入门人工智能、其含有线性代数及高等数学基础的人群观看 1.构成 由输入层、隐藏层、输出层、激活函数、损失函数组成。 输入层&#xff1a;接收原始数据隐藏层&#xff1a;进行…...

【Unity】QFramework通用背包系统优化:使用Odin优化编辑器

前言 在学习凉鞋老师的课程《QFramework系统设计&#xff1a;通用背包系统》第四章时&#xff0c;笔者使用了Odin插件&#xff0c;对Item和ItemDatabase的SO文件进行了一些优化&#xff0c;使物品页面更加紧凑、更易拓展。 核心逻辑和功能没有改动&#xff0c;整体代码量减少…...

基本算法--贪心

1.简述 贪心法的效率非常高&#xff0c;复杂度常常为O&#xff08;1&#xff09;&#xff0c;是一种局部最优的解题方法&#xff0c;而很多问题都需要求全局最优&#xff0c;&#xff0c;所以在使用贪心法之前需要评估是否能从局部最优推广到全局最优。 2.思路 作为算法的贪…...

13. 串口接收模块的项目应用案例

1. 使用串口来控制LED灯工作状态 使用串口发送指令到FPGA开发板&#xff0c;来控制第7课中第4个实验的开发板上的LED灯的工作状态。 LED灯的工作状态&#xff1a;让LED灯按指定的亮灭模式亮灭&#xff0c;亮灭模式未知&#xff0c;由用户指定&#xff0c;8个变化状态为一个循…...

Python re找到特定pattern并将此pattern重复n次

要找到字符串s中的数字&#xff0c;并将这些数字重复3次&#xff1a; import re s "abc123def456ghi789" # 找到所有的数字 numbers re.findall(r\d, s) # 重复每个数字3次 repeated_numbers [num * 3 for num in numbers] # 将重复的数字放回原位置 #…...

ChatGpt报错:We ran into an issue while authenticating you解决办法

在登录ChatGpt时报错&#xff1a;Oops&#xff01;,We ran into an issue while authenticating you.(我们在验证您时遇到问题)&#xff0c;记录一下解决过程。 完整报错&#xff1a; We ran into an issue while authenticating you. If this issue persists, please contact…...

如何从 iPhone 恢复已删除的视频:简单有效方法

无论您是在尝试释放空间时不小心删除了 iPhone 上的视频&#xff0c;还是在出厂时清空了手机&#xff0c;现在所有数据都消失了&#xff0c;都不要放弃。有一些方法可以恢复这些视频。 在本文中&#xff0c;我们将向您展示六种最有效的数据恢复方法&#xff0c;可以帮助您从 i…...

【python量化交易】qteasy使用教程02 - 获取和管理金融数据

qteasy教程2 - 获取并管理金融数据 qteasy教程2 - 获取并管理金融数据开始前的准备工作获取基础数据以及价格数据下载交易日历和基础数据查看股票和指数的基础数据下载沪市股票数据从本地获取股价数据生成K线图 数据类型的查找定期下载数据到本地回顾总结 qteasy教程2 - 获取并…...

数据库学习案例20240206-ORACLE NEW RAC agent and resource关系汇总。

1 集群架构图 整体集群架构图如下&#xff1a; 1 数据库启动顺序OHASD层面 操作系统进程init.ohasd run启动ohasd.bin init.ohasd run 集群自动启动是否被禁用 crsctl enable has/crsGIHOME所在文件系统是否被正常挂载。管道文件npohasd是否能够被访问&#xff0c; cd /var/t…...

TypeScript 入门

课程地址 ts 开发环境搭建 npm i -g typescript查看安装位置&#xff1a; $ npm root -g C:\Users\Daniel\AppData\Roaming\npm\node_modules创建 hello.ts&#xff1a; console.log("hello, ts");编译 ts 文件&#xff0c;得到 js 文件&#xff1a; $ tsc foo.…...

linux 磁盘相关操作

1.U盘接入虚拟机 &#xff08;1&#xff09;在插入u盘时&#xff0c;虚拟机会检测usb设备&#xff0c;在弹出窗口选择连接到虚拟机即可。 &#xff08;2&#xff09;或 直接在虚拟机--->可移动设备--->找到U盘---->连接 2.检测U盘是否被虚拟机识别 ls /dev/sd* 查…...

uniapp 对接腾讯云IM群组成员管理(增删改查)

UniApp 实战&#xff1a;腾讯云IM群组成员管理&#xff08;增删改查&#xff09; 一、前言 在社交类App开发中&#xff0c;群组成员管理是核心功能之一。本文将基于UniApp框架&#xff0c;结合腾讯云IM SDK&#xff0c;详细讲解如何实现群组成员的增删改查全流程。 权限校验…...

HTML 语义化

目录 HTML 语义化HTML5 新特性HTML 语义化的好处语义化标签的使用场景最佳实践 HTML 语义化 HTML5 新特性 标准答案&#xff1a; 语义化标签&#xff1a; <header>&#xff1a;页头<nav>&#xff1a;导航<main>&#xff1a;主要内容<article>&#x…...

智慧工地云平台源码,基于微服务架构+Java+Spring Cloud +UniApp +MySql

智慧工地管理云平台系统&#xff0c;智慧工地全套源码&#xff0c;java版智慧工地源码&#xff0c;支持PC端、大屏端、移动端。 智慧工地聚焦建筑行业的市场需求&#xff0c;提供“平台网络终端”的整体解决方案&#xff0c;提供劳务管理、视频管理、智能监测、绿色施工、安全管…...

VTK如何让部分单位不可见

最近遇到一个需求&#xff0c;需要让一个vtkDataSet中的部分单元不可见&#xff0c;查阅了一些资料大概有以下几种方式 1.通过颜色映射表来进行&#xff0c;是最正规的做法 vtkNew<vtkLookupTable> lut; //值为0不显示&#xff0c;主要是最后一个参数&#xff0c;透明度…...

大模型多显卡多服务器并行计算方法与实践指南

一、分布式训练概述 大规模语言模型的训练通常需要分布式计算技术,以解决单机资源不足的问题。分布式训练主要分为两种模式: 数据并行:将数据分片到不同设备,每个设备拥有完整的模型副本 模型并行:将模型分割到不同设备,每个设备处理部分模型计算 现代大模型训练通常结合…...

【JavaWeb】Docker项目部署

引言 之前学习了Linux操作系统的常见命令&#xff0c;在Linux上安装软件&#xff0c;以及如何在Linux上部署一个单体项目&#xff0c;大多数同学都会有相同的感受&#xff0c;那就是麻烦。 核心体现在三点&#xff1a; 命令太多了&#xff0c;记不住 软件安装包名字复杂&…...

C++ 设计模式 《小明的奶茶加料风波》

&#x1f468;‍&#x1f393; 模式名称&#xff1a;装饰器模式&#xff08;Decorator Pattern&#xff09; &#x1f466; 小明最近上线了校园奶茶配送功能&#xff0c;业务火爆&#xff0c;大家都在加料&#xff1a; 有的同学要加波霸 &#x1f7e4;&#xff0c;有的要加椰果…...

Linux系统部署KES

1、安装准备 1.版本说明V008R006C009B0014 V008&#xff1a;是version产品的大版本。 R006&#xff1a;是release产品特性版本。 C009&#xff1a;是通用版 B0014&#xff1a;是build开发过程中的构建版本2.硬件要求 #安全版和企业版 内存&#xff1a;1GB 以上 硬盘&#xf…...

k8s从入门到放弃之HPA控制器

k8s从入门到放弃之HPA控制器 Kubernetes中的Horizontal Pod Autoscaler (HPA)控制器是一种用于自动扩展部署、副本集或复制控制器中Pod数量的机制。它可以根据观察到的CPU利用率&#xff08;或其他自定义指标&#xff09;来调整这些对象的规模&#xff0c;从而帮助应用程序在负…...

【Post-process】【VBA】ETABS VBA FrameObj.GetNameList and write to EXCEL

ETABS API实战:导出框架元素数据到Excel 在结构工程师的日常工作中,经常需要从ETABS模型中提取框架元素信息进行后续分析。手动复制粘贴不仅耗时,还容易出错。今天我们来用简单的VBA代码实现自动化导出。 🎯 我们要实现什么? 一键点击,就能将ETABS中所有框架元素的基…...