| | |
| | | private ArdMediaPicMapper ardMediaPicMapper; |
| | | @Resource |
| | | private ArdMediaVideoMapper ardMediaVideoMapper; |
| | | @Resource |
| | | private OssServiceContext ossServiceContext; |
| | | |
| | | @Resource |
| | | private ArdFlightTaskMapper ardFlightTaskMapper; |
| | | @Resource |
| | | private ThreadPoolTaskExecutor threadPoolTaskExecutor; |
| | | |
| | | private final Set<Long> uploadingVideos = ConcurrentHashMap.newKeySet(); |
| | | private final Set<Long> uploadingPics = ConcurrentHashMap.newKeySet(); |
| | | |
| | | // 调用接口获取架次图片 |
| | | private List<BoxPhotosReceiver> getBoxPhotos(String serverId, BoxPhotosDTO boxPhotosDTO) { |
| | |
| | | ardMediaVideoMapper.insertArdMediaVideo(ardMedia); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 处理视频缩略图 |
| | | */ |
| | | private void processVideoThumbnail(ArdMediaVideo video) { |
| | | // 生成缩略图 |
| | | try (ByteArrayInputStream in = generateThumbnail(video.getSourceUrl())) { |
| | | String thumbUrl = ossServiceContext.upload( |
| | | "record", |
| | | video.getTaskId() + "/" + video.getId() + ".jpg", |
| | | in, |
| | | MimeTypeUtils.IMAGE_JPEG |
| | | ); |
| | | video.setThumbnailUrl(thumbUrl); |
| | | } catch (Exception e) { |
| | | log.warn("视频缩略图生成或上传失败 taskId={}, videoId={}", video.getTaskId(), video.getId(), e); |
| | | } |
| | | // 获取文件大小 |
| | | try { |
| | | long videoSize = getFileSizeFromUrl(video.getSourceUrl()); |
| | | video.setFileSize(videoSize); |
| | | } catch (Exception e) { |
| | | log.warn("获取视频大小失败 url={}", video.getSourceUrl(), e); |
| | | } |
| | | // 更新数据库 |
| | | video.setUpdateTime(new Date()); |
| | | ardMediaVideoMapper.updateArdMediaVideo(video); |
| | | } |
| | | |
| | | public long getFileSizeFromUrl(String fileUrl) throws IOException { |
| | | URL url = new URL(fileUrl); |
| | | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| | | conn.setRequestMethod("HEAD"); |
| | | conn.setConnectTimeout(5000); |
| | | conn.setReadTimeout(5000); |
| | | conn.connect(); |
| | | long size = conn.getContentLengthLong(); // 返回字节数 |
| | | conn.disconnect(); |
| | | return size; |
| | | } |
| | | |
| | | |
| | | private ByteArrayInputStream generateThumbnail(String videoFile) { |
| | | |
| | | String ffmpeg = osSelect.isWindows() |
| | | ? "ffmpeg" |
| | | : "/usr/local/ffmpeg/bin/ffmpeg"; |
| | | |
| | | Path tempImg = null; |
| | | try { |
| | | tempImg = Files.createTempFile("thumb_", ".jpg"); |
| | | |
| | | List<String> cmd = new ArrayList<>(); |
| | | cmd.add(ffmpeg); |
| | | cmd.add("-y"); |
| | | cmd.add("-loglevel"); |
| | | cmd.add("error"); |
| | | cmd.add("-i"); |
| | | cmd.add(videoFile); |
| | | cmd.add("-ss"); |
| | | cmd.add(String.valueOf(1)); |
| | | cmd.add("-vframes"); |
| | | cmd.add("1"); |
| | | cmd.add("-vf"); |
| | | cmd.add("scale=" + 200 + ":-1"); |
| | | cmd.add(tempImg.toAbsolutePath().toString()); |
| | | |
| | | log.info("ffmpeg cmd: {}", String.join(" ", cmd)); |
| | | |
| | | Process process = new ProcessBuilder(cmd).start(); |
| | | |
| | | drain(process.getErrorStream()); |
| | | |
| | | boolean finished = process.waitFor(15, TimeUnit.SECONDS); // 超时保护 |
| | | if (!finished) { |
| | | process.destroyForcibly(); |
| | | throw new RuntimeException("ffmpeg timeout"); |
| | | } |
| | | |
| | | if (process.exitValue() != 0) { |
| | | throw new RuntimeException("ffmpeg exit=" + process.exitValue()); |
| | | } |
| | | |
| | | byte[] data = Files.readAllBytes(tempImg); |
| | | return new ByteArrayInputStream(data); |
| | | |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("生成缩略图失败", e); |
| | | } finally { |
| | | if (tempImg != null) { |
| | | try { |
| | | Files.deleteIfExists(tempImg); |
| | | } catch (IOException ignore) { |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | private static void drain(InputStream is) { |
| | | new Thread(() -> { |
| | | try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) { |
| | | while (br.readLine() != null) { |
| | | } |
| | | } catch (IOException ignore) { |
| | | } |
| | | }, "ffmpeg-stderr").start(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 将 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; |
| | | } |
| | | } |
| | | |
| | | @Scheduled(fixedDelay = 10000) // 每10秒执行一次 |
| | | public void syncVideoMediaUrl() { |
| | | List<ArdMediaVideo> videos = ardMediaVideoMapper.selectVideosWithNullMedia(); |
| | | for (ArdMediaVideo video : videos) { |
| | | if (!uploadingVideos.add(video.getId())) { |
| | | continue; |
| | | } |
| | | CompletableFuture.runAsync(() -> { |
| | | try { |
| | | // 上传视频到 OSS |
| | | try (InputStream is = openUrlInputStream(video.getSourceUrl(), 5000, 10000)) { |
| | | if (is != null) { |
| | | String mediaUrl = ossServiceContext.upload( |
| | | "record", |
| | | video.getTaskId() + "/" + video.getId() + ".mp4", |
| | | is, |
| | | MimeTypeUtils.VIDEO_MP4 |
| | | ); |
| | | log.info("upload mediaUrl={}", mediaUrl); |
| | | video.setMediaUrl(mediaUrl); |
| | | } |
| | | } |
| | | // 生成缩略图 |
| | | processVideoThumbnail(video); |
| | | |
| | | // 更新数据库 |
| | | video.setUpdateTime(new Date()); |
| | | ardMediaVideoMapper.updateArdMediaVideo(video); |
| | | } catch (Exception e) { |
| | | log.error("生成 mediaUrl/缩略图失败 videoId={}", video.getId(), e); |
| | | } finally { |
| | | uploadingVideos.remove(video.getId()); |
| | | } |
| | | }, threadPoolTaskExecutor); |
| | | } |
| | | } |
| | | |
| | | @Scheduled(fixedDelay = 10000) // 每10秒执行一次 |
| | | public void syncPicMediaUrl() { |
| | | List<ArdMediaPic> pics = ardMediaPicMapper.selectPicsWithNullMedia(); |
| | | for (ArdMediaPic pic : pics) { |
| | | if (!uploadingPics.add(pic.getId())) { |
| | | continue; |
| | | } |
| | | CompletableFuture.runAsync(() -> { |
| | | try (InputStream inputStream = openUrlInputStream(pic.getSourceUrl(), 5000, 10000)) { |
| | | if (inputStream != null) { |
| | | String url = ossServiceContext.upload( |
| | | "pic", |
| | | pic.getTaskId() + "/" + pic.getId() + ".jpg", |
| | | inputStream, |
| | | MimeTypeUtils.IMAGE_JPG |
| | | ); |
| | | pic.setMediaUrl(url); |
| | | pic.setUpdateTime(new Date()); |
| | | ardMediaPicMapper.updatePic(pic); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("图片上传失败 taskId={}, photoId={}", pic.getTaskId(), pic.getId(), e); |
| | | } finally { |
| | | uploadingPics.remove(pic.getId()); |
| | | } |
| | | |
| | | }, threadPoolTaskExecutor); // 使用自定义线程池 |
| | | } |
| | | } |
| | | |
| | | } |