现在的位置: 首页 > 综合 > 正文

java调用ffmpeg获取视频信息函数代码

2014年11月20日 ⁄ 综合 ⁄ 共 3319字 ⁄ 字号 评论关闭
Java代码  收藏代码
  1.     public static void main(String[] args) {  
  2.   
  3.     String result =    processFLV("E:\\test\\京视传媒\\体育类\\xiao.flv");  
  4.      
  5.      
  6.     PatternCompiler compiler =new Perl5Compiler();  
  7.     try {  
  8.         String regexDuration ="Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";  
  9.         String regexVideo ="Video: (.*?), (.*?), (.*?)[,\\s]";  
  10.         String regexAudio ="Audio: (\\w*), (\\d*) Hz";  
  11.      
  12.         Pattern patternDuration = compiler.compile(regexDuration,Perl5Compiler.CASE_INSENSITIVE_MASK);  
  13.         PatternMatcher matcherDuration = new Perl5Matcher();  
  14.         if(matcherDuration.contains(result, patternDuration)){  
  15.             MatchResult re = matcherDuration.getMatch();  
  16.   
  17.             System.out.println("提取出播放时间  ===" +re.group(1));  
  18.             System.out.println("开始时间        =====" +re.group(2));  
  19.             System.out.println("bitrate 码率 单位 kb==" +re.group(3));  
  20.         }  
  21.          
  22.         Pattern patternVideo = compiler.compile(regexVideo,Perl5Compiler.CASE_INSENSITIVE_MASK);  
  23.         PatternMatcher matcherVideo = new Perl5Matcher();  
  24.          
  25.         if(matcherVideo.contains(result, patternVideo)){  
  26.             MatchResult re = matcherVideo.getMatch();  
  27.             System.out.println("编码格式  ===" +re.group(1));  
  28.             System.out.println("视频格式 ===" +re.group(2));  
  29.             System.out.println(" 分辨率  == =" +re.group(3));  
  30.         }  
  31.          
  32.         Pattern patternAudio = compiler.compile(regexAudio,Perl5Compiler.CASE_INSENSITIVE_MASK);  
  33.         PatternMatcher matcherAudio = new Perl5Matcher();  
  34.          
  35.         if(matcherAudio.contains(result, patternAudio)){  
  36.             MatchResult re = matcherAudio.getMatch();  
  37.             System.out.println("音频编码             ===" +re.group(1));  
  38.             System.out.println("音频采样频率  ===" +re.group(2));  
  39.         }  
  40.   
  41.     } catch (MalformedPatternException e) {  
  42.         e.printStackTrace();  
  43.     }  
  44.   
  45.     }  
  46.      
  47.   
  48. //  ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)  
  49.     private static String processFLV(String inputPath) {  
  50. /* 
  51.       if (!checkfile(inputPath)){ 
  52.           _log.warn(inputPath+" is not file"); 
  53.           return false; 
  54.          } 
  55. */  
  56.         List<String> commend=new java.util.ArrayList<String>();  
  57.          
  58. //        commend.add("e:\\videoconver\\ffmpeg\\ffmpeg ");//可以设置环境变量从而省去这行  
  59.        commend.add("ffmpeg");  
  60.         commend.add("-i");  
  61.         commend.add(inputPath);  
  62.         
  63.         try {  
  64.   
  65.             ProcessBuilder builder = new ProcessBuilder();  
  66.             builder.command(commend);  
  67.             builder.redirectErrorStream(true);  
  68.             Process p= builder.start();  
  69.   
  70.            //1. start  
  71.             BufferedReader buf = null// 保存ffmpeg的输出结果流  
  72.             String line = null;  
  73.           //read the standard output  
  74.   
  75.             buf = new BufferedReader(new InputStreamReader(p.getInputStream()));  
  76.              
  77.             StringBuffer sb= new StringBuffer();  
  78.             while ((line = buf.readLine()) != null) {  
  79.              System.out.println(line);  
  80.              sb.append(line);  
  81.              continue;  
  82.                  }  
  83.             int ret = p.waitFor();//这里线程阻塞,将等待外部转换进程运行成功运行结束后,才往下执行  
  84.             //1. end  
  85.             return sb.toString();  
  86.         } catch (Exception e) {  
  87. //            System.out.println(e);  
  88.             return null;  
  89.         }  
  90.     }  

抱歉!评论已关闭.