| | |
| | | import com.ard.work.mapper.ArdMediaMapper; |
| | | import com.ard.work.service.IArdFlightTaskService; |
| | | import com.ard.work.service.IArdServerInfoService; |
| | | import com.ard.work.util.osSelect; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.bytedeco.javacv.FFmpegFrameGrabber; |
| | | import org.bytedeco.javacv.Frame; |
| | | import org.bytedeco.javacv.Java2DFrameConverter; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.imageio.ImageIO; |
| | | import javax.net.ssl.HttpsURLConnection; |
| | | import javax.net.ssl.SSLContext; |
| | | import javax.net.ssl.TrustManager; |
| | | import javax.net.ssl.X509TrustManager; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.net.URLConnection; |
| | | import java.security.cert.X509Certificate; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.concurrent.CompletableFuture; |
| | |
| | | } |
| | | String boxSn = flightTask.getBoxSn(); |
| | | for (String videoPath : videoPathList) { |
| | | |
| | | //videoPath = "https://192.168.1.3:9001/record/DBM250974065008/8212fd8e89814d66b6c32f48d05f5101.mp4"; |
| | | ArdMedia media = ArdMedia.builder() |
| | | .id(IdUtils.simpleUUID()) |
| | | .taskId(flightTask.getTaskId()) |
| | |
| | | .build(); |
| | | |
| | | // ---- 1. 视频上传(失败不抛异常,返回 null) ---- |
| | | |
| | | CompletableFuture<String> videoFuture = CompletableFuture.supplyAsync(() -> { |
| | | try (InputStream inputStream = openUrlInputStream(videoPath, 5000, 10000)) { |
| | | if (inputStream != null) { |
| | |
| | | return null; |
| | | }, threadPoolTaskExecutor); |
| | | // ---- 2. 缩略图上传(失败不抛异常,返回 null) ---- |
| | | |
| | | CompletableFuture<String> thumbFuture = CompletableFuture.supplyAsync(() -> { |
| | | try { |
| | | try (ByteArrayInputStream thumbStream = generateThumbnail(videoPath, 2, 200)) { |
| | | try (ByteArrayInputStream thumbStream = generateThumbnail(videoPath, 1, 200)) { |
| | | if (thumbStream != null) { |
| | | return ossServiceContext.upload( |
| | | "record", |
| | |
| | | // ---- 2. 缩略图上传(失败不抛异常,返回 null) ---- |
| | | CompletableFuture<String> thumbFuture = CompletableFuture.supplyAsync(() -> { |
| | | try { |
| | | try (ByteArrayInputStream thumbStream = generateThumbnail(videoPath, 2, 200)) { |
| | | try (ByteArrayInputStream thumbStream = generateThumbnail(videoPath, 1, 200)) { |
| | | if (thumbStream != null) { |
| | | return ossServiceContext.upload( |
| | | "record", |
| | |
| | | } |
| | | |
| | | // 生成视频缩略图 |
| | | public ByteArrayInputStream generateThumbnail(String videoFile, int skipFrames, int targetWidth) { |
| | | long start = System.currentTimeMillis(); |
| | | FFmpegFrameGrabber grabber = null; |
| | | public ByteArrayInputStream generateThumbnail1(String videoFile, int skipFrames, int targetWidth) { |
| | | // long start = System.currentTimeMillis(); |
| | | // FFmpegFrameGrabber grabber = null; |
| | | // |
| | | // try { |
| | | // grabber = new FFmpegFrameGrabber(videoFile); |
| | | // grabber.start(); |
| | | // |
| | | // int totalFrames = grabber.getLengthInFrames(); |
| | | // System.out.println("视频总帧数: " + totalFrames); |
| | | // System.out.println("视频时长: " + grabber.getLengthInTime() / 1000000 + "秒"); |
| | | // |
| | | // Frame frame = null; |
| | | // int currentFrame = 0; |
| | | // |
| | | // // 读取指定帧数后的第一帧有效图像 |
| | | // while (currentFrame < totalFrames) { |
| | | // frame = grabber.grabImage(); |
| | | // if (frame == null) { |
| | | // break; |
| | | // } |
| | | // |
| | | // if (currentFrame > skipFrames && frame.image != null) { |
| | | // break; |
| | | // } |
| | | // currentFrame++; |
| | | // } |
| | | // |
| | | // if (frame == null || frame.image == null) { |
| | | // throw new Exception("无法获取有效视频帧"); |
| | | // } |
| | | // |
| | | // // 转换 Frame 为 BufferedImage |
| | | // Java2DFrameConverter converter = new Java2DFrameConverter(); |
| | | // BufferedImage image = converter.getBufferedImage(frame); |
| | | // |
| | | // // 按目标宽度等比缩放 |
| | | // int targetHeight = (int) ((double) targetWidth / image.getWidth() * image.getHeight()); |
| | | // BufferedImage scaledImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_3BYTE_BGR); |
| | | // scaledImage.getGraphics().drawImage(image, 0, 0, targetWidth, targetHeight, null); |
| | | // |
| | | // // 转成 ByteArrayInputStream |
| | | // ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| | | // ImageIO.write(scaledImage, "jpg", baos); |
| | | // long cost = System.currentTimeMillis() - start; |
| | | // System.out.println("处理耗时: " + cost + "ms"); |
| | | // |
| | | // return new ByteArrayInputStream(baos.toByteArray()); |
| | | // |
| | | // } catch (Exception e) { |
| | | // throw new RuntimeException(e); |
| | | // } finally { |
| | | // if (grabber != null) { |
| | | // try { |
| | | // grabber.stop(); |
| | | // grabber.close(); |
| | | // } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // } |
| | | // } |
| | | return null; |
| | | } |
| | | |
| | | public ByteArrayInputStream generateThumbnail( |
| | | String videoFile, |
| | | int skipSeconds, |
| | | int targetWidth) { |
| | | String ffmpegPath = ""; |
| | | if (osSelect.isWindows()) { |
| | | ffmpegPath = "ffmpeg"; |
| | | } else if (osSelect.isLinux()) { |
| | | ffmpegPath = "/usr/local/ffmpeg/bin/ffmpeg"; |
| | | } |
| | | try { |
| | | grabber = new FFmpegFrameGrabber(videoFile); |
| | | grabber.start(); |
| | | List<String> command = new ArrayList<>(); |
| | | command.add(ffmpegPath); |
| | | command.add("-ss"); |
| | | command.add(String.format("00:00:%02d", skipSeconds)); |
| | | command.add("-i"); |
| | | command.add(videoFile); |
| | | command.add("-vframes"); |
| | | command.add("1"); |
| | | |
| | | int totalFrames = grabber.getLengthInFrames(); |
| | | System.out.println("视频总帧数: " + totalFrames); |
| | | System.out.println("视频时长: " + grabber.getLengthInTime() / 1000000 + "秒"); |
| | | // 等比缩放 |
| | | command.add("-vf"); |
| | | command.add("scale=" + targetWidth + ":-1"); |
| | | |
| | | Frame frame = null; |
| | | int currentFrame = 0; |
| | | // **确保输出 JPEG** |
| | | command.add("-f"); |
| | | command.add("mjpeg"); // 关键点:输出格式固定为 JPEG |
| | | command.add("-vcodec"); |
| | | command.add("mjpeg"); |
| | | |
| | | // 读取指定帧数后的第一帧有效图像 |
| | | while (currentFrame < totalFrames) { |
| | | frame = grabber.grabImage(); |
| | | if (frame == null) { |
| | | break; |
| | | } |
| | | // 输出到管道 |
| | | command.add("pipe:1"); |
| | | |
| | | if (currentFrame > skipFrames && frame.image != null) { |
| | | break; |
| | | } |
| | | currentFrame++; |
| | | } |
| | | // ⭐ 打印最终命令(可复制) |
| | | log.info("ffmpeg command: {}", String.join(" ", command)); |
| | | |
| | | if (frame == null || frame.image == null) { |
| | | throw new Exception("无法获取有效视频帧"); |
| | | } |
| | | ProcessBuilder pb = new ProcessBuilder(command); |
| | | pb.redirectErrorStream(false); |
| | | Process process = pb.start(); |
| | | |
| | | // 转换 Frame 为 BufferedImage |
| | | Java2DFrameConverter converter = new Java2DFrameConverter(); |
| | | BufferedImage image = converter.getBufferedImage(frame); |
| | | |
| | | // 按目标宽度等比缩放 |
| | | int targetHeight = (int) ((double) targetWidth / image.getWidth() * image.getHeight()); |
| | | BufferedImage scaledImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_3BYTE_BGR); |
| | | scaledImage.getGraphics().drawImage(image, 0, 0, targetWidth, targetHeight, null); |
| | | |
| | | // 转成 ByteArrayInputStream |
| | | // 读取 FFmpeg 输出(JPEG 数据)到内存 |
| | | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| | | ImageIO.write(scaledImage, "jpg", baos); |
| | | long cost = System.currentTimeMillis() - start; |
| | | System.out.println("处理耗时: " + cost + "ms"); |
| | | try (InputStream is = process.getInputStream()) { |
| | | byte[] buffer = new byte[8192]; |
| | | int len; |
| | | while ((len = is.read(buffer)) != -1) { |
| | | baos.write(buffer, 0, len); |
| | | } |
| | | } |
| | | |
| | | int exitCode = process.waitFor(); |
| | | if (exitCode != 0) { |
| | | throw new RuntimeException("FFmpeg 执行失败,退出码: " + exitCode); |
| | | } |
| | | |
| | | return new ByteArrayInputStream(baos.toByteArray()); |
| | | |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } finally { |
| | | if (grabber != null) { |
| | | try { |
| | | grabber.stop(); |
| | | grabber.close(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } catch (IOException | InterruptedException e) { |
| | | throw new RuntimeException("FFmpeg 生成缩略图失败", e); |
| | | } |
| | | } |
| | | |