FFmpeg frame series: get video duration

1. Business scenario

Business requirement: display the duration for the uploaded video.

2. FFmpeg framework to process video information

FFmpeg official website: http://ffmpeg.org/

3. Code implementation

3.1 Windows install FFmpeg

I will teach you in this five-minute JAVA code: FFmpeg implementation video demo (window version) wrote in very detailed, install FFmepg on windows/Linux, I will not elaborate here.

3.2 Execute password to view video information

After installing FFmpeg in the window, execute the command to get the duration in cmd:

F:\ffmpegDemo\ffmpeg\bin\ffmpeg.exe -i F://ffmpegDemo//test.mp4

PS: Decompress the ffmpeg program F:\ffmpegDemo\ffmpeg\bin\ffmpeg.exe and the local video stored in the windows: F://ffmpegDemo//test.mp4

The implementation effect is as follows:
Insert picture description here

3.3 Java parses the information and returns the duration

Execute the command line through FFmpeg to obtain the returned video information, filter the target data of the video information through java, and return it.

public static void main(String[] args) {
    
    
        
        String timeLength = getVideoTime("F://ffmpegDemo//test.mp4","F:\\ffmpegDemo\\ffmpeg\\bin\\ffmpeg.exe");
        if(timeLength.length()>0){
    
    //字符串截取
            timeLength =timeLength.substring(0,timeLength.indexOf("."));
        }
        System.out.println("视频时长:"+timeLength);
       
    }

Insert picture description here

4. Post completed Demo

public class ExecWindowCMD {
    
    

    public static void main(String[] args) {
    
    
        
        String timeLength = getVideoTime("F://ffmpegDemo//test.mp4","F:\\ffmpegDemo\\ffmpeg\\bin\\ffmpeg.exe");
        if(timeLength.length()>0){
    
    //字符串截取
            timeLength =timeLength.substring(0,timeLength.indexOf("."));
        }
        System.out.println("视频时长:"+timeLength);
       
    }

     /**
     *获取视频时间
     * @param video_path  视频路径
     * @param ffmpeg_path ffmpeg安装路径
     * @return
     */
    public static String 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);
        System.out.println("命令行:"+ffmpeg_path+" -i "+video_path);
        try {
    
    
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commands);
            final Process p = builder.start();

            //从输入流中读取视频信息
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            StringBuffer sb = new StringBuffer();
            String line = "";
            while ((line = br.readLine()) != null) {
    
    
                sb.append(line);
            }
            br.close();

            //从视频信息中解析时长
            String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
            Pattern pattern = Pattern.compile(regexDuration);
            Matcher m = pattern.matcher(sb.toString());
            if (m.find()) {
    
    

                //System.out.println(video_path+",视频时长:"+m.group(1)+", 开始时间:"+m.group(2)+",比特率:"+m.group(3)+"kb/s");
                return m.group(1);
            }
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }

        return "";
    }
}

The implementation effect is as follows:
Insert picture description here

Video source file:
Insert picture description here

Guess you like

Origin blog.csdn.net/u010312671/article/details/108738386