Java call FFmpeg implementation of video conversion method _java

Source: Internet
Author: User
Tags flv file

This article describes the Java call FFmpeg implementation of video conversion methods. Share to everyone for your reference. The specific analysis is as follows:

Here the environment I was tested under the Windows platform ...

A total of 4 documents are required under E:\ ffmpeg.exe;mencoder.exe;drv43260.dll;pncrt.dll.

Also in E:\input decentralized various file name A of the following video files, but also to E:\output;java program execution can get a a.flv converted files.

Ffmpeg.exe can parse the format: (asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv, etc.)

For Ffmpeg.exe unresolved file formats (WMV9,RM,RMVB, etc.), you can use other tools (MEncoder) to convert to AVI (ffmpeg resolvable) format;

Mencoder.exe;drv43260.dll;pncrt.dll These 3 files are prepared for file format (WMV9,RM,RMVB, etc.) to be converted to AVI (ffmpeg resolvable) format;

Then convert the good AVI file into the FLV format video file with Ffmpeg.exe ...

The contents of the Java file are as follows:

Import Java.io.File;
Import java.util.List;
 public class Convertvideo {private final static String PATH = "C:\\test\\a.mpg";
     public static void Main (string[] args) {if (!checkfile (PATH)) {System.out.println (path+ "are not file");
    Return
    } if (process ()) {System.out.println ("OK");
    }} private static Boolean process () {int type = Checkcontenttype ();
    Boolean status = FALSE; if (type==0) {status = processflv (PATH);//convert file directly to FLV file} else if (type==1) {String Avifilepath = Proces
      SAVI (type); if (Avifilepath = null) return false;//AVI file does not get status = processflv (Avifilepath);//convert AVI to FLV} r
  Eturn status; private static int Checkcontenttype () {String type = path.substring (Path.lastindexof (".") + 1, path.length ()
). toLowerCase ();
    FFmpeg can parse the format: (asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv, etc.) if (Type.equals ("avi")) {return 0; else if (type.equals ("mpg")) {RETUrn 0;
    else if (type.equals ("wmv")) {return 0;
    else if (type.equals ("3gp")) {return 0;
    else if (type.equals ("mov")) {return 0;
    else if (type.equals ("mp4")) {return 0;
    else if (type.equals ("ASF")) {return 0;
    else if (type.equals ("ASX")) {return 0;
    else if (type.equals ("flv")) {return 0;
    //To FFmpeg unresolved file format (WMV9,RM,RMVB, etc.),//can be converted to AVI (ffmpeg resolvable) format with other tools (MEncoder) first.
    else if (type.equals ("WMV9")) {return 1;
    else if (type.equals ("rm")) {return 1;
    else if (type.equals ("RMVB")) {return 1;
  return 9;
   private static Boolean checkfile (String path) {file File=new file (path);
   if (!file.isfile ()) {return false;
  return true;
  //For FFmpeg unresolved file formats (WMV9,RM,RMVB, etc.), you can first convert to AVI (ffmpeg resolvable) format with another tool (MEncoder).
    private static String Processavi (int type) {list<string> commend=new java.util.arraylist<string> (); CoMmend.add ("E:\\mencoder");
    Commend.add (PATH);
    Commend.add ("-oac");
    Commend.add ("LAVC");
    Commend.add ("-lavcopts");
    Commend.add ("acodec=mp3:abitrate=64");
    Commend.add ("-OVC");
    Commend.add ("XviD");
    Commend.add ("-xvidencopts");
    Commend.add ("bitrate=600");
    Commend.add ("-of");
    Commend.add ("Avi");
    Commend.add ("-O");
    Commend.add ("C:\\home\\a.avi");
      try{Processbuilder builder = new Processbuilder ();
      Builder.command (commend);
      Builder.start ();
    return "C:\\home\\a.avi";
     }catch (Exception e) {e.printstacktrace ();
    return null;
   }}//ffmpeg can parse the format: (asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv, etc.) private static Boolean processflv (String oldfilepath) {
     if (!checkfile (PATH)) {System.out.println (oldfilepath+ "is not file");
     return false;
    } list<string> commend=new java.util.arraylist<string> ();
    Commend.add ("E:\\ffmpeg");
    Commend.add ("I");
  Commend.add (Oldfilepath);  Commend.add ("-ab");
    Commend.add ("64");
    Commend.add ("-acodec");
    Commend.add ("MP3");
    Commend.add ("-ac");
    Commend.add ("2");
    Commend.add ("-ar");
    Commend.add ("22050");
    Commend.add ("B");
    Commend.add ("230");
    Commend.add ("-R");
    Commend.add ("24");
    Commend.add ("Y");
    Commend.add ("c:\\home\\a.flv");
      try {processbuilder builder = new Processbuilder ();
      Builder.command (commend);
      Builder.start ();
    return true;
      catch (Exception e) {e.printstacktrace ();
    return false; }
  }
}

I hope this article will help you with your Java programming.

Related Article

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.