天天看點

Java通過調用FFMPEG擷取視訊時長(已測試)

轉載位址:http://blog.csdn.net/cyh1111/article/details/50232363 

FFmpeg是一套可以用來記錄、轉換數字音頻、視訊,并能将其轉化為流的開源計算機程式。采用LGPL或GPL許可證。它提供了錄制、轉換以及流化音視訊的完整解決方案。它包含了非常先進的音頻/視訊編解碼庫libavcodec,為了保證高可移植性和編解碼品質,libavcodec裡很多codec都是從頭開發的。

      主要利用的/usr/bin/ffmpeg   -i    698.mp4  這個指令  

       由此看來FFmpeg很強大,很多主流的音頻、視訊處理軟體都使用了FFmpeg。

       FFmpeg下載下傳下來解壓,cmd進入到FFmpeg.exe目錄中,即可在指令行下進行各種操作,檢視視訊資訊指令:ffmpeg 視訊 -i,如下圖:

       D:\ffmpeg\Libs>ffmpeg -i D:\MonitorRecord\monitor_20091222_050948_1.avi

[sql]  view plain  copy  

Java通過調用FFMPEG擷取視訊時長(已測試)
Java通過調用FFMPEG擷取視訊時長(已測試)
  1. FFmpeg version SVN-r10087, Copyright (c) 2000-2007 Fabrice Bellard, et al.  
  2.   configuration: --prefix=f:/svn_build_bins --enable-memalign-hack --enable-shared --disable-static --enable-w32threads --enable-liba52 --enable-avisynth --enable-libamr-nb --enable-libamr-wb --enable-libfaac --enable-libfaad --enable-libgsm --enable-libmp3lame --enable-libogg --enable-libtheora --enable-libvorbis --enable-libx264 --enable-gpl --extra-cflags=-I/usr/local/include --extra-ldflags=-L/usr/local/lib  
  3.   libavutil version: 49.5.0  
  4.   libavcodec version: 51.40.4  
  5.   libavformat version: 51.12.2  
  6.   built on Aug 12 2007 11:38:35, gcc: 4.2.1  
  7.   Compiled by msn: dev # fastreaming.com, 2007/08/12  
  8.   Enjoy it  
  9. Input #0, avi, from 'D:\MonitorRecord\monitor_20091222_050948_1.avi':  
  10.   Duration: 00:00:25.0, start: 0.000000, bitrate: 619 kb/s  
  11.   Stream #0.0: Video: mpeg4, yuv420p, 1620x1100,  1.14 fps(r)  
  12. Must supply at least one output file  

    在Java中執行此操作,解析傳回結果,可以得到視訊時長等資訊。

[java]  view plain  copy  

Java通過調用FFMPEG擷取視訊時長(已測試)
Java通過調用FFMPEG擷取視訊時長(已測試)
  1.     public static int getVideoTime(String video_path, String ffmpeg_path) {  
  2.         List<String> commands = new java.util.ArrayList<String>();  
  3.         commands.add(ffmpeg_path);  
  4.         commands.add("-i");  
  5.         commands.add(video_path);  
  6.         try {  
  7.             ProcessBuilder builder = new ProcessBuilder();  
  8.             builder.command(commands);  
  9.             final Process p = builder.start();  
  10.             //從輸入流中讀取視訊資訊  
  11.             BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));  
  12.             StringBuffer sb = new StringBuffer();  
  13.             String line = "";  
  14.             while ((line = br.readLine()) != null) {  
  15.                 sb.append(line);  
  16.             }  
  17.             br.close();  
  18.             //從視訊資訊中解析時長  
  19.             String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";  
  20.             Pattern pattern = Pattern.compile(regexDuration);  
  21.             Matcher m = pattern.matcher(sb.toString());  
  22.             if (m.find()) {  
  23.                 int time = getTimelen(m.group(1));  
  24.                 log.info(video_path+",視訊時長:"+time+", 開始時間:"+m.group(2)+",比特率:"+m.group(3)+"kb/s");  
  25.                 return time;  
  26.             }  
  27.         } catch (Exception e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.         return 0;  
  31.     }  
  32.     //格式:"00:00:10.68"  
  33.     private static int getTimelen(String timelen){  
  34.         int min=0;  
  35.         String strs[] = timelen.split(":");  
  36.         if (strs[0].compareTo("0") > 0) {  
  37.             min+=Integer.valueOf(strs[0])*60*60;//秒  
  38.         }  
  39.         if(strs[1].compareTo("0")>0){  
  40.             min+=Integer.valueOf(strs[1])*60;  
  41.         }  
  42.         if(strs[2].compareTo("0")>0){  
  43.             min+=Math.round(Float.valueOf(strs[2]));  
  44.         }  
  45.         return min;  
  46.     }  

以下是調用方法,經測試上述請求可以獲得正确時長

public void updateVideoDuration(String liveId,int videoId){
		OSSConfig ossConfig = new OSSConfig();
		String videolocalPath = config.getConfigValue("videoLocalPath")+liveId+ossConfig.getSuffix();
		LOGGER.info("updateVideoDuration ffmpegpath:" + videolocalPath+"   ,liveId:"+liveId);
		try{
			int duration = FfmpegUtil.getVideoTime(videolocalPath, "/user/bin/ffmpeg");
			Video video = new Video();
			video.setId(videoId);
			video.setDuration(duration);
			videoService.updateVideoDuration(video);
			LOGGER.info("updateVideoDuration  success,duration:"+duration);  
		}catch(Exception e){
			e.printStackTrace();
			LOGGER.error("updateVideoDuration error:"+e);
		}
		
	}
           

    在 Java 中執行此操作,解析傳回結果,可以得到視訊時長等資訊。