本文共 3269 字,大约阅读时间需要 10 分钟。
packagecom.aw.utils;importorg.apache.commons.lang3.StringUtils;import org.apache.oro.text.regex.*;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.util.HashMap;importjava.util.List;importjava.util.Map;/***@authorhy
* @date 2019/11/7 14:05*/
public classFileParseUtil {public static final Logger LOGGER = LoggerFactory.getLogger(FileParseUtil.class);/*** 提取音频、视频编码等信息
*
*@paramfilePath
*@return
*/
public static MapgetEncodingFormat(String filePath) {
String processFLVResult=processFLV(filePath);
Map retMap= newHashMap();if(StringUtils.isNotBlank(processFLVResult)) {
PatternCompiler compiler= newPerl5Compiler();try{
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= newPerl5Matcher();if(matcherDuration.contains(processFLVResult, patternDuration)) {
MatchResult re=matcherDuration.getMatch();
retMap.put("提取出播放时间", re.group(1));
retMap.put("开始时间", re.group(2));
retMap.put("bitrate 码率 单位 kb", re.group(3));
System.out.println("提取出播放时间 ===" + re.group(1));
System.out.println("开始时间 =====" + re.group(2));
System.out.println("bitrate 码率 单位 kb==" + re.group(3));
}
Pattern patternVideo=compiler.compile(regexVideo, Perl5Compiler.CASE_INSENSITIVE_MASK);
PatternMatcher matcherVideo= newPerl5Matcher();if(matcherVideo.contains(processFLVResult, patternVideo)) {
MatchResult re=matcherVideo.getMatch();
retMap.put("编码格式", re.group(1));
retMap.put("视频格式", re.group(2));
retMap.put("分辨率", re.group(3));
System.out.println("编码格式 ===" + re.group(1));
System.out.println("视频格式 ===" + re.group(2));
System.out.println(" 分辨率 == =" + re.group(3));
}
Pattern patternAudio=compiler.compile(regexAudio, Perl5Compiler.CASE_INSENSITIVE_MASK);
PatternMatcher matcherAudio= newPerl5Matcher();if(matcherAudio.contains(processFLVResult, patternAudio)) {
MatchResult re=matcherAudio.getMatch();
retMap.put("音频编码", re.group(1));
retMap.put("音频采样频率", re.group(2));
System.out.println("音频编码 ===" + re.group(1));
System.out.println("音频采样频率 ===" + re.group(2));
}
}catch(MalformedPatternException e) {
e.printStackTrace();
}
}returnretMap;
}//ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
private staticString processFLV(String inputPath) {
List commend = new java.util.ArrayList();
commend.add("D:\\aisino\\ffmpeg-20191105-c54268c-win64-static\\bin\\ffmpeg");//可以设置环境变量从而省去这行
commend.add("ffmpeg");
commend.add("-i");
commend.add(inputPath);try{
ProcessBuilder builder= newProcessBuilder();
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(newInputStreamReader(p.getInputStream()));
StringBuffer sb= newStringBuffer();while ((line = buf.readLine()) != null) {
System.out.println(line);
sb.append(line);continue;
}int ret = p.waitFor();//这里线程阻塞,将等待外部转换进程运行成功运行结束后,才往下执行//1. end
returnsb.toString();
}catch(Exception e) {
LOGGER.error("-- processFLV error, message is {}", e);return null;
}
}
}
转载地址:http://mgeav.baihongyu.com/