Java calls ffmpeg to implement the method of video conversion

  • 2020-04-01 03:52:11
  • OfStack

This article illustrates how Java calls ffmpeg for video conversion. Share with you for your reference. Specific analysis is as follows:

I tested the environment on the Windows platform...

Need ffmpeg.exe under e:\; Mencoder. Exe; Drv43260. DLL; Pncrt.dll consists of four files.
 
Also put the following video files named a in e:\input; To e: \ output; The Java program executes to get a converted file for a.flv.

Ffmpeg. exe can parse the format :(asx, asf, MPG, WMV, 3gp, mp4, mov, avi, FLV, etc.)

For the file format (wmv9, rm, RMVB, etc.) which cannot be parsed by ffmpegy.exe, avi(which can be parsed by ffmpeg) can be converted by other tools (mencoder).

Mencoder. Exe; Drv43260. DLL; Pncrt.dll these three files are prepared for the file format (wmv9, rm, RMVB, etc.) to be converted to avi(ffmpeg can be resolved).

Convert the converted avi files to FLV format video files 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+" is 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 the file directly to a FLV file
    } else if (type==1) {
      String avifilepath = processAVI(type);
      if (avifilepath == null)
        return false;//Avi file not available
      status = processFLV(avifilepath);//Convert avi to FLV
    }
    return status;
  }
  private static int checkContentType() {
    String type = PATH.substring(PATH.lastIndexOf(".") + 1,
     PATH.length()).toLowerCase();
//Ffmpeg 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;
    }
    //For file formats that ffmpeg cannot parse (wmv9, rm, RMVB, etc.),
    //You can use another tool (mencoder) to convert to avi(ffmpeg parsed) format.
    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 file formats that ffmpeg cannot parse (wmv9, rm, RMVB, etc.),You can use another tool (mencoder) to convert to avi(ffmpeg parsed) format.
  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 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 has been helpful to your Java programming.


Related articles: