Java gets the video duration by calling FFMPEG

  • 2021-07-22 09:43:03
  • OfStack

FFmpeg is a set of open source computer programs that can be used to record, convert digital audio and video, and convert them into streams. Use LGPL or GPL license. It provides a complete solution for recording, converting and streaming audio and video. It includes a very advanced audio/video codec library libavcodec. In order to ensure high portability and codec quality, many codec in libavcodec are developed from scratch.

From this point of view, FFmpeg is very powerful, and many mainstream audio and video processing software use FFmpeg.

Download FFmpeg and decompress it. cmd enters FFmpeg. exe directory, and you can perform various operations under the command line to view video information command: ffmpeg Video-i, as shown in the following figure:

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


FFmpeg version SVN-r10087, Copyright (c) 2000-2007 Fabrice Bellard, et al.
 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
 libavutil version: 49.5.0
 libavcodec version: 51.40.4
 libavformat version: 51.12.2
 built on Aug 12 2007 11:38:35, gcc: 4.2.1
 
 Compiled by msn: dev # fastreaming.com, 2007/08/12
 Enjoy it
 
Input #0, avi, from 'D:\MonitorRecord\monitor_20091222_050948_1.avi':
 Duration: 00:00:25.0, start: 0.000000, bitrate: 619 kb/s
 Stream #0.0: Video: mpeg4, yuv420p, 1620x1100, 1.14 fps(r)
Must supply at least one output file

Perform this operation in Java, parse the returned results, and get information such as video duration.


/**
 *  Total time to get video 
 * @param viedo_path  Video path 
 * @param ffmpeg_path ffmpeg Path 
 * @return
 */
 public static int getVideoTime(String video_path, String ffmpeg_path) {
 List<String> commands = new java.util.ArrayList<String>();
 commands.add(ffmpeg_path);
 commands.add("-i");
 commands.add(video_path);
 try {
 ProcessBuilder builder = new ProcessBuilder();
 builder.command(commands);
 final Process p = builder.start();
 
 // Read video information from the input stream 
 BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
 StringBuffer sb = new StringBuffer();
 String line = "";
 while ((line = br.readLine()) != null) {
 sb.append(line);
 }
 br.close();
 
 // Parsing duration from video information 
 String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
 Pattern pattern = Pattern.compile(regexDuration);
 Matcher m = pattern.matcher(sb.toString());
 if (m.find()) {
 int time = getTimelen(m.group(1));
 log.info(video_path+", Video duration: "+time+",  Start time: "+m.group(2)+", Bit rate: "+m.group(3)+"kb/s");
 return time;
 }
 } catch (Exception e) {
 e.printStackTrace();
 }
 
 return 0;
 }
 
 // Format :"00:00:10.68"
 private static int getTimelen(String timelen){
 int min=0;
 String strs[] = timelen.split(":");
 if (strs[0].compareTo("0") > 0) {
 min+=Integer.valueOf(strs[0])*60*60;// Seconds 
 }
 if(strs[1].compareTo("0")>0){
 min+=Integer.valueOf(strs[1])*60;
 }
 if(strs[2].compareTo("0")>0){
 min+=Math.round(Float.valueOf(strs[2]));
 }
 return min;
}

Related articles: