1. 程式人生 > >JAVA獲取視訊播第一幀

JAVA獲取視訊播第一幀

因為有這個需求,通過 ffmpeg 獲取視訊第一幀圖片

Google一把,就有了結果。

參考:

1、http://www.codereye.com/2010/05/get-first-and-last-thumb-of-video-using.html

1、VideoInfo.java 獲取視訊資訊。讀者可認真研究此方法。

複製程式碼
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 獲取視訊的資訊
 * FFMPEG homepage http://ffmpeg.org/about.html
 */
public class VideoInfo { //視訊路徑 private String ffmpegApp; //視訊時 private int hours; //視訊分 private int minutes; //視訊秒 private float seconds; //視訊width private int width; //視訊height private int heigt; public VideoInfo() {} public VideoInfo(String ffmpegApp) {
this.ffmpegApp = ffmpegApp; } public String toString() { return "time: " + hours + ":" + minutes + ":" + seconds + ", width = " + width + ", height= " + heigt; } 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("w:\\d+ h:\\d+"); matcher = pattern.matcher(sb); if (matcher.find()) { String wh = matcher.group(); //w:100 h:100 String[] strs = wh.split("\\s+"); if(strs != null && strs.length == 2) { width = Integer.parseInt(strs[0].split(":")[1]); heigt = Integer.parseInt(strs[1].split(":")[1]); } } 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 String getFfmpegApp() { return ffmpegApp; } public void setFfmpegApp(String ffmpegApp) { this.ffmpegApp = ffmpegApp; } public int getHours() { return hours; } public void setHours(int hours) { this.hours = hours; } public int getMinutes() { return minutes; } public void setMinutes(int minutes) { this.minutes = minutes; } public float getSeconds() { return seconds; } public void setSeconds(float seconds) { this.seconds = seconds; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getHeigt() { return heigt; } public void setHeigt(int heigt) { this.heigt = heigt; } public static void main(String[] args) { VideoInfo videoInfo = new VideoInfo("D:\\ffmpeg\\bin\\ffmpeg.exe"); try { videoInfo.getInfo("f:/reyo/test.mkv"); System.out.println(videoInfo); } catch (Exception e) { e.printStackTrace(); } } }
複製程式碼

從此類,可以看出,我把 ffmpeg,解壓在了 D:\\ffmpeg\\bin\\ffmpeg.exe 路徑下,傳入一個視訊地址,就可以得出視訊資訊。

2、VideoThumbTaker.java 獲取視訊指定播放時間的圖片

複製程式碼
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

/**
 * @author reyo
 * FFMPEG homepage http://ffmpeg.org/about.html
 * By Google Get first and last thumb of a video using Java and FFMpeg
 * From http://www.codereye.com/2010/05/get-first-and-last-thumb-of-video-using.html
 */

public class VideoThumbTaker
{
    protected String ffmpegApp;

    public VideoThumbTaker(String ffmpegApp)
    {
        this.ffmpegApp = ffmpegApp;
    }

    @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 static void main(String[] args)
    {
        VideoThumbTaker videoThumbTaker = new VideoThumbTaker("D:\\ffmpeg\\bin\\ffmpeg.exe");
        try
        {
            videoThumbTaker.getThumb("f:/reyo/test.mkv", "C:\\thumbTest.png",    800, 600, 0, 0, 9);
            System.out.println("over");
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
複製程式碼

3、VideoFirstThumbTaker.java 獲取第一幀圖片

複製程式碼
import java.io.IOException;

/***
 * 
 * 得到第一秒(也是第一幀)圖片
 */
public class VideoFirstThumbTaker extends VideoThumbTaker
{
    public VideoFirstThumbTaker(String ffmpegApp)
    {
        super(ffmpegApp);
    }

    public void getThumb(String videoFilename, String thumbFilename, int width,
            int height) throws IOException, InterruptedException
    {
        super.getThumb(videoFilename, thumbFilename, width, height, 0, 0, 1);
    }
}
複製程式碼

4、VideoLastThumbTaker.java 獲取最後一幀圖片

複製程式碼
/**
 * 得到最後一秒(也是最後一幀)圖片
 */

public class VideoLastThumbTaker extends VideoThumbTaker
{
    public VideoLastThumbTaker(String ffmpegApp)
    {
        super(ffmpegApp);
    }

    public void getThumb(String videoFilename, String thumbFilename, int width,
            int height) throws IOException, InterruptedException
    {
        VideoInfo videoInfo = new VideoInfo(ffmpegApp);
        videoInfo.getInfo(videoFilename);
        super.getThumb(videoFilename, thumbFilename, width, height,
                videoInfo.getHours(), videoInfo.getMinutes(),
                videoInfo.getSeconds() - 0.2f);
    }
}
複製程式碼