import java.io.*;importjava.util.regex.Matcher;importjava.util.regex.Pattern;/*** 获取视频的信息
* FFMPEG homepagehttp://ffmpeg.org/about.html
*/
public classVideoInfo
{//视频路径
privateString ffmpegApp;//视频时
private inthours;//视频分
private intminutes;//视频秒
private floatseconds;//视频width
private intwidth;//视频height
private intheigt;publicVideoInfo() {}publicVideoInfo(String ffmpegApp)
{this.ffmpegApp =ffmpegApp;
}publicString toString()
{return "time: " + hours + ":" + minutes + ":" + seconds + ", width = " + width + ", height= " +heigt;
}public void getInfo(String videoFilename) throwsIOException,
InterruptedException
{
String tmpFile= videoFilename + ".tmp.png";
ProcessBuilder processBuilder= new ProcessBuilder(ffmpegApp, "-y","-i", videoFilename, "-vframes", "1", "-ss", "0:0:0", "-an","-vcodec", "png", "-f", "rawvideo", "-s", "100*100", tmpFile);
Process process=processBuilder.start();
InputStream stderr=process.getErrorStream();
InputStreamReader isr= newInputStreamReader(stderr);
BufferedReader br= newBufferedReader(isr);
String line;//打印 sb,获取更多信息。 如 bitrate、width、heigt
StringBuffer sb = newStringBuffer();while ((line = br.readLine()) != null)
{
sb.append(line);
}newFile(tmpFile).delete();
System.out.println("video info:\n" +sb);
Pattern pattern= Pattern.compile("Duration: (.*?),");
Matcher matcher=pattern.matcher(sb);if(matcher.find())
{
String time= matcher.group(1);
calcTime(time);
}
pattern= Pattern.compile("w:\\d+ h:\\d+");
matcher=pattern.matcher(sb);if(matcher.find())
{
String wh=matcher.group();//w:100 h:100
String[] strs = wh.split("\\s+");if(strs != null && strs.length == 2)
{
width= Integer.parseInt(strs[0].split(":")[1]);
heigt= Integer.parseInt(strs[1].split(":")[1]);
}
}
process.waitFor();if(br != null)
br.close();if(isr != null)
isr.close();if(stderr != null)
stderr.close();
}private voidcalcTime(String timeStr)
{
String[] parts= timeStr.split(":");
hours= Integer.parseInt(parts[0]);
minutes= Integer.parseInt(parts[1]);
seconds= Float.parseFloat(parts[2]);
}publicString getFfmpegApp()
{returnffmpegApp;
}public voidsetFfmpegApp(String ffmpegApp)
{this.ffmpegApp =ffmpegApp;
}public intgetHours()
{returnhours;
}public void setHours(inthours)
{this.hours =hours;
}public intgetMinutes()
{returnminutes;
}public void setMinutes(intminutes)
{this.minutes =minutes;
}public floatgetSeconds()
{returnseconds;
}public void setSeconds(floatseconds)
{this.seconds =seconds;
}public intgetWidth()
{returnwidth;
}public void setWidth(intwidth)
{this.width =width;
}public intgetHeigt()
{returnheigt;
}public void setHeigt(intheigt)
{this.heigt =heigt;
}public static voidmain(String[] args)
{
VideoInfo videoInfo= new VideoInfo("D:\\ffmpeg\\bin\\ffmpeg.exe");try{
videoInfo.getInfo("f:/reyo/test.mkv");
System.out.println(videoInfo);
}catch(Exception e)
{
e.printStackTrace();
}
}
}