|
@@ -9,22 +9,22 @@
|
|
|
|
|
|
package com.taais.biz.utils;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.File;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
|
|
|
import org.bytedeco.javacv.FFmpegFrameGrabber;
|
|
|
import org.bytedeco.javacv.Frame;
|
|
|
-import org.bytedeco.javacv.Java2DFrameConverter;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.File;
|
|
|
import javax.imageio.ImageIO;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
|
|
+import org.bytedeco.javacv.Java2DFrameConverter;
|
|
|
|
|
|
-import java.nio.file.Files;
|
|
|
-import java.nio.file.Path;
|
|
|
-import java.nio.file.Paths;
|
|
|
|
|
|
-import java.util.stream.Stream;
|
|
|
+import org.bytedeco.opencv.opencv_core.Stream;
|
|
|
|
|
|
public class VideoCapture {
|
|
|
/**
|
|
@@ -37,7 +37,7 @@ public class VideoCapture {
|
|
|
* @throws IOException
|
|
|
* @throws InterruptedException
|
|
|
*/
|
|
|
- public static void startCaputre(String filePath, String savePath__, Long fps)
|
|
|
+ public static void startCaputreFromFile(String filePath, String savePath__, Long fps)
|
|
|
throws IOException {
|
|
|
FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(filePath);
|
|
|
|
|
@@ -76,6 +76,46 @@ public class VideoCapture {
|
|
|
System.out.println("--- 截图完成---");
|
|
|
}
|
|
|
|
|
|
+ public static void startCaputreFromUrl(String videoUrl, String savePath__, Long fps)
|
|
|
+ throws IOException {
|
|
|
+ FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(videoUrl);
|
|
|
+ grabber.setOption("rtsp_transport", "tcp"); // 使用tcp的方式,不然会丢包很严重
|
|
|
+
|
|
|
+ Path savePath = Paths.get(savePath__);
|
|
|
+ if (Files.exists(savePath)) {
|
|
|
+ System.out.println("目录已存在: " + savePath);
|
|
|
+ System.out.println("清空里面的内容");
|
|
|
+ try {
|
|
|
+ deleteDirectory(savePath);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Files.createDirectories(savePath);
|
|
|
+
|
|
|
+ System.out.println("--- 开始截图 ---");
|
|
|
+ grabber.start();
|
|
|
+ long current_time_stamp = 0;
|
|
|
+ long current_time_stamp_step = 1000000 / fps;
|
|
|
+ int idx = 0;
|
|
|
+ while (true) {
|
|
|
+ Frame frame = grabber.grabImage();
|
|
|
+ if (frame != null) {
|
|
|
+ // System.out.println(frame.timestamp);
|
|
|
+ if (frame.timestamp >= current_time_stamp) {
|
|
|
+ idx += 1;
|
|
|
+ current_time_stamp += current_time_stamp_step;
|
|
|
+ saveImage(frame, savePath__, idx);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ grabber.stop();
|
|
|
+ grabber.release();
|
|
|
+ System.out.println("--- 截图完成---");
|
|
|
+ }
|
|
|
+
|
|
|
public static boolean saveImage(Frame frame, String savePath, int idx) {
|
|
|
try {
|
|
|
Path outPath = Paths.get(savePath, System.currentTimeMillis() + "_" + idx + ".jpg");
|
|
@@ -118,7 +158,7 @@ public class VideoCapture {
|
|
|
private static void deleteDirectory(Path directory) throws Exception {
|
|
|
if (Files.isDirectory(directory)) {
|
|
|
// 遍历目录中的所有文件和子目录
|
|
|
- try (Stream<Path> stream = Files.list(directory)) {
|
|
|
+ try (java.util.stream.Stream<Path> stream = Files.list(directory)) {
|
|
|
stream.forEach(path -> {
|
|
|
try {
|
|
|
deleteDirectory(path);
|