Java video format conversion implementation method, java video format conversion

Source: Internet
Author: User
Tags flv file

Java video format conversion implementation method, java video format conversion

This example shares the code for converting Java video formats for your reference. The specific content is as follows:

The core is to use ffmpeg for video conversion. We do not write the code for converting the video, but call ffmpeg to convert the video. Ffmpeg supports the following types: asx, asf, mpg, wmv, 3gp, mp4, mov, avi, and flv. These types can be directly converted using ffmpeg. Ffmpeg does not support the following types: wmv9, rm, rmvb, etc. These types must be converted to avi (which can be parsed by ffmpeg) using other tools (mencoder.

You have to prepare the relevant library and the video to be converted, as shown in figure

 

The following is 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 implements video format conversion * @ author liuyazhuang **/public class ChangeVideo {public static void main (String [] args) {ChangeVideo. convert ("d :\\ myeclipse \ aa. avi "," d :\\ myeclipse \ bb.mp4 "); }/*** @ Param inputFile: the video to be converted * @ param outputFile: The converted video 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 has private static boolean checkfile (String path) {file File = new File (path ); if (! File. isFile () {return false;} return true ;} /*** @ param inputFile * @ param outputFile * @ return * convert the 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 the file directly to the flv file} else if (type = 1) {String avifilepath = processAVI (type, inputFile); if (Avifilepath = null) return false; // The 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 can be parsed by ffmpeg: (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;} // format of files that cannot be parsed by ffmpeg (wmv9, rm, rmvb, etc.). // you can use other tools (mencoder) first) convert to avi (which can be parsed by ffmpeg) format. els E if (type. equals ("wmv9") {return 1;} else if (type. equals ("rm") {return 1;} else if (type. equals ("rmvb") {return 1;} return 9;} // formats parsed by ffmpeg: (asx, asf, mpg, wmv, 3gp, mp4, mov, avi, directly convert to the 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 ("128"); commend. add ("-acodec"); commend. add ("libmp 3lame"); 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 ("512"); 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 ;}// formats of files that cannot be parsed by ffmpeg (wmv9, rm, rmvb, etc.). // you can use other tools (mencoder) convert to avi (which can be parsed by ffmpeg) format. 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. appen D (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. r EadLine ())! = 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 until the conversion of the Mencoder process ends, 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 ;}}}

ChangeVideo class converts video formats.

Package com. sino. test;/*** constant class, it mainly sets the executable program, dynamic link library, and the location of the temporary video file generated during the conversion process * @ author liuyazhuang **/public class Constants {// The path where ffmpeg is stored public static final string ffmpegPath = "d: \ myeclipse \ ffmpeg.exe "; // mencoder storage path public static final String mencoderPath =" d: \ myeclipse \ mencoder.exe "; // The avi storage path converted by mencoder is public static final String avifilepath = "d: \ myeclipse \ temp. avi ";}

Constants is a constant class that mainly sets the executable program, dynamic link library, and the location of temporary video files generated during the conversion process.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.