This article shares the specific code for Java video format conversion for your reference. The specific content is as follows
The core is to use ffmpeg to convert videos. We do not write the code to convert videos ourselves, but just call ffmpeg, which will help us complete the video conversion. The types supported by ffmpeg are: asx, asf, mpg, wmv, 3gp, mp4, mov, avi, flv, etc. These types can be directly converted using ffmpeg. The types that ffmpeg does not support are: wmv9, rm, rmvb, etc. These types need to be converted to avi (ffmpeg can parse) format first using other tools (mencoder).
I won’t say much nonsense. First of all, I need to prepare the relevant libraries and videos to be converted, as shown in the following figure
The following is the code part
package com.sino.test; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; /** * java implements the conversion of video format* @author liuyazhuang * */ public class ChangeVideo { public static void main(String[] args) { ChangeVideo.convert("d://myeclipse//aa.avi", "d://myeclipse//bb.mp4"); } /** * @param inputFile: Videos that need to be converted* @param outputFile: Videos after conversion w * @return */ public static boolean convert(String inputFile, String outputFile) { if (!checkfile(inputFile)) { System.out.println(inputFile + " is nokt file"); return false; } if (process(inputFile, outputFile)) { System.out.println("ok"); return true; } return false; } // Check whether the file exists private static boolean checkfile(String path) { File file = new File(path); if (!file.isFile()) { return false; } return true; } /** * @param inputFile * @param outputFile * @return * Convert video file*/ private static boolean process(String inputFile, String outputFile) { int type = checkContentType(inputFile); boolean status = false; if (type == 0) { status = processFLV(inputFile, outputFile);// Convert avi to flv file directly} else if (type == 1) { String avifilepath = processAVI(type, inputFile); if (avifilepath == null) return false;// Avi file does not get status = processFLV(avifilepath, outputFile);// Convert avi to flv } return status; } private static int checkContentType(String inputFile) { String type = inputFile.substring(inputFile.lastIndexOf(".") + 1, inputFile.length()).toLowerCase(); // Formats that ffmpeg can parse: (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("wmv")) { return 0; } else if (type.equals("wmv")) { return 0; } else if (type.equals("wmv")) { return 0; } else if (type.equals("wmv")) { return 0; } else if (type.equals("wmv")) { return 0; } else if (type.equals("wmv")) { return 0; } else if (type.equals("wmv")) { return 0; } else if (type.equals("wmv")) { return 0; } else if (type.equals("wmv")) { return 0; } (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; } // File formats that cannot be parsed for ffmpeg (wmv9, rm, rmvb, etc.), // You can first use another tool (mencoder) to convert to avi(ffmpeg parsable) format. else if (type.equals("wmv9")) { return 1; } else if (type.equals("rm")) { return 1; } else if (type.equals("rmvb")) { return 1; } return 9; } // The formats that ffmpeg parsable: (asx, asf, mpg, wmv, 3gp, mp4, mov, avi, flv, etc.) are directly converted to target video private static boolean processFLV(String inputFile, String outputFile) { if (!checkfile(inputFile)) { System.out.println(inputFile + " is not file"); return false; } List<String> comment = new ArrayList<String>(); comment.add(Constants.ffmpegPath); comment.add("-i"); comment.add(inputFile); comment.add("-ab"); comment.add("128"); comment.add("-acodec"); comment.add("libmp3lame"); comment.add("-ac"); comment.add("1"); comment.add("-ar"); comment.add("22050"); comment.add("-r"); comment.add("29.97"); //High quality comment.add("-qscale"); comment.add("6"); //Low quality///commend.add("-b"); //commend.add("512"); comment.add("-y"); comment.add(outputFile); StringBuffer test = new StringBuffer(); for (int i = 0; i < comment.size(); i++) { test.append(commend.get(i) + " "); } System.out.println(test); try { ProcessBuilder builder = new ProcessBuilder(); builder.command(commend); builder.start(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } // File formats that cannot be parsed for ffmpeg (wmv9, rm, rmvb, etc.), // You can first use other tools (mencoder) to convert to avi (ffmpeg can parsed) format. private static String processAVI(int type, String inputFile) { File file = new File(Constants.avifilepath); if (file.exists()) file.delete(); List<String> comment = new ArrayList<String>(); comment.add(Constants.mencoderPath); comment.add(inputFile); comment.add("-oac"); comment.add("mp3lame"); comment.add("-lameopts"); comment.add("preset=64"); comment.add("-ovc"); comment.add("xvid"); comment.add("-xvidencopts"); comment.add("bitate=600"); comment.add("-of"); comment.add("avi"); comment.add("-o"); comment.add(Constants.avifilepath); StringBuffer test = new StringBuffer(); for (int i = 0; i < comment.size(); i++) { test.append(commend.get(i) + " "); } System.out.println(test); try { ProcessBuilder builder = new ProcessBuilder(); builder.command(commend); Process p = builder.start(); final InputStream is1 = p.getInputStream(); final InputStream is2 = p.getErrorStream(); new Thread() { public void run() { BufferedReader br = new BufferedReader( new InputStreamReader(is1)); try { String lineB = null; while ((lineB = br.readLine()) != null) { if (lineB != null) System.out.println(lineB); } } catch (IOException e) { e.printStackTrace(); } } } }.start(); new Thread() { public void run() { BufferedReader br2 = new BufferedReader( new InputStreamReader(is2)); try { String lineC = null; while ((lineC = br2.readLine()) != null) { if (lineC != null) System.out.println(lineC); } } catch (IOException e) { e.printStackTrace(); } } }.start(); // Wait for the Mencoder process to complete conversion, then call the ffmepg process p.waitFor(); System.out.println("who cares"); return Constants.avifilepath; } catch (Exception e) { System.err.println(e); return null; } } } ChangeVideo-like mainly converts video formats
package com.sino.test; /** * Constant class, mainly sets the location of executable programs, dynamic link libraries, and temporary video files generated during the conversion process* @author liuyazhuang * */ public class Constants { // path stored by ffmpeg public static final String ffmpegPath = "d://myeclipse//ffmpeg.exe"; // path stored by mencoder public static final String mencoderPath = "d://myeclipse//mencoder.exe"; // avi storage path converted through mencoder public static final String avifilepath = "d://myeclipse//temp.avi"; } Constants, which mainly sets the location of executable programs, dynamic link libraries, and temporary video files generated during the conversion process.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.