java get video duration and video screenshot

1. First you need to download the ffmpeg plugin

There may be many things downloaded, but these two are actually needed to complete.

2. Then try to see if the plugin is available

Open cmd, follow the command in the figure, enter the path of ffmpeg, enter ffmpeg.exe -i video path address

If the following message appears, the plug-in is available. The information will record the duration, start point, file format, etc.

3. Start programming

    /**
     * 获取视频总时间
     *
     * @param videoPath  视频路径
     * @param ffmpegPath ffmpeg路径
     * @return
     */
    public static int getVideoTime(String videoPath, String ffmpegPath) throws Exception {
        List<String> commands = new java.util.ArrayList<String>();
        commands.add(ffmpegPath);
        commands.add("-i");
        commands.add(videoPath);
        //模拟cmd指令发送
        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()) {
            int time = getTimelen(m.group(1));
//                System.out.println(video_path+",视频时长:"+time+", 开始时间:"+m.group(2)+",比特率:"+m.group(3)+"kb/s");
            return time;
        }
        return 0;
    }
    
    //格式:"00:00:10.68" 转化为秒数
    public 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;//秒
        }
        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;
    }
/**
 * Get a screenshot of the first few seconds of the video
 *
 * @param videoPath
 * @param ffmpegPath
 * @param time
 * @param savePath
 */
public static void getVideoCover(String videoPath, String ffmpegPath, int time, String savePath) throws Exception {
    String videoName = videoPath.substring(videoPath.lastIndexOf("\\") + 1);
    String videoCoverName = videoName.substring(0, videoName.lastIndexOf(".")) + ".jpg";
    List<String> cmd = new ArrayList<>();
    cmd.add(ffmpegPath);
    cmd.add("-i");
    cmd.add(videoPath);
    cmd.add("-y");
    cmd.add("-f");
    cmd.add("mjpeg");
    cmd.add("-ss");
    cmd.add(String.valueOf(time));
    cmd.add("-t");
    cmd.add("0.001");
    cmd.add("-s");
    cmd.add("1920*1080");
    cmd.add(savePath + "\\" + videoCoverName);
    ProcessBuilder builder = new ProcessBuilder();
    builder.command(cmd);
    builder.start();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324635112&siteId=291194637