nacos里面配置的ffmpegPath,如果不用这个,也可以自己写一个ffmpegPath的地址文章来源:https://www.toymoban.com/news/detail-732034.html
@NacosValue(value = "${data.ffmpegPath}", autoRefreshed = true)
private String ffmpegPath;
代码文章来源地址https://www.toymoban.com/news/detail-732034.html
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;
}
}
到了这里,关于java ffmpeg获取视频时长的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!