|
@@ -1,7 +1,6 @@
|
|
|
package com.iden.common.util;
|
|
|
|
|
|
-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;
|
|
@@ -15,9 +14,6 @@ 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.*;
|
|
|
-
|
|
|
|
|
|
/**
|
|
|
* 视频工具
|
|
@@ -56,7 +52,8 @@ public class VideoUtil {
|
|
|
System.out.print(i + ",");
|
|
|
if (frame != null && frame.image != null) {
|
|
|
System.out.println(i);
|
|
|
- files.add(writeToFile(frame, saveFile, i));
|
|
|
+ String fileName = String.valueOf(System.currentTimeMillis()) + i;
|
|
|
+ files.add(writeToFile(frame, saveFile, fileName));
|
|
|
}
|
|
|
second++;
|
|
|
}
|
|
@@ -70,8 +67,8 @@ public class VideoUtil {
|
|
|
if (count > length) {
|
|
|
count = length;
|
|
|
}
|
|
|
- System.out.println(length);
|
|
|
- System.out.println(count);
|
|
|
+// 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++) {
|
|
@@ -81,17 +78,20 @@ public class VideoUtil {
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
- public static List<File> fetchPicByCount(File videoFile, String saveFile, int count) throws Exception {
|
|
|
+ public static List<File> fetchPicByCount(File videoFile, String saveDir, int count) throws Exception {
|
|
|
|
|
|
java.util.List<File> files = new ArrayList<>();
|
|
|
|
|
|
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videoFile);
|
|
|
ff.start();
|
|
|
|
|
|
+ // 此视频时长(s/秒)
|
|
|
+ Long duration = ff.getLengthInTime() / (1000 * 1000);
|
|
|
+ System.out.println("duration==" + duration);
|
|
|
+ // 视频总帧数
|
|
|
int frameLength = ff.getLengthInFrames();
|
|
|
|
|
|
-
|
|
|
- System.out.println("length:" + frameLength);
|
|
|
+ System.out.println("length==" + frameLength);
|
|
|
|
|
|
List<Integer> list = getList(count, frameLength);
|
|
|
|
|
@@ -105,7 +105,8 @@ public class VideoUtil {
|
|
|
if (list.contains(i)) {
|
|
|
if (frame != null && frame.image != null) {
|
|
|
System.out.println(i);
|
|
|
- files.add(writeToFile(frame, saveFile, i));
|
|
|
+ String fileName = String.valueOf(System.currentTimeMillis()) + i;
|
|
|
+ files.add(writeToFile(frame, saveDir, fileName));
|
|
|
}
|
|
|
}
|
|
|
i++;
|
|
@@ -114,10 +115,48 @@ public class VideoUtil {
|
|
|
return files;
|
|
|
}
|
|
|
|
|
|
+ public static List<File> fetchAllPic(File videoFile, String saveDir, Date shootEndTime) throws Exception {
|
|
|
+
|
|
|
+ Long endTime = shootEndTime.getTime() ;
|
|
|
+
|
|
|
+ java.util.List<File> files = new ArrayList<>();
|
|
|
+
|
|
|
+ FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videoFile);
|
|
|
+ ff.start();
|
|
|
+
|
|
|
+ // 此视频时长(ms/毫秒秒)
|
|
|
+ Long duration = ff.getLengthInTime() / 1000;
|
|
|
+ System.out.println("duration==" + duration);
|
|
|
|
|
|
- private static File writeToFile(Frame frame, String saveFile, int second) throws Exception {
|
|
|
- String fileName = String.valueOf(System.currentTimeMillis()) + second;
|
|
|
- File targetFile = new File(saveFile + File.separator + fileName + ".jpg");
|
|
|
+ Long beginTime = endTime - duration;
|
|
|
+
|
|
|
+ // 视频总帧数
|
|
|
+ int frameLength = ff.getLengthInFrames();
|
|
|
+
|
|
|
+ System.out.println("length==" + frameLength);
|
|
|
+
|
|
|
+ //帧率(1秒多少帧)
|
|
|
+ double framRate = ff.getFrameRate();
|
|
|
+
|
|
|
+ int i = 0;
|
|
|
+ Frame frame = null;
|
|
|
+
|
|
|
+ while (i < frameLength) {
|
|
|
+ frame = ff.grabImage();
|
|
|
+ if (frame != null && frame.image != null) {
|
|
|
+ Double time = beginTime + (i * (1000/framRate));
|
|
|
+ String fileName = DateUtils.formatToDateStr(new Date(time.longValue()),"yyyyMMddHHmmss") + "_" + UUID.randomUUID().toString();
|
|
|
+ files.add(writeToFile(frame, saveDir, fileName));
|
|
|
+ }
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ ff.stop();
|
|
|
+ return files;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static File writeToFile(Frame frame, String saveDir, String fileName) throws Exception {
|
|
|
+
|
|
|
+ File targetFile = new File(saveDir + File.separator + fileName + ".jpg");
|
|
|
String imgSuffix = "jpg";
|
|
|
|
|
|
Java2DFrameConverter converter = new Java2DFrameConverter();
|
|
@@ -157,51 +196,17 @@ public class VideoUtil {
|
|
|
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 static void main(String[] args) {
|
|
|
try {
|
|
|
//getList(10,113);
|
|
|
|
|
|
- File file = new File("E:/4bcd37e90a0d087c349bbd817b5b4798.mp4");
|
|
|
- List<File> files = VideoUtil.fetchPicByCount(file, "E:/223", 100);
|
|
|
- System.out.println(files.get(0).getName());
|
|
|
- System.out.println(VideoUtil.getVideoTime(file));
|
|
|
+ File file = new File("E:/4bcd37e90a0d087c349bbd817b5b4798.mp4");
|
|
|
+// List<File> files = VideoUtil.fetchPicByCount(file, "E:/223", 100);
|
|
|
+// System.out.println(files.get(0).getName());
|
|
|
+// System.out.println(VideoUtil.getVideoTime(file));
|
|
|
+ fetchAllPic(file,"E:/223",new Date());
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|