Java 视频处理,截帧操作
1.maven
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv-platform</artifactId>
<version>3.4.1-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>
2.工具类
import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacpp.opencv_videoio;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.*;
import java.util.List; import static org.bytedeco.javacpp.opencv_imgcodecs.cvSaveImage;
import static org.bytedeco.javacpp.opencv_videoio.*; /**
* 视频工具
*
* @author
*/
public class VideoUtil { private static final int SECOND = 50; private static final Logger logger = LoggerFactory.getLogger(VideoUtil.class); /**
* 获取指定视频的帧并保存为图片至指定目录
*
* @param videoFile 源视频文件
* @param saveFile 截取帧的图片存放路径
* @throws Exception
*/
public static List<File> fetchPic(File videoFile, String saveFile, int second) throws Exception { java.util.List<File> files = new ArrayList<>(); FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videoFile);
ff.start();
int lenght = ff.getLengthInAudioFrames();
System.out.println(ff.getFrameRate()); int i = 0;
Frame frame = null; while (i < lenght) {
// 过滤前5帧,避免出现全黑的图片,依自己情况而定
frame = ff.grabImage();
if (i >= (int) (ff.getFrameRate() * second) && frame.image != null) {
System.out.print(i + ",");
if (frame != null && frame.image != null) {
System.out.println(i);
files.add(writeToFile(frame, saveFile, i));
}
second++;
}
i += second;
}
ff.stop();
return files;
} public static List<Integer> getList(int count, int length) {
if (count > length) {
count = length;
}
System.out.println(length);
System.out.println(count);
int total = (int) (length / count);
List<Integer> list = new ArrayList<>();
for (int i = 0; i < count; i++) {
list.add(i * total);
System.out.println(i * total);
}
return list;
} public static List<File> fetchPicByCount(File videoFile, String saveFile, int count) throws Exception { java.util.List<File> files = new ArrayList<>(); FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videoFile);
ff.start(); int frameLength = ff.getLengthInFrames(); System.out.println("length:" + frameLength); List<Integer> list = getList(count, frameLength); System.out.println(ff.getFrameRate()); int i = 0;
Frame frame = null; while (i < frameLength) {
frame = ff.grabImage();
if (list.contains(i)) {
if (frame != null && frame.image != null) {
System.out.println(i);
files.add(writeToFile(frame, saveFile, i));
}
}
i++;
}
ff.stop();
return files;
} public static File writeToFile(Frame frame, String saveFile, int second) throws InvokeException {
String fileName = String.valueOf(System.currentTimeMillis()) + second;
File targetFile = new File(saveFile + File.separator + fileName + ".jpg");
String imgSuffix = "jpg"; Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage srcBi = converter.getBufferedImage(frame);
int owidth = srcBi.getWidth();
int oheight = srcBi.getHeight();
// 对截取的帧进行等比例缩放
int width = 800;
int height = (int) (((double) width / owidth) * oheight);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
bi.getGraphics().drawImage(srcBi.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
try {
ImageIO.write(bi, imgSuffix, targetFile);
} catch (Exception e) {
throw new InvokeException(ResultEnum.FAILED.getCode(), "截帧失败");
}
return targetFile;
} /**
* 获取视频时长,单位为秒
*
* @param file
* @return 时长(s)
*/
public static Long getVideoTime(File file) {
Long times = 0L;
try {
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(file);
ff.start();
times = ff.getLengthInTime() / (1000 * 1000);
ff.stop();
} catch (Exception e) {
e.printStackTrace();
}
return times;
} public static void getBySecond(String filePath, String directory) {
opencv_videoio.CvCapture capture = cvCaptureFromFile(filePath);
//帧率
double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
System.out.println("帧率:" + fps);
opencv_core.IplImage frame = null;
double pos1 = 0; double rootCount = 0;
while (true) { //读取关键帧
frame = cvQueryFrame(capture); rootCount = fps;
while (rootCount > 0) {
//这一段的目的是跳过每一秒钟的帧数,也就是说fps是帧率(一秒钟有多少帧),在读取一帧后,跳过fps数量的帧就相当于跳过了1秒钟。
frame = cvQueryFrame(capture);
rootCount--;
} //获取当前帧的位置
pos1 = cvGetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES);
System.out.println(pos1); if (null == frame)
break; cvSaveImage("E:/223/" + pos1 + ".jpg", frame); } cvReleaseCapture(capture);
} /*public void getBySecond() {
opencv_videoio.CvCapture capture = opencv_highgui.cvC("D:/085402.crf"); //帧率
int fps = (int) opencv_highgui.cvGetCaptureProperty(capture, opencv_highgui.CV_CAP_PROP_FPS);
System.out.println("帧率:"+fps); opencv_core.IplImage frame = null;
double pos1 = 0; int rootCount = 0; while (true) { //读取关键帧
frame = opencv_highgui.cvQueryFrame(capture); rootCount = fps;
while(rootCount > 0 ){
//这一段的目的是跳过每一秒钟的帧数,也就是说fps是帧率(一秒钟有多少帧),在读取一帧后,跳过fps数量的帧就相当于跳过了1秒钟。
frame = opencv_highgui.cvQueryFrame(capture);
rootCount--;
} //获取当前帧的位置
pos1 = opencv_highgui.cvGetCaptureProperty(capture,opencv_highgui.CV_CAP_PROP_POS_FRAMES);
System.out.println(pos1); if (null == frame)
break; opencv_highgui.cvSaveImage("d:/img/" + pos1 + ".jpg",frame); } opencv_highgui.cvReleaseCapture(capture);
}*/ public static void main(String[] args) {
try {
//getList(10,113); File file = new File("E:/2.mp4");
List<File> files = VideoUtil.fetchPicByCount(file, "E:/223", 100);
System.out.println(files.get(0).getName());
System.out.println(VideoUtil.getVideoTime(file));
} catch (Exception e) {
e.printStackTrace();
}
}
}
参考:https://blog.csdn.net/qq_22175485/article/details/81025525
Java 视频处理,截帧操作的更多相关文章
- java获取视频播第一帧
FFMPEG 功能很强大,做视频必备的软件.大家可通过 http://ffmpeg.org/ 了解.Windows版本的软件,可通过 http://ffmpeg.zeranoe.com/builds/ ...
- OpenCV 编程简单介绍(矩阵/图像/视频的基本读写操作)
PS. 因为csdn博客文章长度有限制,本文有部分内容被截掉了.在OpenCV中文站点的wiki上有可读性更好.而且是完整的版本号,欢迎浏览. OpenCV Wiki :<OpenCV 编程简单 ...
- Opencv基础知识-----视频的读取和操作
Opencv读取视频代码 #include "stdafx.h" #include"highgui.h" int main(int argc,char* a ...
- selenium webdriver 截屏操作
有时候我们需要进行截屏操作,特别是遇到一些比较重要的页面信息(出现错误)或者出现不同需要进行对比时, 我们就需要对正在处理的页面进行截屏! 未经作者允许,禁止转载! package test_wait ...
- AsciiPic Java视频转成字符画
AsciiPic Java视频转成字符画 github下载 https://github.com/dejavudwh/AsciiPic 运行截图 //没有做GUI 比较简陋 节省时间 main里的文件 ...
- tcpdump截帧工具使用
一.tcpdump介绍 tcpdump是Linux下功能强大的截帧工具,相当于windows下的wireshark一下,只是操作方式是命令行的,需要熟悉Linux命令行操作. 常用的Linux ...
- java的基础知识文件操作和标识符
1.文件夹的操作 dir :显示当前文件夹中的所有文件和文件夹. cd 路径: 进入到指定的路径. cd .. : 回到上一级目录 cd \ : 回到当前目录的跟目录 md 文件夹名 创建一个 ...
- Elasticsearch【JAVA REST Client】客户端操作
ES系统作为集群,环境搭建非常方便简单. 现在在我们的应用中,如何对这个集群进行操作呢? 我们利用ES系统,通常都是下面的架构: 在这里,客户端的请求通过LB进行负载均衡,因为操作任何一个ES的实例, ...
- Java Calendar 类的时间操作
Java Calendar 类的时间操作 标签: javaCalendar时间Date 2013-07-30 17:53 140401人阅读 评论(7) 收藏 举报 分类: 所有(165) Java ...
随机推荐
- 便于记忆的SA构造
首先学习基数排序. memset(b, 0, sizeof(b)); for(int i = 0; i < n; i++) b[a[i]]++; for(int i = 1; i <= m ...
- 关于function和task的说明
1. 关于函数function调用,总结两个要点: 1. 函数调用一般产生一个值,这个值被赋值给某个变量 2. 函数所返回的值只能是一个,不可以是多个,不能像C语言中采用指针的方式返回多个值.因 ...
- 出题人的女装(牛客练习赛38题B) (概率+分式运算)
链接:https://ac.nowcoder.com/acm/contest/358/B来源:牛客网 出题人的女装 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...
- <iOS开发>之App上架流程(2017)
本文主要介绍了App上架流程,以及上架过程中会遇到的一些问题. 一.App上架前的准备. 上架前,需要开发人员有苹果开发者账号,具体请阅读苹果开发者账号注册申请流程.本文是在已经拥有开发者账号的前提下 ...
- 【学习总结】Git学习-参考廖雪峰老师教程九-使用码云
学习总结之Git学习-总 目录: 一.Git简介 二.安装Git 三.创建版本库 四.时光机穿梭 五.远程仓库 六.分支管理 七.标签管理 八.使用GitHub 九.使用码云 十.自定义Git 期末总 ...
- 【问题解决方案】之 hadoop 用jps命令后缺少namenode的问题
用Xshell连接腾讯cloud里的虚拟机后,jps命令查无namenode导致过滤排序程序跑不起来,如图: 解决方案: Google之,说需要重启,格式化后再启动Hadoop.但鉴于本人不知道实现的 ...
- pip Read timed out 和 pip 源
解决方法,设置超时时间 pip --default-timeout=100 install -U Pillow 安装时指定源(--index-url) #例如安装scipy时使用豆瓣的源 pip in ...
- babel (二) update to v7
一.rootmode In 7.1, we've introduced a rootMode option for further lookup if necessary. 二.Remove prop ...
- scala下划线
作为函数的参数 一个匿名的函数传递给一个方法或者函数的时候,scala会尽量推断出参数类型.例如一个完整的匿名函数作为参数可以写为 scala> def compute(f: (Double)= ...
- win10远程桌面连接提示身份验证错误,要求的函数不受支持的解决方案
转自https://www.baidu.com/link?url=67JXh4h79mN47mEenuH_ElGkSh9_GdOiY-Xp9Ihw0_mQIZHrPx-HxY3EIm_nTZKPoRZ ...