VideoFileFactory.java
6.63 KB
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
package top.panll.assist.service;
import net.bramp.ffmpeg.probe.FFmpegProbeResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.panll.assist.dto.VideoFile;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class VideoFileFactory {
private final static Logger logger = LoggerFactory.getLogger(VideoFileFactory.class);
public static VideoFile createFile(FFmpegExecUtils ffmpegExecUtils, File file){
if (!file.exists()) {
return null;
}
if (!file.isFile()){
return null;
}
if (!file.getName().endsWith(".mp4")){
return null;
}
if (file.isHidden()){
return null;
}
String date = file.getParentFile().getName();
if (file.getName().indexOf(":") > 0) {
// 格式为 HH:mm:ss-HH:mm:ss-时长
String[] split = file.getName().split("-");
if (split.length != 3) {
return null;
}
String startTimeStr = date + " " + split[0];
String endTimeStr = date + " " + split[1];
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
VideoFile videoFile = new VideoFile();
videoFile.setFile(file);
videoFile.setTargetFormat(false);
try {
Date startTimeDate = simpleDateFormat.parse(startTimeStr);
videoFile.setStartTime(startTimeDate);
Date endTimeDate = simpleDateFormat.parse(endTimeStr);
videoFile.setEndTime(endTimeDate);
videoFile.setDuration((endTimeDate.getTime() - startTimeDate.getTime()));
} catch (ParseException e) {
logger.error("[构建视频文件对象] 格式化时间失败, file:{}", file.getAbsolutePath(), e);
return null;
}
return videoFile;
}else if (getStrCountInStr(file.getName(), "-") == 3){
// 格式为zlm的录制格式 HH-mm-ss-序号
String startStr = file.getName().substring(0, file.getName().lastIndexOf("-"));
String startTimeStr = date + " " + startStr;
VideoFile videoFile = null;
try {
FFmpegProbeResult fFmpegProbeResult = ffmpegExecUtils.getFfprobe().probe(file.getAbsolutePath());
double duration = fFmpegProbeResult.getFormat().duration * 1000;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date startTimeDate = simpleDateFormat.parse(startTimeStr);
Date endTimeDate = new Date(startTimeDate.getTime() + new Double(duration).longValue());
videoFile = new VideoFile();
videoFile.setTargetFormat(false);
videoFile.setFile(file);
videoFile.setStartTime(startTimeDate);
videoFile.setEndTime(endTimeDate);
videoFile.setDuration((endTimeDate.getTime() - startTimeDate.getTime())/1000);
} catch (IOException e) {
logger.error("[构建视频文件对象] 获取视频时长失败, file:{}", file.getAbsolutePath(), e);
return null;
} catch (ParseException e) {
logger.error("[构建视频文件对象] 格式化时间失败, file:{}", file.getAbsolutePath(), e);
return null;
}
return videoFile;
}else if (getStrCountInStr(file.getName(), "-") == 2 && file.getName().length() == 10 ){
// 格式为zlm的录制格式 HH-mm-ss
String startTimeStr = date + " " + file.getName();
VideoFile videoFile = null;
try {
FFmpegProbeResult fFmpegProbeResult = ffmpegExecUtils.getFfprobe().probe(file.getAbsolutePath());
double duration = fFmpegProbeResult.getFormat().duration * 1000;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date startTimeDate = simpleDateFormat.parse(startTimeStr);
Date endTimeDate = new Date(startTimeDate.getTime() + new Double(duration).longValue());
videoFile = new VideoFile();
videoFile.setTargetFormat(false);
videoFile.setFile(file);
videoFile.setStartTime(startTimeDate);
videoFile.setEndTime(endTimeDate);
videoFile.setDuration((endTimeDate.getTime() - startTimeDate.getTime())/1000);
} catch (IOException e) {
logger.error("[构建视频文件对象] 获取视频时长失败, file:{}", file.getAbsolutePath(), e);
return null;
} catch (ParseException e) {
logger.warn("[构建视频文件对象] 格式化时间失败, file:{}", file.getAbsolutePath(), e);
return null;
}
return videoFile;
}else if (getStrCountInStr(file.getName(), "-") == 1 ){
// 格式为zlm的录制格式 HH-mm-ss
// 格式为 HH:mm:ss-HH:mm:ss-时长
String[] split = file.getName().split("-");
if (split.length != 2) {
return null;
}
String startTimeStr = date + " " + split[0];
String endTimeStr = date + " " + split[1];
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HHmmss");
VideoFile videoFile = new VideoFile();
videoFile.setTargetFormat(true);
videoFile.setFile(file);
try {
Date startTimeDate = simpleDateFormat.parse(startTimeStr);
videoFile.setStartTime(startTimeDate);
Date endTimeDate = simpleDateFormat.parse(endTimeStr);
videoFile.setEndTime(endTimeDate);
videoFile.setDuration((endTimeDate.getTime() - startTimeDate.getTime()));
} catch (ParseException e) {
logger.error("[构建视频文件对象] 格式化时间失败, file:{}", file.getAbsolutePath(), e);
return null;
}
return videoFile;
}else {
return null;
}
}
public static int getStrCountInStr(String sourceStr, String content) {
int index = sourceStr.indexOf(content);
if (index < 0) {
return 0;
}
int count = 1;
int lastIndex = sourceStr.lastIndexOf(content);
while (index != lastIndex) {
index = sourceStr.indexOf(content, index + 1);
count++;
}
return count;
}
}