java获取视频播第一帧

来源:互联网 发布:北京搜索引擎优化seo 编辑:程序博客网 时间:2024/05/19 11:48

这里用到FFMPEG ,做视频必备的软件。大家可通过 http://ffmpeg.org/ 了解。Windows版本的软件,可通过http://ffmpeg.zeranoe.com/builds/ 下载。


public class VideoThumbTaker {
    protected String ffmpegApp = "D:\\ffmpeg\\bin\\ffmpeg.exe";
    // 视频时
    private int hours;
    // 视频分
    private int minutes;
    // 视频秒
    private float seconds;
    // 视频width
    private int width;
    // 视频height
    private int height;

    public VideoThumbTaker() {

    }


  @SuppressWarnings("unused")
    /****
     * 获取指定时间内的图片
     * @param videoFilename:视频路径
     * @param thumbFilename:图片保存路径
     * @param width:图片长
     * @param height:图片宽
     * @param hour:指定时
     * @param min:指定分
     * @param sec:指定秒
     * @throws IOException
     * @throws InterruptedException
     */
    public void getThumb(String videoFilename, String thumbFilename, int width,
            int height, int hour, int min, float sec) throws IOException,
            InterruptedException
    {
        ProcessBuilder processBuilder = new ProcessBuilder(ffmpegApp, "-y",
                "-i", videoFilename, "-vframes", "1", "-ss", hour + ":" + min
                        + ":" + sec, "-f", "mjpeg", "-s", width + "*" + height,
                "-an", thumbFilename);

        Process process = processBuilder.start();

        InputStream stderr = process.getErrorStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null)
            ;
        process.waitFor();
        
        if(br != null)
            br.close();
        if(isr != null)
            isr.close();
        if(stderr != null)
            stderr.close();
    }
    
   


    public void getInfo(String videoFilename) throws IOException, InterruptedException {
        String tmpFile = videoFilename + ".tmp.png";
        ProcessBuilder processBuilder = new ProcessBuilder(ffmpegApp, "-y", "-i", videoFilename, "-vframes", "1", "-ss",
                "0:0:0", "-an", "-vcodec", "png", "-f", "rawvideo", "-s", "100*100", tmpFile);

        Process process = processBuilder.start();

        InputStream stderr = process.getErrorStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        String line;
        // 打印 sb,获取更多信息。 如 bitrate、width、heigt
        StringBuffer sb = new StringBuffer();
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

        new File(tmpFile).delete();

        System.out.println("video info:\n" + sb);
        Pattern pattern = Pattern.compile("Duration: (.*?),");
        Matcher matcher = pattern.matcher(sb);

        if (matcher.find()) {
            String time = matcher.group(1);
            calcTime(time);
        }

        pattern = Pattern.compile("Video: (.*?), (.*?), (.*?),(.*?),");
        matcher = pattern.matcher(sb);

        if (matcher.find()) {
            String wh = matcher.group(4);
            // w:100 h:100
            
            String[] strs = wh.split("x");
            if (strs != null && strs.length == 2) {
                width = Integer.parseInt(strs[0].trim());
                if (strs[1].trim().contains("[")) {
                    String hString=strs[1].trim().substring(0,strs[1].trim().indexOf("["));
                    height = Integer.parseInt(hString);
                }else {
                    height = Integer.parseInt(strs[1].trim());
                }
            }
        }

        process.waitFor();
        if (br != null)
            br.close();
        if (isr != null)
            isr.close();
        if (stderr != null)
            stderr.close();
    }

    private void calcTime(String timeStr) {
        String[] parts = timeStr.split(":");
        hours = Integer.parseInt(parts[0]);
        minutes = Integer.parseInt(parts[1]);
        seconds = Float.parseFloat(parts[2]);
    }

    public void CutVideoPic(String videoPath, String imgPath) {
        VideoThumbTaker videoThumbTaker = new VideoThumbTaker();
        try {
            videoThumbTaker.getThumb(videoPath, imgPath, 0, 0, 9);
            System.out.println("over");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

原创粉丝点击