* list長度爲8
* 序列1: 提取出播放時間
* 序列2: 開始時間
* 序列3: bitrate 碼率 單位 kb
* 序列4: 編碼格式
* 序列5: 視頻格式
* 序列6: 分辨率
* 序列7: 音頻編碼
* 序列8: 音頻採樣頻率
* @param oldfilePath 原文件
* @return
*/
private static List<String> getVideoInfo(String oldfilePath) {
List<String> list = new ArrayList<String>();
if (!checkfile(oldfilePath)) {
System.out.println(oldfilePath + " is not file");
return null;
}
List<String> commend=new java.util.ArrayList<String>();
commend.add(ffmpegPath);
commend.add("-i");
commend.add(oldfilePath);
System.out.println(commend.toString());
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.redirectErrorStream(true);
Process p= builder.start();
//1. start
BufferedReader buf = null; // 保存ffmpeg的輸出結果流
String line = null;
//read the standard output
buf = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer sb= new StringBuffer();
while ((line = buf.readLine()) != null) {
System.out.println(line);
sb.append(line);
continue;
}
int ret = p.waitFor();//這裏線程阻塞,將等待外部轉換進程運行成功運行結束後,才往下執行
//1. end
String result = sb.toString();//得到視頻流
//通過視頻流獲取視頻信息
PatternCompiler compiler =new Perl5Compiler();
String regexDuration ="Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
String regexVideo ="Video: (.*?), (.*?), (.*?)[,\\s]";
String regexAudio ="Audio: (\\w*), (\\d*) Hz";
Pattern patternDuration = compiler.compile(regexDuration,Perl5Compiler.CASE_INSENSITIVE_MASK);
PatternMatcher matcherDuration = new Perl5Matcher();
if(matcherDuration.contains(result, patternDuration)){
MatchResult re = matcherDuration.getMatch();
list.add(re.group(1));//提取出播放時間
list.add(re.group(2));//開始時間
list.add(re.group(3));//bitrate 碼率 單位 kb
}
Pattern patternVideo = compiler.compile(regexVideo,Perl5Compiler.CASE_INSENSITIVE_MASK);
PatternMatcher matcherVideo = new Perl5Matcher();
if(matcherVideo.contains(result, patternVideo)){
MatchResult re = matcherVideo.getMatch();
list.add(re.group(1));//編碼格式
list.add(re.group(2));//視頻格式
list.add(re.group(3));//分辨率
}
Pattern patternAudio = compiler.compile(regexAudio,Perl5Compiler.CASE_INSENSITIVE_MASK);
PatternMatcher matcherAudio = new Perl5Matcher();
if(matcherAudio.contains(result, patternAudio)){
MatchResult re = matcherAudio.getMatch();
list.add(re.group(1));//音頻編碼
list.add(re.group(2));//音頻採樣頻率
}
} catch (Exception e) {
return null;
}
return list;
}