liusuyi
2025-12-28 7e4432176e90f5710e108037e473afc106229af8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package com.ard.work.service.impl;
 
import com.ard.common.utils.DateUtils;
import com.ard.common.utils.file.MimeTypeUtils;
import com.ard.oss.service.impl.OssServiceContext;
import com.ard.work.component.forest.TyClient;
import com.ard.work.domain.dto.BoxPhotosDTO;
import com.ard.work.domain.dto.BoxVideosDTO;
import com.ard.work.domain.entity.ArdFlightTask;
import com.ard.work.domain.entity.ArdMediaPic;
import com.ard.work.domain.entity.ArdMediaVideo;
import com.ard.work.domain.receiver.BoxPhotosReceiver;
import com.ard.work.domain.receiver.BoxVideosReceiver;
import com.ard.work.domain.receiver.TyHttpCommonReceiver;
import com.ard.work.mapper.ArdFlightTaskMapper;
import com.ard.work.mapper.ArdMediaPicMapper;
import com.ard.work.mapper.ArdMediaVideoMapper;
import com.ard.work.service.IArdMediaService;
import com.ard.work.util.osSelect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
/**
 * Description:
 * Author: lsy
 * Date: 2025年11月18日13:17
 **/
@Slf4j
@Service
public class ArdMediaServiceImpl implements IArdMediaService {
 
