|
@@ -29,8 +29,10 @@ public class CameraUtil {
|
|
|
|
|
|
private static String url;
|
|
|
private static String historyUrl;
|
|
|
- private static String ffmpegPath;
|
|
|
+ private static String ffmpegPath = "C:\\Users\\Administrator.DESKTOP-0NUUTMM\\Downloads\\ffmpeg-5.1.2-essentials_build\\ffmpeg-5.1.2-essentials_build\\bin\\ffmpeg.exe";
|
|
|
private static String mappingUrl;
|
|
|
+ private static String flvPath;
|
|
|
+ private static String filePath;
|
|
|
@Autowired
|
|
|
private CaneraConfig caneraConfig;
|
|
|
|
|
@@ -45,13 +47,20 @@ public class CameraUtil {
|
|
|
*/
|
|
|
public static String historyPlay(List<String> fromVideoFileList, String ph) throws IOException {
|
|
|
// 视频服务映射路径
|
|
|
- String NewfilePath = BaseConfig.getProfile() + ph;
|
|
|
+ String NewfilePath = flvPath + ph;
|
|
|
log.info("NewfilePath:{}", NewfilePath);
|
|
|
log.info("fromVideoFileList:{}", fromVideoFileList);
|
|
|
convetor(fromVideoFileList, NewfilePath);
|
|
|
- return "/profile" + ph;
|
|
|
+ return mappingUrl + "record/flv/hazard/" + ph;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 实时播放的拼接流
|
|
|
+ *
|
|
|
+ * @param cameraCode 相机编码
|
|
|
+ * @param channel 相机通道
|
|
|
+ * @return
|
|
|
+ */
|
|
|
public static String getPlayFlv(String cameraCode, String channel) {
|
|
|
return historyUrl + "/hdl/" + channel + "/" + cameraCode + ".flv";
|
|
|
}
|
|
@@ -72,7 +81,22 @@ public class CameraUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public static List<String> historyPlayList(String channel, Date startTm, Date endTm) {
|
|
|
+ public static String historyPlayListStr(String channel, Date startTm, Date endTm) {
|
|
|
+ List<String> list = historyPlayList(channel, startTm, endTm, filePath);
|
|
|
+ String ph = "flv/" + DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS, new Date())
|
|
|
+ + "-" + channel + "-"
|
|
|
+ + UUID.randomUUID().toString() + ".flv";
|
|
|
+ try {
|
|
|
+ return historyPlay(list, ph);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static List<String> historyPlayList(String channel, Date startTm, Date endTm, String mappingUrl) {
|
|
|
if (StringUtils.isBlank(channel)
|
|
|
|| ObjectUtils.isEmpty(startTm)
|
|
|
|| ObjectUtils.isEmpty(endTm)) {
|
|
@@ -84,9 +108,7 @@ public class CameraUtil {
|
|
|
// 调用视频服务返回参数
|
|
|
String startTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, startTm);
|
|
|
String endTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, endTm);
|
|
|
- String ph = "/flv/" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, new Date())
|
|
|
- + "/" + channel + "/" + startTime + "/" + endTime + "/"
|
|
|
- + UUID.randomUUID().toString() + ".flv";
|
|
|
+
|
|
|
String param = "channel=" + channel + "&startTime=" + startTime + "&endTime=" + endTime;
|
|
|
String s = HttpUtils.sendGet(historyUrl + "/api/record/flv/list", param);
|
|
|
// 视频拼接
|
|
@@ -104,19 +126,23 @@ public class CameraUtil {
|
|
|
String s4 = s2[0] + s2[2];
|
|
|
Date sdate = DateUtils.dateTime(DateUtils.YYYYMMDDHHMMSS, s3);
|
|
|
Date edate = DateUtils.dateTime(DateUtils.YYYYMMDDHHMMSS, s4);
|
|
|
- /* s | e | */
|
|
|
- if (startTm.compareTo(sdate) <= 0 && endTm.compareTo(edate) > 0) {
|
|
|
+ /* sdate |startTm| edate |endTm| */
|
|
|
+ if (startTm.compareTo(sdate) >= 0
|
|
|
+ && startTm.compareTo(edate) <= 0
|
|
|
+ && endTm.compareTo(edate) >= 0) {
|
|
|
m.put(sdate, mappingUrl + path.toString());
|
|
|
- /* | s e | */
|
|
|
- } else if (startTm.compareTo(sdate) >= 0 && endTm.compareTo(edate) <= 0) {
|
|
|
+ /* |startTm| sdate edate |endTm| */
|
|
|
+ } else if (startTm.compareTo(sdate) <= 0 && endTm.compareTo(edate) >= 0) {
|
|
|
m.put(sdate, mappingUrl + path.toString());
|
|
|
|
|
|
- /* | s | e */
|
|
|
- } else if (startTm.compareTo(sdate) >= 0 && endTm.compareTo(edate) >= 0) {
|
|
|
+ /* |startTm| sdate |endTm| edate */
|
|
|
+ } else if (startTm.compareTo(sdate) <= 0
|
|
|
+ && endTm.compareTo(sdate) >= 0
|
|
|
+ && endTm.compareTo(edate) <= 0) {
|
|
|
m.put(sdate, mappingUrl + path.toString());
|
|
|
|
|
|
- /* s| | e */
|
|
|
- } else if (startTm.compareTo(sdate) <= 0 && endTm.compareTo(edate) >= 0) {
|
|
|
+ /* sdate |startTm| |endTm| edate */
|
|
|
+ } else if (startTm.compareTo(sdate) >= 0 && endTm.compareTo(edate) <= 0) {
|
|
|
m.put(sdate, mappingUrl + path.toString());
|
|
|
}
|
|
|
}
|
|
@@ -130,23 +156,10 @@ public class CameraUtil {
|
|
|
ls.add(m.get(d));
|
|
|
});
|
|
|
return ls;
|
|
|
-// try {
|
|
|
-// return historyPlay(ls, ph);
|
|
|
-// } catch (IOException e) {
|
|
|
-// e.printStackTrace();
|
|
|
-// }
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- @PostConstruct
|
|
|
- public void init() {
|
|
|
- url = caneraConfig.getUrl();
|
|
|
- mappingUrl = caneraConfig.getMappingUrl();
|
|
|
- historyUrl = caneraConfig.getHistoryUrl();
|
|
|
- ffmpegPath = caneraConfig.getFfmpegPath();
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* * 参数:
|
|
|
* **List<String> fromVideoFileList 需要合并的多视频url地址以List存放**
|
|
@@ -156,100 +169,114 @@ public class CameraUtil {
|
|
|
public static void convetor(List<String> fromVideoFileList,
|
|
|
String NewfilePath) throws IOException {
|
|
|
|
|
|
- new Thread(
|
|
|
- () -> {
|
|
|
- try {
|
|
|
- List<String> voidTS = new ArrayList<>();
|
|
|
- Process process = null;
|
|
|
- ProcessBuilder builder = null;
|
|
|
- List<String> command = null;
|
|
|
- for (int i = 0; i < fromVideoFileList.size(); i++) {
|
|
|
- String fromVideoFile = fromVideoFileList.get(i);
|
|
|
- command = new ArrayList<String>();
|
|
|
- command.add(ffmpegPath);
|
|
|
- command.add("-y");
|
|
|
- command.add("-i");
|
|
|
- command.add(fromVideoFile);
|
|
|
- command.add("-vcodec");
|
|
|
- command.add("copy");
|
|
|
- command.add("-bsf:v");
|
|
|
- command.add("h264_mp4toannexb");
|
|
|
- command.add("-f");
|
|
|
- command.add("mpegts");
|
|
|
- command.add(fromVideoFile.substring(0,
|
|
|
- fromVideoFile.lastIndexOf(".")) + ".ts");
|
|
|
- builder = new ProcessBuilder(command);
|
|
|
- voidTS.add(fromVideoFile.substring(0,
|
|
|
- fromVideoFile.lastIndexOf("."))
|
|
|
- + ".ts");
|
|
|
- try {
|
|
|
- process = builder.start();
|
|
|
- InputStream errorStream = process
|
|
|
- .getErrorStream();
|
|
|
- InputStreamReader inputStreamReader = new InputStreamReader(
|
|
|
- errorStream);
|
|
|
- BufferedReader br = new BufferedReader(
|
|
|
- inputStreamReader);
|
|
|
- String line = "";
|
|
|
- StringBuffer sb = new StringBuffer();
|
|
|
- while ((line = br.readLine()) != null) {
|
|
|
- sb.append(line);
|
|
|
- }
|
|
|
- String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
|
|
|
- Pattern pattern = Pattern
|
|
|
- .compile(regexDuration);
|
|
|
- Matcher m = pattern.matcher(sb.toString());
|
|
|
- System.out.println(sb.toString());
|
|
|
- br.close();
|
|
|
- inputStreamReader.close();
|
|
|
- errorStream.close();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- List<String> dos = new ArrayList<>();
|
|
|
- StringBuffer tsPath = new StringBuffer();
|
|
|
- tsPath.append(ffmpegPath);
|
|
|
- tsPath.append(" -i ");
|
|
|
- tsPath.append("concat:");
|
|
|
- for (int t = 0; t < voidTS.size(); t++) {
|
|
|
- if (t != voidTS.size() - 1) {
|
|
|
- tsPath.append(voidTS.get(t) + "|");
|
|
|
- } else {
|
|
|
- tsPath.append(voidTS.get(t));
|
|
|
- }
|
|
|
- }
|
|
|
- tsPath.append(" -vcodec ");
|
|
|
- tsPath.append(" copy ");
|
|
|
- tsPath.append(" -bsf:a ");
|
|
|
- tsPath.append(" aac_adtstoasc ");
|
|
|
- tsPath.append(" -movflags ");
|
|
|
- tsPath.append(" +faststart ");
|
|
|
- tsPath.append(NewfilePath);
|
|
|
- Process pr = Runtime.getRuntime().exec(
|
|
|
- tsPath.toString());
|
|
|
- process.getInputStream();
|
|
|
- pr.getOutputStream().close();
|
|
|
- pr.getInputStream().close();
|
|
|
- pr.getErrorStream().close();
|
|
|
- try {
|
|
|
- pr.waitFor();
|
|
|
- Thread.sleep(1000);
|
|
|
- pr.destroy();
|
|
|
- } catch (InterruptedException e) {
|
|
|
- // TODO Auto-generated catch block
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- //删除生成的ts文件
|
|
|
- for (String filePath : voidTS) {
|
|
|
- File file = new File(filePath);
|
|
|
- file.delete();
|
|
|
- pr.destroy();
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+
|
|
|
+ try {
|
|
|
+ List<String> voidTS = new ArrayList<>();
|
|
|
+ Process process = null;
|
|
|
+ ProcessBuilder builder = null;
|
|
|
+ List<String> command = null;
|
|
|
+ for (int i = 0; i < fromVideoFileList.size(); i++) {
|
|
|
+ String fromVideoFile = fromVideoFileList.get(i);
|
|
|
+ command = new ArrayList<String>();
|
|
|
+ command.add(ffmpegPath);
|
|
|
+ command.add("-y");
|
|
|
+ command.add("-i");
|
|
|
+ command.add(fromVideoFile);
|
|
|
+ command.add("-vcodec");
|
|
|
+ command.add("copy");
|
|
|
+ command.add("-bsf:v");
|
|
|
+ command.add("h264_mp4toannexb");
|
|
|
+ command.add("-f");
|
|
|
+ command.add("mpegts");
|
|
|
+ command.add(fromVideoFile.substring(0,
|
|
|
+ fromVideoFile.lastIndexOf(".")) + ".ts");
|
|
|
+ builder = new ProcessBuilder(command);
|
|
|
+ voidTS.add(fromVideoFile.substring(0,
|
|
|
+ fromVideoFile.lastIndexOf("."))
|
|
|
+ + ".ts");
|
|
|
+ try {
|
|
|
+ process = builder.start();
|
|
|
+ InputStream errorStream = process
|
|
|
+ .getErrorStream();
|
|
|
+ InputStreamReader inputStreamReader = new InputStreamReader(
|
|
|
+ errorStream);
|
|
|
+ BufferedReader br = new BufferedReader(
|
|
|
+ inputStreamReader);
|
|
|
+ String line = "";
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ while ((line = br.readLine()) != null) {
|
|
|
+ sb.append(line);
|
|
|
}
|
|
|
- }).start();
|
|
|
+ String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
|
|
|
+ Pattern pattern = Pattern
|
|
|
+ .compile(regexDuration);
|
|
|
+ Matcher m = pattern.matcher(sb.toString());
|
|
|
+ System.out.println(sb.toString());
|
|
|
+ br.close();
|
|
|
+ inputStreamReader.close();
|
|
|
+ errorStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<String> dos = new ArrayList<>();
|
|
|
+ StringBuffer tsPath = new StringBuffer();
|
|
|
+ tsPath.append(ffmpegPath);
|
|
|
+ tsPath.append(" -i ");
|
|
|
+ tsPath.append("concat:");
|
|
|
+ for (int t = 0; t < voidTS.size(); t++) {
|
|
|
+ if (t != voidTS.size() - 1) {
|
|
|
+ tsPath.append(voidTS.get(t) + "|");
|
|
|
+ } else {
|
|
|
+ tsPath.append(voidTS.get(t));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ tsPath.append(" -vcodec ");
|
|
|
+ tsPath.append(" copy ");
|
|
|
+ tsPath.append(" -bsf:a ");
|
|
|
+ tsPath.append(" aac_adtstoasc ");
|
|
|
+ tsPath.append(" -movflags ");
|
|
|
+ tsPath.append(" +faststart ");
|
|
|
+ tsPath.append(NewfilePath);
|
|
|
+ Process pr = Runtime.getRuntime().exec(
|
|
|
+ tsPath.toString());
|
|
|
+ process.getInputStream();
|
|
|
+ pr.getOutputStream().close();
|
|
|
+ pr.getInputStream().close();
|
|
|
+ pr.getErrorStream().close();
|
|
|
+ try {
|
|
|
+ pr.waitFor();
|
|
|
+ Thread.sleep(1000);
|
|
|
+ pr.destroy();
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ //删除生成的ts文件
|
|
|
+ for (String filePath : voidTS) {
|
|
|
+ File file = new File(filePath);
|
|
|
+ file.delete();
|
|
|
+ pr.destroy();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
+
|
|
|
+ List<String> fromVideoFileList = new ArrayList();
|
|
|
+ String p = "C:\\Users\\Administrator.DESKTOP-0NUUTMM\\Desktop\\work\\106\\流媒体\\";
|
|
|
+ String newfilePath = p + "hb.flv";
|
|
|
+ fromVideoFileList.add(p + "20230303_174931_175031-d6d56396-b9a8-11ed-aeb9-00163e06a5f3.flv");
|
|
|
+ fromVideoFileList.add(p + "20230303_175032_175132-fb32b9fe-b9a8-11ed-aeb9-00163e06a5f3.flv");
|
|
|
+ fromVideoFileList.add(p + "20230303_175133_175233-1f97af04-b9a9-11ed-aeb9-00163e06a5f3.flv");
|
|
|
+ fromVideoFileList.add(p + "20230303_175234_175334-43f4daf1-b9a9-11ed-aeb9-00163e06a5f3.flv");
|
|
|
+ fromVideoFileList.add(p + "m.flv");
|
|
|
+ fromVideoFileList.add(p + "m33.flv");
|
|
|
+ fromVideoFileList.add(p + "mv.flv");
|
|
|
+ fromVideoFileList.add(p + "mvido.flv");
|
|
|
+ convetor(fromVideoFileList, newfilePath);
|
|
|
}
|
|
|
|
|
|
//工具类
|
|
@@ -266,40 +293,13 @@ public class CameraUtil {
|
|
|
System.out.println(s);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- /**
|
|
|
- * 合并多个视频文件
|
|
|
- */
|
|
|
- public static void mergeFile(List<String> fromVideoFileList, String NewfilePath) {
|
|
|
- // 合并命令
|
|
|
- String commit = "$0 -f concat -safe 0 -i $1 -c copy $2";
|
|
|
-
|
|
|
- String str = commit.replace("$0", ffmpegPath);
|
|
|
- if (!ObjectUtils.isEmpty(fromVideoFileList)) {
|
|
|
- for (String s : fromVideoFileList) {
|
|
|
- str = str.replace("$1", s);
|
|
|
- }
|
|
|
- }
|
|
|
- str = str.replace("$2", NewfilePath);
|
|
|
- System.out.println(str);
|
|
|
- Runtime runtime = Runtime.getRuntime();
|
|
|
- try {
|
|
|
- Process proce = runtime.exec(str);
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static void main(String[] args) throws IOException {
|
|
|
-
|
|
|
- List<String> fromVideoFileList = new ArrayList();
|
|
|
- String p = "C:\\Users\\Administrator.DESKTOP-0NUUTMM\\Desktop\\work\\106\\流媒体\\";
|
|
|
- String NewfilePath = p + "mergevideo.flv";
|
|
|
- fromVideoFileList.add(p + "20230303_174931_175031-d6d56396-b9a8-11ed-aeb9-00163e06a5f3.flv");
|
|
|
- fromVideoFileList.add(p + "20230303_175032_175132-fb32b9fe-b9a8-11ed-aeb9-00163e06a5f3.flv");
|
|
|
- fromVideoFileList.add(p + "20230303_175133_175233-1f97af04-b9a9-11ed-aeb9-00163e06a5f3.flv");
|
|
|
- fromVideoFileList.add(p + "20230303_175234_175334-43f4daf1-b9a9-11ed-aeb9-00163e06a5f3.flv");
|
|
|
- convetor(fromVideoFileList, NewfilePath);
|
|
|
-
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ url = caneraConfig.getUrl();
|
|
|
+ mappingUrl = caneraConfig.getMappingUrl();
|
|
|
+ historyUrl = caneraConfig.getHistoryUrl();
|
|
|
+ ffmpegPath = caneraConfig.getFfmpegPath();
|
|
|
+ flvPath = caneraConfig.getFlvPath();
|
|
|
+ filePath = caneraConfig.getFilePath();
|
|
|
}
|
|
|
}
|