nacos里面配置的ffmpegPath,如果不用这个,也可以自己写一个ffmpegPath的地址
- @NacosValue(value = "${data.ffmpegPath}", autoRefreshed = true)
- private String ffmpegPath;
代码
- public Float getVideoSeconds(String remoteVideoURL){
- try {
- // 定义远程视频的URL
- // 构建FFmpeg命令
-
-
- ProcessBuilder processBuilder = new ProcessBuilder(ffmpegPath, "-i",remoteVideoURL);
-
-
-
- // 读取FFmpeg的输出信息
-
-
- // 创建ProcessBuilder并执行命令
-
- processBuilder.redirectErrorStream(true);
- Process process = processBuilder.start();
-
- // 读取FFmpeg命令输出
- BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
- String line;
-
- String durationInSeconds = null;
- while ((line = reader.readLine()) != null) {
- if (line.contains("Duration:")) {
- // 获取包含视频时长信息的行
- String durationLine = line.split("Duration:")[1].split(",")[0].trim();
- String[] durationParts = durationLine.split(":");
- int hours = Integer.parseInt(durationParts[0].trim());
- int minutes = Integer.parseInt(durationParts[1].trim());
- double seconds = Double.parseDouble(durationParts[2].trim());
-
- // 计算总秒数
- double totalSeconds = hours * 3600 + minutes * 60 + seconds;
- durationInSeconds = String.valueOf(totalSeconds);
- }
- }
-
- reader.close();
-
- // 打印视频时长
- if (durationInSeconds != null) {
- System.out.println("视频时长:" + durationInSeconds);
- return Float.valueOf(durationInSeconds);
- } else {
- System.out.println("无法获取视频时长。");
- return null;
- }
- } catch (IOException e) {
-
- e.printStackTrace();
- return null;
- }
- }