直接上代码:
public abstract class FFmpegUtils { FFmpegUtils ffmpegUtils; int timeLengthSec = 1; String timeLength = ""; Pattern pattern = Pattern.compile("Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s"); String frameRegexDuration = "size=([\\s\\S]*) time=(.*?) bitrate=([\\s\\S]*) speed=(.*?)x"; String videoframeRegexDuration = "frame=([\\s,\\d]*) fps=(.*?) q=(.*?) size=([\\s\\S]*) time=(.*?) bitrate=([\\s\\S]*) speed=(.*?)x"; Pattern framePattern = Pattern.compile(frameRegexDuration); public static void main(String[] args){ String target = ""; /* try { target = extractAsyn("D:\\ffmpeg4.2\\bin\\ffmpeg.exe", "-y -f image2 -ss 1 -t 0.001 -s 640x480", "E:\\迅雷下载\\电影\\test.avi", "E:\\迅雷下载\\电影\\test.avi.jpg"); System.out.println(target); } catch (Throwable e) { System.err.println(e.getMessage()); } */ try { new FFmpegUtils() { @Override public void dealLine(String line) { System.out.println(line); if(timeLength == null || timeLength.equals("")) { Matcher m = pattern.matcher(line.trim()); if (m.find()) { timeLength = m.group(1); if(timeLength!=null){ timeLengthSec = FFVideoUtil.getTimelen(timeLength); } System.out.println(timeLength+"||"+timeLengthSec); } } //获取视频信息 Matcher matcher = framePattern.matcher(line); if(matcher.find()){ try { String execTimeStr = matcher.group(2); int execTimeInt = FFVideoUtil.getTimelen(execTimeStr); double devnum = FFBigDecimalUtil.div(execTimeInt,timeLengthSec,5); double progressDouble = FFBigDecimalUtil.mul(devnum,100); System.out.println("execTimeInt:"+execTimeInt+"&,devnum:"+devnum+"&,progressDouble:"+progressDouble); } catch (IllegalAccessException e) { System.err.println("获取输出流异常:"+e.getMessage()); } } } @Override public void dealStream(Process process) { if (process == null) { return; } // 处理InputStream的线程 new Thread() { @Override public void run() { BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = null; try { while ((line = in.readLine()) != null) { //logger.info("output: " + line); dealLine(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } }.start(); // 处理ErrorStream的线程 new Thread() { @Override public void run() { BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream())); String line = null; try { while ((line = err.readLine()) != null) { dealLine(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { err.close(); } catch (IOException e) { e.printStackTrace(); } } } }.start(); } }.processVideoSync("D:\\ffmpeg4.2\\bin\\ffmpeg.exe", " -f|mp3", "E:\\迅雷下载\\电影\\test.avi", "E:\\迅雷下载\\电影\\test.avi.mp3"); System.out.println(target); } catch (Throwable e) { System.err.println(e.getMessage()); } } //异步 适合抽帧等快速的操作 public static String extractAsyn( String ffmpegPath,String cmdParam, String sourceFile,String targetFile) throws Throwable { Runtime runtime = Runtime.getRuntime(); Process proce = null; // 视频截图命令,封面图。 8是代表第8秒的时候截图 String cmd = ""; String cut = ffmpegPath +" -i "+ sourceFile +" "+ cmdParam +" "+ targetFile; String cutCmd = cmd + cut; proce = runtime.exec(cutCmd); proce.getOutputStream(); System.out.println("抽帧命令是:"+cut); return targetFile; } public static boolean checkfile(String path) { File file = new File(path); if (!file.isFile()) { return false; } return true; } //异步处理 public boolean processVideoSync(String ffmpegPath,String cmdParam, String sourceFile,String targetFile) { // 文件命名 List<String> commond = new ArrayList<String>(); commond.add(ffmpegPath); commond.add("-i"); commond.add(sourceFile); commond.addAll(Arrays.asList(cmdParam.trim().split("\\|"))); commond.add(targetFile); if(new File(targetFile).exists()) { new File(targetFile).delete(); } String cmds = ""; for (String cmd : commond) { cmds = cmds + " " + cmd; } System.out.println("执行命令参数为:" + cmds); try { // 调用线程命令进行转码 Process videoProcess = new ProcessBuilder(commond).redirectErrorStream(true).start(); //new PrintStream(videoProcess.getInputStream()).start(); //videoProcess.waitFor(); /*new InputStreamReader(videoProcess.getErrorStream()); BufferedReader stdout = new BufferedReader(new InputStreamReader(videoProcess.getInputStream())); String line; while ((line = stdout.readLine()) != null) { dealLine(line); }*/ dealStream(videoProcess); videoProcess.waitFor(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } //处理输出流 public abstract void dealLine(String line); public abstract void dealStream(Process process ); }