找的资料,整理了一下
ffmpeg下载地址:http://ffmpeg.org/
依赖包:jakarta-oro.jar,提供一个下载链接http://download.csdn.net/detail/q370797240/8420579
1 import java.io.BufferedReader; 2 import java.io.InputStreamReader; 3 import java.util.List; 4 5 import org.apache.oro.text.regex.MalformedPatternException; 6 import org.apache.oro.text.regex.PatternCompiler; 7 import org.apache.oro.text.regex.PatternMatcher; 8 import org.apache.oro.text.regex.Perl5Compiler; 9 import org.apache.oro.text.regex.Perl5Matcher; 10 11 12 13 public class Test { 14 15 public static void main(String[] args) { 16 String result = processFLV("F:\\test\\tc111418.mov"); 17 PatternCompiler compiler = new Perl5Compiler(); 18 try { 19 String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s"; 20 String regexVideo = "Video: (.*?), (.*?), (.*?)[,\\s]"; 21 String regexAudio = "Audio: (\\w*), (\\d*) Hz"; 22 23 org.apache.oro.text.regex.Pattern patternDuration = compiler 24 .compile(regexDuration, Perl5Compiler.CASE_INSENSITIVE_MASK); 25 PatternMatcher matcherDuration = new Perl5Matcher(); 26 if (matcherDuration.contains(result, patternDuration)) { 27 org.apache.oro.text.regex.MatchResult re = matcherDuration 28 .getMatch(); 29 30 System.out.println("提取出播放时间 ===" + re.group(1)); 31 System.out.println("开始时间 =====" + re.group(2)); 32 System.out.println("bitrate 码率 单位 kb==" + re.group(3)); 33 } 34 35 org.apache.oro.text.regex.Pattern patternVideo = 36 compiler.compile(regexVideo,Perl5Compiler.CASE_INSENSITIVE_MASK); 37 PatternMatcher matcherVideo = new Perl5Matcher(); 38 39 if(matcherVideo.contains(result, patternVideo)){ 40 org.apache.oro.text.regex.MatchResult re = matcherVideo.getMatch(); 41 System.out.println("编码格式 ===" +re.group(1)); 42 System.out.println("视频格式 ===" +re.group(2)); 43 System.out.println(" 分辨率 == =" +re.group(3)); 44 } 45 46 org.apache.oro.text.regex.Pattern patternAudio = 47 compiler.compile(regexAudio,Perl5Compiler.CASE_INSENSITIVE_MASK); 48 PatternMatcher matcherAudio = new Perl5Matcher(); 49 50 if(matcherAudio.contains(result, patternAudio)){ 51 org.apache.oro.text.regex.MatchResult re = matcherAudio.getMatch(); 52 System.out.println("音频编码 ===" +re.group(1)); 53 System.out.println("音频采样频率 ===" +re.group(2)); 54 } 55 56 } catch (MalformedPatternException e) { 57 e.printStackTrace(); 58 } 59 60 } 61 62 // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) 63 private static String processFLV(String inputPath) { 64 List<String> commend = new java.util.ArrayList<String>(); 65 commend.add("F:/ffmpeg/bin/ffmpeg.exe"); 66 commend.add("-i"); 67 commend.add(inputPath); 68 69 try { 70 ProcessBuilder builder = new ProcessBuilder(); 71 builder.command(commend); 72 builder.redirectErrorStream(true); 73 Process p = builder.start(); 74 75 // 1. start 76 BufferedReader buf = null; // 保存ffmpeg的输出结果流 77 String line = null; 78 // read the standard output 79 80 buf = new BufferedReader(new InputStreamReader(p.getInputStream())); 81 82 StringBuffer sb = new StringBuffer(); 83 while ((line = buf.readLine()) != null) { 84 System.out.println(line); 85 sb.append(line); 86 continue; 87 } 88 p.waitFor();// 这里线程阻塞,将等待外部转换进程运行成功运行结束后,才往下执行 89 // 1. end 90 return sb.toString(); 91 } catch (Exception e) { 92 // System.out.println(e); 93 return null; 94 } 95 } 96 97 }
另外ffmpeg也可以用来截图,截图方法如下:
1 /** 2 * 给指定的视频文件截图,图片大小为256*144 3 * @param fullPath 4 * @param fileName 5 * @throws IOException 6 */ 7 public static void createImg(String fullPath, String fileName)throws IOException { 8 if(fullPath==null||fileName==null){ 9 return; 10 } 11 StringBuilder cmdBuilder = new StringBuilder(); 12 cmdBuilder.append("F:/ffmpeg/bin/ffmpeg.exe -i"); 13 // cmdBuilder.append("/usr/local/bin/ffmpeg -i"); 14 cmdBuilder.append(" "); 15 cmdBuilder.append("-i"); 16 cmdBuilder.append(" "); 17 cmdBuilder.append(fullPath); 18 cmdBuilder.append(" "); 19 cmdBuilder.append("-y -f image2 -ss 8 -t 0.001 -s 256*144"); 20 cmdBuilder.append(" "); 21 cmdBuilder.append("F:/image/"); 22 cmdBuilder.append(fileName); 23 24 String cmdStr = cmdBuilder.toString(); 25 cmdStr = cmdStr.replace(".mov|.mp4", ".jpg"); 26 Process process = Runtime.getRuntime().exec(cmdStr); 27 try { 28 process.waitFor(); 29 } catch (InterruptedException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } 33 }
fullPath为所要截图的视频,
fileName为截图文件的名称,这里截图的地址存放在F:/image/文件,偷懒没有把它写入传入参数中~ ~。