Java for video format conversion

Source: Internet
Author: User
Tags flv file

The core is the use of ffmpeg for video conversion, we do not write the conversion video code, just call FFmpeg, it will help us complete the video conversion. The types supported by FFmpeg are: asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv, etc., which can be converted directly using FFmpeg. FFmpeg unsupported types are: WMV9,RM,RMVB, etc., these types need to be converted to AVI (ffmpeg resolvable) format first with another tool (MEncoder).

Nonsense not mostly said, first of all to the relevant library and to convert the video ready, such as

Here's the Code section.

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 implementation of video format conversion * @author Liuyazhuang * */public class Changevideo {public static void main (string[] args) {Changevideo.conv ERT ("D:\\myeclipse\\aa.avi", "D:\\myeclipse\\bb.mp4");} /** * @param inputfile: Video required to convert * @param outputFile: Converted video W * @return */public static Boolean convert (String inputfile, Str ing 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;} Checks 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 ou tputfile) {int type = CheckcontenttyPE (inputfile); Boolean status = false;if (type = = 0) {status = processflv (Inputfile, outputFile);//convert file directly to FLV file} else if ( Type = = 1) {String Avifilepath = Processavi (type, inputfile); if (Avifilepath = = null) return false;//AVI file does not get status = Pro cessflv (Avifilepath, outputFile);//convert AVI to Flv}return status;} private static int Checkcontenttype (string inputfile) {String type = Inputfile.substring (Inputfile.lastindexof (".") + 1, Inputfile.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;} FFmpeg cannot parse the file format (WMV9,RM,RMVB, etc.),//can be converted to AVI (FFmpeg can parse) format first with another tool (mencoder). else if (type.equals ("WMV9")) {return 1 ;} Elseif (Type.equals ("rm")) {return 1;} else if (Type.equals ("RMVB")) {return 1;} return 9;} FFmpeg can parse the format: (asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv, etc.) directly into the target video private static Boolean processflv (String inputfile, String outputFile) {if (!checkfile (inputfile)) {System.out.println (Inputfile + "is not file"); return false;} List<string> commend = new arraylist<string> () Commend.add (Constants.ffmpegpath); Commend.add ("-i"); Commend.add (Inputfile), Commend.add ("-ab"), Commend.add ("n"), Commend.add ("-acodec"), Commend.add ("Libmp3lame"); Commend.add ("-ac"), Commend.add ("1"), Commend.add ("-ar"), Commend.add ("22050"), Commend.add ("-R"); Commend.add (" 29.97 ");//high-quality Commend.add ("-qscale "), Commend.add (" 6 ");//low-quality//commend.add ("-B ");//commend.add (" + "); Commend.add ("-y"); Commend.add (OutputFile); StringBuffer test = new StringBuffer (); for (int i = 0; i < commend.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;}} FFmpeg cannot parse the file format (WMV9,RM,RMVB, etc.),//can be converted to AVI (FFmpeg can parse) format first with another tool (mencoder). private static String Processavi (int Type, String inputfile) {File File = new file (Constants.avifilepath); if (File.exists ()) File.delete (); List<string> commend = new arraylist<string> (); Commend.add (Constants.mencoderpath); Commend.add ( Inputfile) Commend.add ("-oac"); Commend.add ("Mp3lame"); Commend.add ("-lameopts"); Commend.add ("preset=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 (Constants.avifilepath); StringBuffer test = new StringBuffer (); for (int i = 0; i < commend.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 Inputstreamrea Der (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 L INEC = Null;while ((Linec = Br2.readline ()) = null) {if (Linec! = null) System.out.println (LINEC);}} catch (IOException e) {e.printstacktrace ();}}}. Start ();//MEncoder process conversion end, and then call the FFMEPG process p.waitfor (); System.out.println ("Who cares"); return Constants.avifilepath;} catch (Exception e) {System.err.println (e); return null;}}}
Class Changevideo mainly for the conversion of video formats

Package com.sino.test;/** * Constant class, mainly sets the executable program and dynamic link library and the location of temporary video files generated during conversion * @author Liuyazhuang * */public class Constants {//FF MPEG stored path public static final String ffmpegpath = "D:\\myeclipse\\ffmpeg.exe";//mencoder stored path public static final string Mencoderpath = "D:\\myeclipse\\mencoder.exe";//AVI storage path through mencoder conversion to public static final String Avifilepath = "d:\\ Myeclipse\\temp.avi ";}

Constant class constants, which mainly sets the executable program and the dynamic link library, as well as the location of temporary video files generated during the conversion process


Java for video format conversion

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.