FFmpegFrameGrabber视频抽帧工具类

2023-11-08 10:40

本文主要是介绍FFmpegFrameGrabber视频抽帧工具类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Bytedeco

通过视频链接进行关键帧抽取图片,利用FFmpegFrameGrabber对视频流进行抽帧处理。

一、引入POM依赖

        <dependency><groupId>org.bytedeco</groupId><artifactId>javacv</artifactId><version>1.4.1</version></dependency><dependency><groupId>org.bytedeco.javacpp-presets</groupId><artifactId>ffmpeg-platform</artifactId><version>3.4.2-1.4.1</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>javacpp</artifactId><version>1.5.8</version></dependency>

二、抽帧代码实现

import lombok.extern.slf4j.Slf4j;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;/*** 视频抽帧工具*/
@Slf4j
public class VideoFrame {public static Map<Integer, InputStream> process(String videoUrl, Integer stepSecond, Integer count, String uuid, String ipProxy) {Map<Integer, InputStream> result = null;int num = 0;while (num < 2) {log.info("开始抽帧");result = videoUrlIntercept(videoUrl, stepSecond, count, uuid, ipProxy);if (result.size() > 0) {log.info("uuid:{},第{}次抽帧成功", uuid, num + 1);break;} else {//第一次使用ipProxy不成功就更换log.info("uuid:{},第{}次抽帧保存失败,ipProxy:{}", uuid, num + 1, ipProxy);ipProxy = null;num++;}}return result;}/*** 视频文件边下载边抽帧1秒1帧** @param videoUrl   网络视频文件URL* @param stepSecond 每隔几秒取一帧,默认1s* @param count      需要截取的帧个数* @param uuid       uuid* @return*/public static Map<Integer, InputStream> videoUrlIntercept(String videoUrl, Integer stepSecond, Integer count, String uuid, String ipProxy) {Map<Integer, InputStream> files = new HashMap<>();stepSecond = stepSecond == null ? 1 : stepSecond;FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videoUrl);// 设置超时时间为40秒ff.setOption("timeout", "40000000");ff.setOption("user_agent", UserAgent.getUserAgent());try {ff.start();long timeLength = ff.getLengthInTime();Frame frame = ff.grabImage();long startTime = frame.timestamp;long timestamp = 0;int second = 0;int picNum = 0;while (timestamp <= timeLength) {log.info("uuid:{},抽取第{}帧,video_url:{}",uuid,picNum,videoUrl);timestamp = startTime + second * 1000000L;ff.setTimestamp(timestamp);frame = ff.grabImage();if (frame != null) {if (frame.image != null) {InputStream inputStream = doExecuteFrame(frame, picNum);if (inputStream != null) {files.put(picNum, inputStream);}picNum++;if (count != null && picNum == count) {break;}}}second += stepSecond;if(picNum > 60) {break;}}ff.stop();} catch (Exception e) {log.error("下载抽帧失败,uuid:{},ipPort:{},videoUrl:{},msg:{}", uuid, null, videoUrl, e.getMessage());e.printStackTrace();}return files;}/*** 视频文件指定时间段的帧截取** @param videoUrl  视频文件URL* @param start     视频开始的帧* @param count     需要截取的帧个数* @param isAvgTime 在截帧时 是否均匀分布计算时间* @return*/public static Map<Integer, InputStream> videoIntercept(String videoUrl, int start, int count, boolean isAvgTime) {log.info("开始抽取视频帧数,videoUrl:{}",videoUrl);Frame frame = null;//<时间, 图片流>Map<Integer, InputStream> files = new HashMap<>();FFmpegFrameGrabber fFmpegFrameGrabber = new FFmpegFrameGrabber(videoUrl);fFmpegFrameGrabber.setOption("timeout", "40000000");try {fFmpegFrameGrabber.start();long frameTime = 1;if (isAvgTime) {frameTime = fFmpegFrameGrabber.getLengthInTime() / count / 1000000L;if (frameTime < 0) {frameTime = 1;}}for (int i = start; i <= count; i++) {fFmpegFrameGrabber.setTimestamp(i * frameTime * 1000 * 1000);frame = fFmpegFrameGrabber.grabImage();InputStream inputStream = doExecuteFrame(frame, i);if (inputStream != null) {files.put(i, inputStream);}}fFmpegFrameGrabber.stop();} catch (Exception E) {log.info("下载的视频抽帧失败,msg:" + E.getMessage());E.printStackTrace();}return files;}public static InputStream doExecuteFrame(Frame frame, int index) {if (frame == null || frame.image == null) {return null;}Java2DFrameConverter converter = new Java2DFrameConverter();BufferedImage bi = converter.getBufferedImage(frame);InputStream inputStream = bufferedImageToInputStream(bi);return inputStream;}public static InputStream bufferedImageToInputStream(BufferedImage image) {ByteArrayOutputStream os = new ByteArrayOutputStream();try {ImageIO.write(image, "jpg", os);InputStream input = new ByteArrayInputStream(os.toByteArray());return input;} catch (IOException e) {}return null;}public static void main(String[] args) throws IOException {String videoUrl = "http://vd2.bdstatic.com/mda-pej1ztfufz8axvtu/360p/h264/1684545921389774683/mda-pej1ztfufz8axvtu.mp4";Map<Integer, InputStream> integerInputStreamMap = videoIntercept(videoUrl, 1, 13, false);System.out.println(integerInputStreamMap.size());for (Integer seconds : integerInputStreamMap.keySet()) {InputStream inputStream = integerInputStreamMap.get(seconds);String fileName = MD5Util.createMd5(System.currentTimeMillis()+"抖音测试3" + "_" + seconds) + ".jpg";String filePath = "/test/01/"+fileName;//本地磁盘存储String uploadURL = PictureDownload.downloadFrame(fileName, "D:/image" + filePath, inputStream, "douyin", true);System.out.println("seconds: " + seconds + ", uploadURL: " + uploadURL);}}
}

这篇关于FFmpegFrameGrabber视频抽帧工具类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/369289

相关文章

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

MySQL慢查询工具的使用小结

《MySQL慢查询工具的使用小结》使用MySQL的慢查询工具可以帮助开发者识别和优化性能不佳的SQL查询,本文就来介绍一下MySQL的慢查询工具,具有一定的参考价值,感兴趣的可以了解一下... 目录一、启用慢查询日志1.1 编辑mysql配置文件1.2 重启MySQL服务二、配置动态参数(可选)三、分析慢查

基于Python实现进阶版PDF合并/拆分工具

《基于Python实现进阶版PDF合并/拆分工具》在数字化时代,PDF文件已成为日常工作和学习中不可或缺的一部分,本文将详细介绍一款简单易用的PDF工具,帮助用户轻松完成PDF文件的合并与拆分操作... 目录工具概述环境准备界面说明合并PDF文件拆分PDF文件高级技巧常见问题完整源代码总结在数字化时代,PD

Python按照24个实用大方向精选的上千种工具库汇总整理

《Python按照24个实用大方向精选的上千种工具库汇总整理》本文整理了Python生态中近千个库,涵盖数据处理、图像处理、网络开发、Web框架、人工智能、科学计算、GUI工具、测试框架、环境管理等多... 目录1、数据处理文本处理特殊文本处理html/XML 解析文件处理配置文件处理文档相关日志管理日期和

使用Python开发一个Ditto剪贴板数据导出工具

《使用Python开发一个Ditto剪贴板数据导出工具》在日常工作中,我们经常需要处理大量的剪贴板数据,下面将介绍如何使用Python的wxPython库开发一个图形化工具,实现从Ditto数据库中读... 目录前言运行结果项目需求分析技术选型核心功能实现1. Ditto数据库结构分析2. 数据库自动定位3

基于Python实现简易视频剪辑工具

《基于Python实现简易视频剪辑工具》这篇文章主要为大家详细介绍了如何用Python打造一个功能完备的简易视频剪辑工具,包括视频文件导入与格式转换,基础剪辑操作,音频处理等功能,感兴趣的小伙伴可以了... 目录一、技术选型与环境搭建二、核心功能模块实现1. 视频基础操作2. 音频处理3. 特效与转场三、高

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核