| | |
| | | package com.ard.work.service.impl; |
| | | |
| | | import com.ard.common.constant.CacheConstants; |
| | | import com.ard.common.core.domain.entity.SysUser; |
| | | import com.ard.common.core.redis.RedisCache; |
| | | import com.ard.common.utils.DateUtils; |
| | | import com.ard.common.utils.file.MimeTypeUtils; |
| | | import com.ard.common.utils.uuid.IdUtils; |
| | |
| | | import com.ard.work.mapper.ArdMediaMapper; |
| | | import com.ard.work.service.IArdFlightTaskService; |
| | | import com.ard.work.service.IArdServerInfoService; |
| | | import com.ard.work.service.control.UavControlService; |
| | | import com.ard.work.util.osSelect; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.bytedeco.javacpp.BytePointer; |
| | | import org.bytedeco.javacv.FFmpegFrameGrabber; |
| | | import org.bytedeco.javacv.Frame; |
| | | import org.bytedeco.javacv.Java2DFrameConverter; |
| | | import org.bytedeco.opencv.global.opencv_imgproc; |
| | | import org.bytedeco.opencv.opencv_core.Size; |
| | | import org.opencv.core.Mat; |
| | | import org.opencv.videoio.VideoCapture; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | 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.*; |
| | | import java.awt.image.BufferedImage; |
| | | import java.awt.image.DataBufferByte; |
| | | import java.io.*; |
| | | import javax.net.ssl.HttpsURLConnection; |
| | | import javax.net.ssl.SSLContext; |
| | | import javax.net.ssl.TrustManager; |
| | | import javax.net.ssl.X509TrustManager; |
| | | 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; |
| | | import java.util.stream.Collectors; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.ByteArrayOutputStream; |
| | | |
| | | @Slf4j |
| | | @Service |
| | |
| | | private OssServiceContext ossServiceContext; |
| | | @Resource |
| | | private ThreadPoolTaskExecutor threadPoolTaskExecutor; // 使用线程池执行异步任务 |
| | | |
| | | @Resource |
| | | private UavControlService uavControlService; |
| | | @Autowired |
| | | private RedisCache redisCache; |
| | | /** |
| | | * 查询飞行架次列表 |
| | | * |
| | |
| | | .startTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, boxHistoryReceiver.getStartTime())) |
| | | .endTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, boxHistoryReceiver.getEndTime())) |
| | | .build(); |
| | | |
| | | Long flyUserId = uavControlService.getCurrentController(flightTask.getBoxSn(), "fly"); |
| | | if (flyUserId != null) { |
| | | SysUser flyUser = redisCache.getCacheObject(CacheConstants.SYS_USER_KEY + flyUserId); |
| | | if (flyUser != null && flyUser.getNickName() != null) { |
| | | flightTask.setCreateBy(flyUser.getNickName()); |
| | | } |
| | | } |
| | | flightTask.setCreateTime(DateUtils.getNowDate()); |
| | | // 插入新记录 |
| | | int rows = ardFlightTaskMapper.insertArdFlightTask(flightTask); |
| | |
| | | } else { |
| | | // 如果存在,则更新记录 |
| | | log.debug("Updating task {}", boxHistoryReceiver.getTaskId()); |
| | | updateFlightTask(ardFlightTask, boxHistoryReceiver); |
| | | // 增量更新 |
| | | updateVideoToDatabase(ardFlightTask, boxHistoryReceiver.getVideoPathList()); |
| | | // 增量更新图片 |
| | | updatePhotoToDatabase(ardFlightTask, serverId, boxHistoryReceiver.getBoxSn()); |
| | | |
| | | } |
| | | } |
| | | private void updateFlightTask(ArdFlightTask local, BoxHistoryReceiver remote) { |
| | | |
| | | boolean needUpdate = false; |
| | | |
| | | // endTime 更新(重要!!!) |
| | | Date newEnd = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, remote.getEndTime()); |
| | | if (newEnd != null && (local.getEndTime() == null || !local.getEndTime().equals(newEnd))) { |
| | | local.setEndTime(newEnd); |
| | | needUpdate = true; |
| | | } |
| | | |
| | | // photoCount 更新 |
| | | int remotePhotoCount = Integer.parseInt(remote.getPhotoCount()); |
| | | if (local.getPhotoCount() != remotePhotoCount) { |
| | | local.setPhotoCount(remotePhotoCount); |
| | | needUpdate = true; |
| | | } |
| | | |
| | | // videoCount 更新 |
| | | int remoteVideoCount = remote.getVideoPathList() != null ? remote.getVideoPathList().size() : 0; |
| | | if (local.getVideoCount() != remoteVideoCount) { |
| | | local.setVideoCount(remoteVideoCount); |
| | | needUpdate = true; |
| | | } |
| | | |
| | | if (needUpdate) { |
| | | ardFlightTaskMapper.updateArdFlightTask(local); |
| | | } |
| | | |
| | | // 其它媒体增量更新 |
| | | updateVideoToDatabase(local, remote.getVideoPathList()); |
| | | updatePhotoToDatabase(local, local.getServerId(), remote.getBoxSn()); |
| | | } |
| | | |
| | | |
| | |
| | | |
| | | // 插入视频记录 |
| | | private void insertVideoToDatabase(ArdFlightTask flightTask, List<String> videoPathList) { |
| | | if (videoPathList != null && !videoPathList.isEmpty()) { |
| | | for (String videoPath : videoPathList) { |
| | | ArdMedia ardMedia = ArdMedia.builder() |
| | | .id(IdUtils.simpleUUID()) |
| | | .taskId(flightTask.getTaskId()) |
| | | .mediaType(1) // 1表示视频 |
| | | .sourceUrl(videoPath) |
| | | .fileSize(getFileSizeByUrl(videoPath)) |
| | | .build(); |
| | | CompletableFuture.supplyAsync(() -> { |
| | | try { |
| | | // 上传视频 |
| | | String videoUrl = uploadFileToOss(flightTask.getBoxSn(), videoPath, "record", |
| | | MimeTypeUtils.VIDEO_MP4); |
| | | ardMedia.setMediaUrl(videoUrl); |
| | | // 生成缩略图 InputStream |
| | | ByteArrayInputStream thumbnailStream = generateThumbnailInputStream(videoPath, 2, 200); |
| | | if (thumbnailStream != null) { |
| | | String thumbnailUrl = ossServiceContext.upload("record", |
| | | flightTask.getBoxSn() + "/" + IdUtils.fastSimpleUUID() + ".jpg", |
| | | thumbnailStream, |
| | | MimeTypeUtils.IMAGE_JPEG); |
| | | ardMedia.setThumbnailUrl(thumbnailUrl); |
| | | if (videoPathList == null || videoPathList.isEmpty()) { |
| | | return; |
| | | } |
| | | 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()) |
| | | .mediaType(1) |
| | | .sourceUrl(videoPath) |
| | | .fileSize(getFileSizeByUrl(videoPath)) |
| | | .build(); |
| | | |
| | | // ---- 1. 视频上传(失败不抛异常,返回 null) ---- |
| | | |
| | | CompletableFuture<String> videoFuture = CompletableFuture.supplyAsync(() -> { |
| | | try (InputStream inputStream = openUrlInputStream(videoPath, 5000, 10000)) { |
| | | if (inputStream != null) { |
| | | return ossServiceContext.upload( |
| | | "record", |
| | | boxSn + "/" + media.getId() + ".mp4", |
| | | inputStream, |
| | | MimeTypeUtils.VIDEO_MP4 |
| | | ); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | }, threadPoolTaskExecutor); |
| | | // ---- 2. 缩略图上传(失败不抛异常,返回 null) ---- |
| | | |
| | | CompletableFuture<String> thumbFuture = CompletableFuture.supplyAsync(() -> { |
| | | try { |
| | | try (ByteArrayInputStream thumbStream = generateThumbnail(videoPath, 1, 200)) { |
| | | if (thumbStream != null) { |
| | | return ossServiceContext.upload( |
| | | "record", |
| | | boxSn + "/" + media.getId() + ".jpg", |
| | | thumbStream, |
| | | MimeTypeUtils.IMAGE_JPEG |
| | | ); |
| | | } |
| | | |
| | | ardMedia.setCreateTime(new Date()); |
| | | return ardMedia; |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | }, threadPoolTaskExecutor).thenAccept(am -> { |
| | | if (am != null) { |
| | | ardMediaMapper.insertArdMedia(am); |
| | | } |
| | | }); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | }, threadPoolTaskExecutor); |
| | | |
| | | } |
| | | // 两个完成后写数据库(任意失败不阻断流程) |
| | | videoFuture.thenCombine(thumbFuture, (vUrl, tUrl) -> { |
| | | media.setMediaUrl(vUrl); |
| | | media.setThumbnailUrl(tUrl); |
| | | media.setCreateTime(new Date()); |
| | | return media; |
| | | }).thenAcceptAsync(ardMediaMapper::insertArdMedia, threadPoolTaskExecutor); |
| | | } |
| | | } |
| | | |
| | |
| | | if (videoPathList == null) { |
| | | return; |
| | | } |
| | | Integer count = flightTask.getVideoCount();//获取本地库视频数量 |
| | | // 需要插入新的视频 |
| | | List<ArdMedia> existingVideos = ardMediaMapper.selectArdMediaByTaskIdAndType(flightTask.getTaskId(), 1); |
| | | Integer count = existingVideos.size();//获取本地库视频数量 |
| | | int size = videoPathList.size();//获取接口返回的视频数量 |
| | | if (count != null && size > count) { |
| | | log.info("录像数量不一致,需要更新数据库"); |
| | | // 需要插入新的视频 |
| | | List<ArdMedia> existingVideos = ardMediaMapper.selectArdMediaByTaskIdAndType(flightTask.getTaskId(), 1); |
| | | log.info("任务【{}】录像数量不一致,需要更新数据库", flightTask.getTaskId()); |
| | | |
| | | // 提取现有的视频路径 |
| | | List<String> existingVideoPaths = existingVideos.stream() |
| | | .map(ArdMedia::getSourceUrl) // 获取每个视频的 URL |
| | |
| | | .filter(videoPath -> !existingVideoPaths.contains(videoPath)) // 过滤出新的视频 |
| | | .collect(Collectors.toList()); |
| | | if (!newVideos.isEmpty()) { |
| | | String boxSn = flightTask.getBoxSn(); |
| | | // 插入新的视频记录 |
| | | for (String videoPath : newVideos) { |
| | | ArdMedia ardMedia = ArdMedia.builder() |
| | |
| | | .fileSize(getFileSizeByUrl(videoPath)) |
| | | .sourceUrl(videoPath) |
| | | .build(); |
| | | CompletableFuture.supplyAsync(() -> { |
| | | try { |
| | | // 上传视频 |
| | | String videoUrl = uploadFileToOss(flightTask.getBoxSn(), videoPath, "record", |
| | | MimeTypeUtils.VIDEO_MP4); |
| | | ardMedia.setMediaUrl(videoUrl); |
| | | |
| | | |
| | | // 生成缩略图 InputStream |
| | | ByteArrayInputStream thumbnailStream = generateThumbnailInputStream(videoPath, 2, 200); |
| | | if (thumbnailStream != null) { |
| | | String thumbnailUrl = ossServiceContext.upload("record", |
| | | flightTask.getBoxSn() + "/" + IdUtils.fastSimpleUUID() + ".jpg", |
| | | thumbnailStream, |
| | | MimeTypeUtils.IMAGE_JPEG); |
| | | ardMedia.setThumbnailUrl(thumbnailUrl); |
| | | // ---- 1. 视频上传(失败不抛异常,返回 null) ---- |
| | | CompletableFuture<String> videoFuture = CompletableFuture.supplyAsync(() -> { |
| | | try (InputStream inputStream = openUrlInputStream(videoPath, 5000, 10000)) { |
| | | if (inputStream != null) { |
| | | return ossServiceContext.upload( |
| | | "record", |
| | | boxSn + "/" + ardMedia.getId() + ".mp4", |
| | | inputStream, |
| | | MimeTypeUtils.VIDEO_MP4 |
| | | ); |
| | | } |
| | | |
| | | ardMedia.setCreateTime(new Date()); |
| | | return ardMedia; |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | }, threadPoolTaskExecutor).thenAccept(am -> { |
| | | if (am != null) { |
| | | ardMediaMapper.insertArdMedia(am); |
| | | return null; |
| | | }, threadPoolTaskExecutor); |
| | | // ---- 2. 缩略图上传(失败不抛异常,返回 null) ---- |
| | | CompletableFuture<String> thumbFuture = CompletableFuture.supplyAsync(() -> { |
| | | try { |
| | | try (ByteArrayInputStream thumbStream = generateThumbnail(videoPath, 1, 200)) { |
| | | if (thumbStream != null) { |
| | | return ossServiceContext.upload( |
| | | "record", |
| | | boxSn + "/" + ardMedia.getId() + ".jpg", |
| | | thumbStream, |
| | | MimeTypeUtils.IMAGE_JPEG |
| | | ); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | }); |
| | | return null; |
| | | }, threadPoolTaskExecutor); |
| | | // 两个完成后写数据库(任意失败不阻断流程) |
| | | videoFuture.thenCombine(thumbFuture, (vUrl, tUrl) -> { |
| | | ardMedia.setMediaUrl(vUrl); |
| | | ardMedia.setThumbnailUrl(tUrl); |
| | | ardMedia.setCreateTime(new Date()); |
| | | return ardMedia; |
| | | }).thenAcceptAsync(ardMediaMapper::insertArdMedia, threadPoolTaskExecutor); |
| | | } |
| | | // 更新任务的视频数量 |
| | | flightTask.setVideoCount(flightTask.getVideoCount() + newVideos.size()); |
| | |
| | | } |
| | | |
| | | private void updatePhotoToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) { |
| | | int count = flightTask.getPhotoCount(); //获取本地库图片数量 |
| | | |
| | | // 获取本地数据库中已存在的图片记录(类型为 0 表示图片) |
| | | List<ArdMedia> existingPhotos = ardMediaMapper.selectArdMediaByTaskIdAndType(flightTask.getTaskId(), 0); |
| | | int count = existingPhotos.size(); //获取本地库图片数量 |
| | | // 查询图片 |
| | | BoxPhotosDTO boxPhotosDTO = BoxPhotosDTO.builder() |
| | | .taskId(flightTask.getTaskId()) |
| | |
| | | int size = photoList.size(); |
| | | |
| | | if (size > count) { |
| | | log.info("图片数量不一致,需要更新数据库"); |
| | | // 获取本地数据库中已存在的图片记录(类型为 0 表示图片) |
| | | List<ArdMedia> existingPhotos = ardMediaMapper.selectArdMediaByTaskIdAndType(flightTask.getTaskId(), 0); |
| | | log.info("任务【{}】图片数量不一致,需要更新数据库", flightTask.getTaskId()); |
| | | // 需要插入新的视频 |
| | | // 提取现有的图片路径(根据 imgUrl 来判断是否重复) |
| | | List<String> existingPhotoUrls = existingPhotos.stream() |
| | |
| | | } |
| | | |
| | | private void addPhotos(ArdFlightTask flightTask, List<BoxPhotosReceiver> photoList) { |
| | | String boxSn = flightTask.getBoxSn(); |
| | | for (BoxPhotosReceiver photo : photoList) { |
| | | ArdMedia ardMedia = ArdMedia.builder() |
| | | .id(IdUtils.simpleUUID()) |
| | |
| | | .captureTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, photo.getTimeStamp())) |
| | | .build(); |
| | | // 异步上传文件到OSS,并在上传完成后插入数据库 |
| | | CompletableFuture.supplyAsync(() -> uploadFileToOss(flightTask.getBoxSn(), photo.getImgUrl(), "pic", |
| | | MimeTypeUtils.IMAGE_JPG), threadPoolTaskExecutor) |
| | | CompletableFuture.supplyAsync(() -> { |
| | | try (InputStream inputStream = openUrlInputStream(photo.getImgUrl(), 5000, 10000)) { |
| | | if (inputStream != null) { |
| | | return ossServiceContext.upload( |
| | | "pic", |
| | | boxSn + "/" + ardMedia.getId() + ".jpg", |
| | | inputStream, |
| | | MimeTypeUtils.IMAGE_JPG |
| | | ); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | }, threadPoolTaskExecutor) |
| | | .thenAccept(url -> { |
| | | ardMedia.setMediaUrl(url); // 获取上传后的文件 URL |
| | | ardMedia.setMediaUrl(url); |
| | | ardMedia.setCreateTime(new Date()); |
| | | ardMediaMapper.insertArdMedia(ardMedia); // 插入数据库 |
| | | ardMediaMapper.insertArdMedia(ardMedia); |
| | | }); |
| | | } |
| | | } |
| | | |
| | | |
| | | // 通用的上传方法,支持视频和图片等文件上传 |
| | | private String uploadFileToOss(String boxSn, String fileUrl, String bucket, String contentType) { |
| | | InputStream fileInputStream = null; |
| | | String fileOssUrl = ""; |
| | | try { |
| | | // 创建信任所有证书的SSLContext |
| | | TrustManager[] trustAllCerts = new TrustManager[]{ |
| | | new X509TrustManager() { |
| | | @Override |
| | | public java.security.cert.X509Certificate[] getAcceptedIssuers() { |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public void checkClientTrusted( |
| | | java.security.cert.X509Certificate[] certs, String authType) { |
| | | } |
| | | |
| | | @Override |
| | | public void checkServerTrusted( |
| | | java.security.cert.X509Certificate[] certs, String authType) { |
| | | } |
| | | } |
| | | }; |
| | | |
| | | SSLContext sc = SSLContext.getInstance("SSL"); |
| | | sc.init(null, trustAllCerts, new java.security.SecureRandom()); |
| | | HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); |
| | | |
| | | // 创建所有主机名都信任的HostnameVerifier |
| | | HostnameVerifier allHostsValid = (hostname, session) -> true; |
| | | HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); |
| | | |
| | | // 现在可以正常访问 |
| | | |
| | | // 从URL创建InputStream |
| | | URL url = new URL(fileUrl); |
| | | fileInputStream = url.openStream(); |
| | | |
| | | // 文件名 |
| | | String fileName = boxSn + "/" + IdUtils.fastSimpleUUID() + "." + MimeTypeUtils.getExtension(contentType); |
| | | |
| | | // 调用OSS服务上下文进行文件上传 |
| | | fileOssUrl = ossServiceContext.upload(bucket, fileName, fileInputStream, contentType); |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | // 异常处理,可以选择记录日志或者抛出业务异常 |
| | | } finally { |
| | | // 关闭InputStream |
| | | if (fileInputStream != null) { |
| | | try { |
| | | fileInputStream.close(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | return fileOssUrl; // 返回上传后的文件OSS URL |
| | | } |
| | | |
| | | // 获取文件大小 |
| | | private long getFileSizeByUrl(String fileUrl) { |
| | |
| | | } |
| | | |
| | | // 生成视频缩略图 |
| | | public ByteArrayInputStream generateThumbnailInputStream(String videoFile, |
| | | int skipFrames, |
| | | int targetWidth) throws Exception { |
| | | long start = System.currentTimeMillis(); |
| | | FFmpegFrameGrabber grabber = 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); |
| | | |
| | | int totalFrames = grabber.getLengthInFrames(); |
| | | System.out.println("视频总帧数: " + totalFrames); |
| | | System.out.println("视频时长: " + grabber.getLengthInTime() / 1000000 + "秒"); |
| | | command.add("-timeout"); |
| | | command.add("3000000"); |
| | | command.add("-rw_timeout"); |
| | | command.add("3000000"); |
| | | |
| | | Frame frame = null; |
| | | int currentFrame = 0; |
| | | command.add("-ss"); |
| | | command.add(String.format("00:00:%02d", skipSeconds)); |
| | | command.add("-i"); |
| | | command.add(videoFile); |
| | | command.add("-vframes"); |
| | | command.add("1"); |
| | | |
| | | // 读取指定帧数后的第一帧有效图像 |
| | | while (currentFrame < totalFrames) { |
| | | frame = grabber.grabImage(); |
| | | if (frame == null) { |
| | | break; |
| | | } |
| | | // 等比缩放 |
| | | command.add("-vf"); |
| | | command.add("scale=" + targetWidth + ":-1"); |
| | | |
| | | if (currentFrame > skipFrames && frame.image != null) { |
| | | break; |
| | | } |
| | | currentFrame++; |
| | | } |
| | | // **确保输出 JPEG** |
| | | command.add("-f"); |
| | | command.add("mjpeg"); // 关键点:输出格式固定为 JPEG |
| | | command.add("-vcodec"); |
| | | command.add("mjpeg"); |
| | | |
| | | if (frame == null || frame.image == null) { |
| | | throw new Exception("无法获取有效视频帧"); |
| | | } |
| | | // 输出到管道 |
| | | command.add("pipe:1"); |
| | | |
| | | // 转换 Frame 为 BufferedImage |
| | | Java2DFrameConverter converter = new Java2DFrameConverter(); |
| | | BufferedImage image = converter.getBufferedImage(frame); |
| | | // ⭐ 打印最终命令(可复制) |
| | | log.info("ffmpeg command: {}", String.join(" ", command)); |
| | | |
| | | // 按目标宽度等比缩放 |
| | | 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); |
| | | ProcessBuilder pb = new ProcessBuilder(command); |
| | | pb.redirectErrorStream(false); |
| | | Process process = pb.start(); |
| | | |
| | | // 转成 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()); |
| | | |
| | | } finally { |
| | | if (grabber != null) { |
| | | try { |
| | | grabber.stop(); |
| | | grabber.close(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } catch (IOException | InterruptedException e) { |
| | | throw new RuntimeException("FFmpeg 生成缩略图失败", e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将 URL 转成 InputStream |
| | | * |
| | | * @param fileUrl 文件 URL |
| | | * @param connectTimeoutMillis 连接超时(毫秒) |
| | | * @param readTimeoutMillis 读取超时(毫秒) |
| | | * @return InputStream,如果失败返回 null |
| | | */ |
| | | public InputStream openUrlInputStream(String fileUrl, int connectTimeoutMillis, int readTimeoutMillis) { |
| | | try { |
| | | URL url = new URL(fileUrl); |
| | | |
| | | // 支持 HTTPS 信任所有证书(如果需要访问自签名证书) |
| | | if ("https".equalsIgnoreCase(url.getProtocol())) { |
| | | TrustManager[] trustAllCerts = new TrustManager[]{ |
| | | new X509TrustManager() { |
| | | @Override |
| | | public void checkClientTrusted(X509Certificate[] chain, String authType) { |
| | | } |
| | | |
| | | @Override |
| | | public void checkServerTrusted(X509Certificate[] chain, String authType) { |
| | | } |
| | | |
| | | @Override |
| | | public X509Certificate[] getAcceptedIssuers() { |
| | | return null; |
| | | } |
| | | } |
| | | }; |
| | | SSLContext sc = SSLContext.getInstance("SSL"); |
| | | sc.init(null, trustAllCerts, new java.security.SecureRandom()); |
| | | HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); |
| | | HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true); |
| | | } |
| | | |
| | | URLConnection connection = url.openConnection(); |
| | | connection.setConnectTimeout(connectTimeoutMillis); |
| | | connection.setReadTimeout(readTimeoutMillis); |
| | | |
| | | return connection.getInputStream(); |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | } |