この記事では、参照用のJavaビデオ形式の変換の特定のコードを共有しています。特定のコンテンツは次のとおりです
コアは、FFMPEGを使用してビデオを変換することです。自分でビデオを変換するためのコードを書くのではなく、FFMPEGに電話するだけで、ビデオ変換を完了するのに役立ちます。 FFMPEGでサポートされるタイプは、ASX、ASF、MPG、WMV、3GP、MP4、MOV、AVI、FLVなどです。これらのタイプは、FFMPEGを使用して直接変換できます。 FFMPEGがサポートしていないタイプは次のとおりです。WMV9、RM、RMVBなど。これらのタイプは、最初に他のツール(メンコーダー)を使用してAVI(FFMPEGが解析できます)形式に変換する必要があります。
ナンセンスはあまり言いません。まず第一に、次の図に示すように、変換する関連ライブラリとビデオを準備する必要があります
以下はコード部分です
パッケージcom.sino.test; java.io.bufferedreaderをインポートします。 java.io.fileをインポートします。 java.io.ioexceptionをインポートします。 java.io.inputStreamをインポートします。 java.io.inputStreamReaderをインポートします。 java.util.arraylistをインポートします。 java.util.listをインポートします。 /** * Javaはビデオ形式の変換を実装します * @author liuyazhuang * */public class changevideo {public static void main(string [] args){changevideo.convert( "d://myeclipse/aa.avi"、 "d://myeclipse//bb.mp4"); } / ** * @param inputfile:変換する必要があるビデオ * @param出力ファイル:変換後のビデオfalseを返します。 } if(process(inputFile、outputFile)){system.out.println( "ok"); trueを返します。 } falseを返します。 } //ファイルがprivate static boolean checkfile(string path){file file = new file(path); if(!file.isfile()){return false; } 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); // aviをflvファイルに直接変換} else(type == 1){string avifilepath = processavi(type、inputfile); if(avifilepath == null)false; // aviファイルはステータス= processflv(avifilepath、outputfile); // aviをflvに変換} return statusを取得しません。 } private static int checkContentType(string inputFile){string input = inputFile.substring(inputFile.lastIndexof( "。") + 1、inputfile.length())。tolowercase(); // ffmpegが解析できるフォーマット:(asx、asf、mpg、wmv、3gp、mp4、mov、avi、flvなど)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; } // ffmpeg(wmv9、rm、rmvbなど)に解析できないファイル形式、//最初に別のツール(メンコーダー)を使用して、AVI(ffmpeg parsable)形式に変換できます。 else if(type.equals( "wmv9")){return 1; } else if(type.equals( "rm")){return 1; } else if(type.equals( "rmvb")){return 1; } 9を返します。 } // ffmpeg parsable:(asx、asf、mpg、wmv、3gp、mp4、mov、avi、flvなど)の形式は、ターゲットビデオ入力file、string outputfile){if(!checkfile){inputfile){system.out.putfile + " falseを返します。 } list <string>コメント= 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"); //高品質のcomment.add( "-qscale"); comment.add( "6"); //低品質/// 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(称賛); builder.start(); trueを返します。 } catch(Exception e){e.printstacktrace(); falseを返します。 }} // ffmpeg(wmv9、rm、rmvbなど)では解析できないファイル形式、//最初に他のツール(メンコーダー)を使用してAVI(ffmpeg can parsed)形式に変換できます。 private static string processavi(int type、string inputfile){file file = new file(constants.avifilepath); if(file.exists())file.delete();リスト<文字列>コメント= 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(称賛);プロセスp = builder.start(); final inputstream is1 = p.getinputStream(); final inputstream is2 = p.geterrorStream(); new shood(){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(); } } } }。始める(); new shood(){public void run(){bufferedreader br2 = new BufferedReader(new inputStreamReader(IS2)); try {stringlinec = null; while((linec = br2.readline())!= null){if(linec!= null)system.out.println(linec); }} catch(ioexception e){e.printstacktrace(); } } }。始める(); // Mencoderプロセスが変換を完了するのを待ち、次にFFMEPGプロセスp.waitfor()を呼び出します。 system.out.println( "who cares"); returnantants.avifilepath; } catch(Exception e){System.err.println(e); nullを返します。 }}} ChangeVideoのように、主にビデオ形式を変換します
パッケージcom.sino.test; /** *一定のクラスは、主に実行可能なプログラム、動的リンクライブラリ、および変換プロセス中に生成された一時的なビデオファイルの場所を設定します * @author liuyazhuang * * * //パブリッククラス定数{// ffmpegパブリック最終文字列ffmpegpath = "d://myeclipse//ffmpeg.exe"; // Mencoderによって保存されたパスPublic Static Final String MencoderPath = "d://myeclipse//mencoder.exe"; // Mencoderを介して変換されたAVIストレージパスpublic static final string avifilepath = "d://myeclipse//temp.avi"; }主に実行可能なプログラム、動的リンクライブラリ、および変換プロセス中に生成された一時的なビデオファイルの場所を設定する定数。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。