    @Resource
    private TyClient tyClient;
    @Resource
    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) {
        TyHttpCommonReceiver<List<BoxPhotosReceiver>> boxPhotos = tyClient.getBoxPhotos(serverId, boxPhotosDTO);
        return boxPhotos.getContent();
    }
 
    // 调用接口获取架次视频
    private List<BoxVideosReceiver> getBoxVideos(String serverId, BoxVideosDTO boxVideosDTO) {
        TyHttpCommonReceiver<List<BoxVideosReceiver>> boxVideos = tyClient.getBoxVideos(serverId, boxVideosDTO);
        return boxVideos.getContent();
    }
 
    @Override
    public List<ArdMediaPic> selectArdMediaPicList(ArdMediaPic ardMediaPic) {
        return ardMediaPicMapper.selectPicList(ardMediaPic);
    }
 
    @Override
    public List<ArdMediaVideo> selectArdMediaVideoList(ArdMediaVideo ardMediaVideo) {
        return ardMediaVideoMapper.selectArdMediaVideoList(ardMediaVideo);
    }
 
    @Override
    public void insertPhotoToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) {
        log.warn("开始获取图片信息");
        // 查询图片
        BoxPhotosDTO boxPhotosDTO = new BoxPhotosDTO();
        boxPhotosDTO.setTaskId(flightTask.getTaskId());
        boxPhotosDTO.setBoxSn(boxSn);
        boxPhotosDTO.setPageIndex(1);
        boxPhotosDTO.setPageSize(100);
        List<BoxPhotosReceiver> photoList = getBoxPhotos(serverId, boxPhotosDTO);
        if (photoList != null && !photoList.isEmpty()) {
            addPhotos(flightTask, photoList);
        }
    }
 
    @Override
    public void insertVideoToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) {
        log.warn("开始获取视频信息");
        BoxVideosDTO boxVideosDTO = new BoxVideosDTO();
        boxVideosDTO.setTaskId(flightTask.getTaskId());
        boxVideosDTO.setBoxSn(boxSn);
        boxVideosDTO.setPageIndex(1);
        boxVideosDTO.setPageSize(100);
        List<BoxVideosReceiver> videoList = getBoxVideos(serverId, boxVideosDTO);
        if (videoList != null && !videoList.isEmpty()) {
            addVideos(flightTask, videoList);
        }
    }
 
    @Override
    public void syncPicToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) {
 
        // 获取本地数据库中已存在的图片记录
        List<ArdMediaPic> existingPhotos = ardMediaPicMapper.selectPicByTaskId(flightTask.getTaskId());
        int count = existingPhotos.size(); //获取本地库图片数量
        // 查询图片
        BoxPhotosDTO boxPhotosDTO = BoxPhotosDTO.builder()
                .taskId(flightTask.getTaskId())
                .boxSn(boxSn)
                .pageIndex(1)
                .pageSize(500)
                .build();
        TyHttpCommonReceiver<List<BoxPhotosReceiver>> boxPhotos = tyClient.getBoxPhotos(serverId, boxPhotosDTO);
        List<BoxPhotosReceiver> photoList = boxPhotos.getContent();
        int size = photoList.size();
 
        if (size > count) {
            log.info("任务【{}】图片数量不一致,需要更新数据库", flightTask.getTaskId());
            // 需要插入新的视频
            // 提取现有的图片路径(根据 imgUrl 来判断是否重复)
            List<String> existingPhotoUrls = existingPhotos.stream()
                    .map(ArdMediaPic::getSourceUrl) // 提取 ArdMediaPic 中的图片 URL
                    .collect(Collectors.toList());
 
            // 筛选出 photoList 中没有出现在 existingPhotos 中的新图片
            List<BoxPhotosReceiver> newPhotos = photoList.stream()
                    .filter(photo -> !existingPhotoUrls.contains(photo.getImgUrl())) // 过滤出新图片
                    .collect(Collectors.toList());
            if (!newPhotos.isEmpty()) {
                // 插入新的图片记录
                addPhotos(flightTask, newPhotos);
            }
        }
    }
 
    @Override
    public void syncVideoToDatabase(ArdFlightTask flightTask, String serverId, String boxSn) {
 
        // 获取本地数据库中已存在的视频记录
        List<ArdMediaVideo> existingVideos = ardMediaVideoMapper.selectArdMediaVideoByTaskId(flightTask.getTaskId());
        int count = existingVideos.size(); //获取本地库视频数量
        // 查询图片
        BoxVideosDTO boxVideosDTO = BoxVideosDTO.builder()
                .taskId(flightTask.getTaskId())
                .boxSn(boxSn)
                .pageIndex(1)
                .pageSize(500)
                .build();
        TyHttpCommonReceiver<List<BoxVideosReceiver>> boxVideos = tyClient.getBoxVideos(serverId, boxVideosDTO);
        List<BoxVideosReceiver> videoList = boxVideos.getContent();
        int size = videoList.size();
 
        if (size > count) {
            log.info("任务【{}】视频数量不一致,需要更新数据库", flightTask.getTaskId());
            // 需要插入新的视频
            // 提取现有的图片路径(根据 imgUrl 来判断是否重复)
            List<String> existingVideoUrls = existingVideos.stream()
                    .map(ArdMediaVideo::getSourceUrl) // 提取 ArdMedia 中的视频 URL
                    .collect(Collectors.toList());
 
            // 筛选出 photoList 中没有出现在 existingPhotos 中的新图片
            List<BoxVideosReceiver> newVideos = videoList.stream()
                    .filter(photo -> !existingVideoUrls.contains(photo.getImgUrl())) // 过滤出新视频
                    .collect(Collectors.toList());
            if (!newVideos.isEmpty()) {
                // 插入新的视频记录
                addVideos(flightTask, newVideos);
                // 更新任务的视频数量
                flightTask.setVideoCount(flightTask.getVideoCount() + newVideos.size());
                ardFlightTaskMapper.updateArdFlightTask(flightTask);
            }
        }
    }
 
    private void addPhotos(ArdFlightTask flightTask, List<BoxPhotosReceiver> photoList) {
        log.warn("开始添加图片");
        for (BoxPhotosReceiver photo : photoList) {
            ArdMediaPic ardMedia = ArdMediaPic.builder()
                    .id(photo.getPhotoId())
                    .taskId(flightTask.getTaskId())
                    .fileSize(photo.getFileSize())
                    .fileMd5(photo.getFileMd5())
                    .sourceUrl(photo.getImgUrl())
                    .longitude(photo.getLng())
                    .latitude(photo.getLat())
                    .height(photo.getHeight())
                    .captureTime(DateUtils.parseDate(photo.getTimeStamp()))
                    .build();
            ardMedia.setCreateTime(new Date());
            ardMediaPicMapper.insertPic(ardMedia);
        }
    }
 
    private void addVideos(ArdFlightTask flightTask, List<BoxVideosReceiver> videoList) {
        for (BoxVideosReceiver video : videoList) {
            ArdMediaVideo ardMedia = ArdMediaVideo.builder()
                    .id(video.getId())
                    .name(video.getName())
                    .taskId(flightTask.getTaskId())
//                    .fileSize(getFileSizeByUrl(video.getImgUrl()))
                    .sourceUrl(video.getImgUrl())
                    .startTime(DateUtils.parseDate(video.getStartTime()))
                    .endTime(DateUtils.parseDate(video.getEndTime()))
                    .spanSeconds(Integer.parseInt(video.getSpanSeconds()))
                    .build();
            ardMedia.setCreateTime(new Date());
            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); // 使用自定义线程池
        }
    }
 
}