nacos里面配置的ffmpegPath,如果不用這個(gè),也可以自己寫一個(gè)ffmpegPath的地址文章來源:http://www.zghlxwxcb.cn/news/detail-732034.html
@NacosValue(value = "${data.ffmpegPath}", autoRefreshed = true)
private String ffmpegPath;
代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-732034.html
public Float getVideoSeconds(String remoteVideoURL){
try {
// 定義遠(yuǎn)程視頻的URL
// 構(gòu)建FFmpeg命令
ProcessBuilder processBuilder = new ProcessBuilder(ffmpegPath, "-i",remoteVideoURL);
// 讀取FFmpeg的輸出信息
// 創(chuàng)建ProcessBuilder并執(zhí)行命令
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:")) {
// 獲取包含視頻時(shí)長信息的行
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());
// 計(jì)算總秒數(shù)
double totalSeconds = hours * 3600 + minutes * 60 + seconds;
durationInSeconds = String.valueOf(totalSeconds);
}
}
reader.close();
// 打印視頻時(shí)長
if (durationInSeconds != null) {
System.out.println("視頻時(shí)長:" + durationInSeconds);
return Float.valueOf(durationInSeconds);
} else {
System.out.println("無法獲取視頻時(shí)長。");
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
到了這里,關(guān)于java ffmpeg獲取視頻時(shí)長的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!