疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

Java获取视频的信息


 

 
/**
* 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;
}