JAVA程序调取ffmpeg工具处理视频问题 1.安装ffmpeg工具 ffmpeg的官网地址是:https://www.ffmpeg.org/ 详情的安装步骤基本其他博客基本都有 2.JAVA代码处理ffmpeg 1)获取视频码率(ffprobe -v quiet -print_format json -show_format -i VCG42N473315161.mp4) public static String getBitrate(String filePath) { try { List<String> command = new ArrayList<String>(); command.add("ffprobe"); command.add("-v"); command.add("quiet"); command.add("-print_format"); command.add("json"); command.add("-show_format"); command.add("-i"); command.add(filePath); Process p= Runtime.getRuntime().exec(command.toArray(new String[command.size()])); BufferedInputStream in = new BufferedInputStream(p.getInputStream()); BufferedReader inBr = new BufferedReader(new InputStreamReader(in)); StringBuffer sb = new StringBuffer(); String lineStr; while ((lineStr = inBr.readLine()) != null) sb.append(lineStr); if (p.waitFor() != 0) { if (p.exitValue() == 1) System.err.println("命令执行失败!"); } inBr.close(); in.close(); return analyseInfo(sb.toString());
} catch (Exception e) { e.printStackTrace(); } return ""; } 2)切一个其他码率的视频如原视频码率为5000K现在要3000K码率视频(ffmpeg -i 原视频.mp4 -b:v 2000k 切换后.mp4) public static void changeBitrate(String inputPath,String outPath,String bitrate) { try { List<String> command = new ArrayList<String>(); command.add("ffmpeg"); command.add("-i"); command.add(inputPath); command.add("-b:v"); command.add(bitrate); command.add("-bufsize"); command.add(bitrate); command.add("-maxrate"); command.add(bitrate); command.add(outPath); Process p= Runtime.getRuntime().exec(command.toArray(new String[command.size()])); BufferedInputStream in = new BufferedInputStream(p.getInputStream()); BufferedReader inBr = new BufferedReader(new InputStreamReader(in)); StringBuffer sb = new StringBuffer(); String lineStr; while ((lineStr = inBr.readLine()) != null) sb.append(lineStr); if (p.waitFor() != 0) { if (p.exitValue() == 1) System.err.println("命令执行失败!"); } inBr.close(); in.close();
} catch (Exception e) { e.printStackTrace(); }
} 3)设置成无声(ffmpeg -i VCG42N1249212638.mp4 -vcodec copy -filter:a volume=0 test2.mp4) public static void setvolume(String inputPath,String outPath) { try { List<String> command = new ArrayList<String>(); command.add("ffmpeg"); command.add("-i"); command.add(inputPath); command.add("-vcodec"); command.add("copy"); command.add("-filter:a"); command.add("volume=0"); command.add(outPath); Process p= Runtime.getRuntime().exec(command.toArray(new String[command.size()])); BufferedInputStream in = new BufferedInputStream(p.getInputStream()); BufferedReader inBr = new BufferedReader(new InputStreamReader(in)); StringBuffer sb = new StringBuffer(); String lineStr; while ((lineStr = inBr.readLine()) != null) sb.append(lineStr); if (p.waitFor() != 0) { if (p.exitValue() == 1) System.err.println("命令执行失败!"); } inBr.close(); in.close();
} catch (Exception e) { e.printStackTrace(); }
} 4.切个gif图片(ffmpeg -i xxx.mp4 -ss 00:00:00 -t 2 -s 400*710 -r 12 xxx.gif) public static void getGif(String inputPath,String outPath) { try { List<String> command = new ArrayList<String>(); command.add("ffmpeg"); command.add("-i"); command.add(inputPath); command.add("-ss"); command.add("00:00:00"); command.add("-t"); command.add("2"); command.add("-s"); command.add("400*710"); command.add("-r"); command.add("12"); command.add(outPath); Process p= Runtime.getRuntime().exec(command.toArray(new String[command.size()])); BufferedInputStream in = new BufferedInputStream(p.getInputStream()); BufferedReader inBr = new BufferedReader(new InputStreamReader(in)); StringBuffer sb = new StringBuffer(); String lineStr; while ((lineStr = inBr.readLine()) != null) sb.append(lineStr); if (p.waitFor() != 0) { if (p.exitValue() == 1) System.err.println("命令执行失败!"); } inBr.close(); in.close();
} catch (Exception e) { e.printStackTrace(); }
} 相信通过上面的几个示例大家能够举一反三知道怎么通过java调用ffmpeg对视频进行处理了