shikeying
2022-09-24 7b3b249a7f2320f97e21e94e26a65f4b4ead0b6e
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
package com.iplatform.recvideo.util;
 
import com.iplatform.recvideo.Constants;
import com.iplatform.recvideo.VideoFolderInfo;
import com.walker.infrastructure.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
public class VideoFileUtils {
 
    protected static final transient Logger logger = LoggerFactory.getLogger(VideoFileUtils.class);
 
    /**
     * 拼接视频批量(存储)文件夹路径,如: /opt/ai/video/20220921,其中'20220921'为批量号
     * @param videoDataFolder
     * @param batchId
     * @return
     */
    public static final String combineBatchPath(String videoDataFolder, String batchId){
        String batchFolderPath = null;
        if(videoDataFolder.endsWith(StringUtils.FOLDER_SEPARATOR)){
            batchFolderPath = videoDataFolder + batchId;
        } else {
            batchFolderPath = videoDataFolder + StringUtils.FOLDER_SEPARATOR + batchId;
        }
        return batchFolderPath;
    }
 
    public static final List<VideoFolderInfo> getBatchVideoFolderInfo(String videoDataFolder, String batchId){
//        String batchFolderPath = videoDataFolder + File.separator + batchId;
        String batchFolderPath = combineBatchPath(videoDataFolder, batchId);
//        if(videoDataFolder.endsWith(StringUtils.FOLDER_SEPARATOR)){
//            batchFolderPath = videoDataFolder + batchId;
//        } else {
//            batchFolderPath = videoDataFolder + StringUtils.FOLDER_SEPARATOR + batchId;
//        }
        File batchFolder = new File(batchFolderPath);
        if(!batchFolder.exists()){
            logger.error("视频文件夹不存在,无法获取图片集合信息。batchFolderPath = " + batchFolderPath);
            return null;
        }
        File[] files = batchFolder.listFiles();
        if(files == null || files.length == 0){
            logger.error("视频文件夹下没有任何图像文件夹: " + batchFolderPath);
            return null;
        }
 
        List<VideoFolderInfo> resultList = new ArrayList<>();
 
        VideoFolderInfo videoFolderInfo = null;
        for(File file : files){
            if(file.isFile()){
                continue;   // 视频文件忽略,只看文件夹
            }
            videoFolderInfo = new VideoFolderInfo(file.getName());
//            logger.debug("find folder = " + file.getName() + ", " + file.getAbsolutePath());
            acquireImagesInVideoFolder(file, videoFolderInfo);
            resultList.add(videoFolderInfo);
        }
        return resultList;
    }
 
    private static void acquireImagesInVideoFolder(File imageFolder, VideoFolderInfo videoFolderInfo){
//        ImageInfo imageInfo = null;
        File[] imageFiles = imageFolder.listFiles();
        for(File file : imageFiles){
            if(!file.isFile()){
                continue;
            }
            if(!file.getName().endsWith(Constants.IMAGE_SUFFIX)){
                logger.warn("图像后缀名不是指定格式'.jpg', name = " + file.getName());
                continue;
            }
//            imageInfo = new ImageInfo();
//            imageInfo.setVideoId(videoFolderInfo.getVideoId());
//            imageInfo.setImagePath(file.getAbsolutePath());
//            imageInfo.setImageName(file.getName());
            videoFolderInfo.addImageInfo(file.getAbsolutePath(), file.getName());
        }
    }
}