http://www.ffmpeg.org/download.htmlhtml
主要須要bin目錄下的ffmpeg可執行文件java
package com.aw.utils; import org.apache.commons.lang3.StringUtils; import org.apache.oro.text.regex.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author hy * @date 2019/11/7 14:05 */ public class FileParseUtil { public static final Logger LOGGER = LoggerFactory.getLogger(FileParseUtil.class); /** * 提取音頻、視頻編碼等信息 * * @param filePath * @return */ public static Map<String, String> getEncodingFormat(String filePath) { String processFLVResult = processFLV(filePath); Map retMap = new HashMap(); if (StringUtils.isNotBlank(processFLVResult)) { PatternCompiler compiler = new Perl5Compiler(); 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 = new Perl5Matcher(); 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 = new Perl5Matcher(); 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 = new Perl5Matcher(); 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(); } } return retMap; } // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) private static String processFLV(String inputPath) { List<String> commend = new java.util.ArrayList<String>(); commend.add("D:\\aisino\\ffmpeg-20191105-c54268c-win64-static\\bin\\ffmpeg");//能夠設置環境變量從而省去這行 commend.add("ffmpeg"); commend.add("-i"); commend.add(inputPath); 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 return sb.toString(); } catch (Exception e) { LOGGER.error("-- processFLV error, message is {}", e); return null; } } }