利用ffmpeg读取视频时长

百度了一下午,查找怎么用ffmpeg来获取视频时长,各种报错,各种不好使,心里窝火,google了一下,2分钟解决了,实在忍不住吐槽一下。下面是代码,很简单的几行代码:

package dealwithVideo;
import java.io.File;  
import java.util.ArrayList;  
import java.util.List;  
import java.util.Scanner;
import java.util.regex.Pattern;


public class ConvertVideo {  


// private final static String PATH = "F:\\ffmpeg\\1.mp4";  
//处理视频
public void dealVideo(String wholePath,String filename) {  
String videoPath = wholePath+filename;//视频路径+视频名称
if (!checkfile(videoPath)) {  //检查视频是否存在
System.out.println(videoPath + " is not file");  
return;  

if (process(wholePath,filename)) {  
System.out.println("截图完成");  
}  
}  
//检查视频是否存在
private static boolean checkfile(String path) {  
File file = new File(path);  
if (!file.isFile()) {  
return false;  
}  
return true;  
}  
//转码并截图
private static boolean process(String wholePath,String filename) {
int type = checkContentType(filename); //查看视频类型
if(type==0){//如果不是flv,则转码为flv并获取缩略图
return processFLV(wholePath,filename);// 将其他格式转为flv 
}
else{
return getPic(wholePath,filename);//如果是flv格式则直接获取缩略图
}

}  
//查看视频类型 type=0为其他类型,9为flv
private static int checkContentType(String filename) {  
String type = filename.substring(filename.length()-3,filename.length());
System.out.println(type+"------------------------");
// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)  
if (type.equals("avi")) {  
return 0;  
} else if (type.equals("mpg")) {  
return 0;  
} else if (type.equals("wmv")) {  
return 0;  
} else if (type.equals("3gp")) {  
return 0;  
} else if (type.equals("mov")) {  
return 0;  
} else if (type.equals("mp4")) {  
return 0;  
} else if (type.equals("asf")) {  
return 0;  
} else if (type.equals("asx")) {  
return 0;  
}   
return 9;  
}




private static boolean getPic(String wholePath,String filename){
String filenameBef = filename.substring(0, filename.length()-4);
//截取缩略图
List commend1 = new ArrayList();  
commend1.add("F:\\ffmpeg\\ffmpeg.exe");  
commend1.add("-i");  
commend1.add(wholePath+filename);  
commend1.add("-y");
// commend1.add("-f");  
commend1.add("image2");  
commend1.add("-ss");  
commend1.add("8");  
commend1.add("-s");  
commend1.add("600x500");  
commend1.add("f:\\ffmpeg\\"+filenameBef+".jpg");  
System.out.println(commend1);

try {  
Runtime runtime = Runtime.getRuntime();  
       Process proce = null;
String ffmpegStr = "F:\\ffmpeg\\ffmpeg.exe -i "+wholePath+filename+"  -f image2 " +
"-ss  5 -s 600*500 " +
  "f:\\ffmpeg\\"+filenameBef+".jpg";
proce = runtime.exec(ffmpegStr);
// ProcessBuilder builder1 = new ProcessBuilder(commend1);  
// builder1.command(commend1);  
// builder1.start();  
// System.out.println(ffmpegStr);
//获取视频总时长
Scanner sc = new Scanner(proce.getErrorStream());


       // Find duration
       Pattern durPattern = Pattern.compile("(?<=Duration: )[^,]*");
       String dur = sc.findWithinHorizon(durPattern, 0);
       if (dur == null)
         throw new RuntimeException("Could not parse duration.");
       String[] hms = dur.split(":");
       double totalSecs = Integer.parseInt(hms[0]) * 3600
                        + Integer.parseInt(hms[1]) *   60
                        + Double.parseDouble(hms[2]);
       System.out.println("Total duration: " + totalSecs + " seconds.");


return true;  
} catch (Exception e) {  
e.printStackTrace();  
return false;  
}  

}


// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)  
private static boolean processFLV(String wholePath,String filename) {  


if (!checkfile(wholePath+filename)) {  
System.out.println(wholePath+filename+ " is not file");  
return false;  
}  
String filenameBef = filename.substring(0, filename.length()-4);
// 文件命名  
// Calendar c = Calendar.getInstance();  
// String savename = String.valueOf(c.getTimeInMillis())+ Math.round(Math.random() * 100000);  
//转化视频
List commend = new ArrayList();  
commend.add("F:\\ffmpeg\\ffmpeg.exe");  
commend.add("-i");  
commend.add(wholePath+filename);  
commend.add("-ab");  
commend.add("56");  
commend.add("-ar");  
commend.add("22050");  
commend.add("-qscale");  
commend.add("8");  
commend.add("-r");  
commend.add("15");  
commend.add("-s");  
commend.add("600x500");  
commend.add("f:\\ffmpeg\\"+filenameBef+".flv"); 
try {  
Runtime runtime = Runtime.getRuntime();  
            Process proce = null;  
            String cmd = "";  
            String cut = "     f:\\ffmpeg\\ffmpeg.exe   -i   "  
                    +   wholePath+filename
                    + "      -f   image2   -ss   5   -s   600x480   f:\\ffmpeg\\"  
                    + filenameBef+".jpg";  
            String cutCmd = cmd + cut;  
            proce = runtime.exec(cutCmd);  
            ProcessBuilder builder = new ProcessBuilder(commend);  
             builder.command(commend);  
            builder.start();  
return true;  
} catch (Exception e) {  
e.printStackTrace();  
return false;  
}  

}  
}  

你可能感兴趣的:(ffmpeg,ffmpeg,duration视频时长,java)