Get video first frame (specified time) picture via FFmpeg

Source: Internet
Author: User
Tags readline stringbuffer

1, videoinfo.java  to obtain video information. Readers can carefully study this method.

Import java.io.*;
Import Java.util.regex.Matcher;

Import Java.util.regex.Pattern; /** * Access to video information * FFMPEG Homepage http://ffmpeg.org/about.html/public class Videoinfo {//video path private String
    Ffmpegapp;
    Video when private int hours;
    Video split private int minutes;
    Video seconds private float seconds;
    Video width private int width;
    

    Video 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 {St
        Ring 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; Print SB and get more information.
        such as 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;
    The 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;
    The public void setheigt (int heigt) {this.heigt = heigt; public static void Main (string[] args) {Videoinfo videoinfo = new Videoinfo ("D:\\FFMPEG\\BIN\\FFMPEG.E
        Xe ");
            try {videoinfo.getinfo ("f:/sun/diana_vickers_-_the_boy_who_murdered_love.mkv");
        System.out.println (Videoinfo);
        catch (Exception e) {e.printstacktrace (); }
    }
}

From this class, you can see, I put ffmpeg, decompression in the D:\\ffmpeg\\bin\\ffmpeg.exe path, the introduction of a video address, you can draw video information.

2, Videothumbtaker.java get video to specify the time to play the picture

Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;

Import Java.io.BufferedReader; /** * @author Bashan * FFMPEG Homepage http://ffmpeg.org/about.html * by Google get-I and last thumb of a-video usi ng 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")/**** * Get a picture of a specified time @param videofilename: Video path * @param thumbfilenam E: Picture Save path * @param width: Picture length * @param height: Picture width * @param hour: At specified time * @param min: Designated Sub * @param sec: Means Fixed seconds * @throws IOException * @throws interruptedexception/public void Getthumb (String videofilename, S Tring thumbfilename, int width, int height, int hour, int min, float sec) throws IOException, inte Rruptedexception {Processbuilder Processbuilder = new Processbuilder (Ffmpegapp, "-Y", "I", video  Filename, "-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:\\ffm
        Peg\\bin\\ffmpeg.exe "); try {Videothumbtaker.getthumb ("F:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.