ffmpeg获取视频时长

/**
* 获取视频时长
*
* @param fileId
* @return
*/
private Integer getFileDuration(String fileId) {
String ffmpeg_path = "D:\soft\ffmpeg.exe";
String video_path = "D:\3708579e857a68b7654eb08df19214f6.mp4";
List<String> commands = new java.util.ArrayList<>();
commands.add(ffmpegPath);
commands.add("-i");
commands.add(video_path);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commands);
final Process p = builder.start();

//从输入流中读取视频信息
BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();

//从视频信息中解析时长
String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s";
Pattern pattern = Pattern.compile(regexDuration);
Matcher m = pattern.matcher(sb.toString());
if (m.find()) {
int time = getTimelen(m.group(1));
logger.info(video_path+",视频时长:"+time+", 开始时间:"+m.group(2)+",比特率:"+m.group(3)+"kb/s");
return time;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}

//格式:"00:00:10.68"
private static int getTimelen(String timelen){
int min=0;
String strs[] = timelen.split(":");
if (strs[0].compareTo("0") > 0) {
min+=Integer.valueOf(strs[0])*60*60;//秒
}
if(strs[1].compareTo("0")>0){
min+=Integer.valueOf(strs[1])*60;
}
if(strs[2].compareTo("0")>0){
min+=Math.round(Float.valueOf(strs[2]));
}
return min;
}
原文地址:https://www.cnblogs.com/ssk913/p/14435644.html