이 기사에서는 참조에 대한 Java 비디오 형식 변환에 대한 특정 코드를 공유합니다. 특정 내용은 다음과 같습니다
핵심은 FFMPEG를 사용하여 비디오를 변환하는 것입니다. 우리는 비디오를 직접 변환하기 위해 코드를 작성하지 않고 FFMPEG에 전화하여 비디오 변환을 완료하는 데 도움이됩니다. FFMPEG에서 지원하는 유형은 ASX, ASF, MPG, WMV, 3GP, MP4, MOV, AVI, FLV 등입니다. 이러한 유형은 FFMPEG를 사용하여 직접 변환 할 수 있습니다. FFMPEG가 지원하지 않는 유형은 WMV9, RM, RMVB 등입니다. 이러한 유형은 AVI (FFMPEG CAN) 형식으로 먼저 다른 도구 (Mencoder)를 사용하여 구문 분석해야합니다.
나는 말도 안되는 말을하지 않을 것입니다. 우선, 다음 그림과 같이 관련 라이브러리와 비디오를 변환 할 준비를해야합니다.
다음은 코드 부분입니다
패키지 com.sino.test; import java.io.bufferedReader; import java.io.file; import java.io.ioexception; import java.io.inputstream; import 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 outputfile : 변환 후 비디오 w * @return * / public static boolean convert (String inputfile, String outputfile) {if (! checkfile (inputfile)) {system.out.out.out.out.println (입력 파일 + "); 거짓을 반환합니다. } if (process (inputfile, outputfile)) {system.out.println ( "ok"); 진실을 반환하십시오. } false를 반환합니다. } // 파일이 비공개 정적 부울 체크 파일 (String Path)에 있는지 확인하십시오 {file file = 새 파일 (path); if (! file.isfile ()) {return false; } true를 반환합니다. } / ** * @param inputfile * @param outputfile * @return * 변환 비디오 파일 * / 개인 정적 부울 프로세스 (문자열 inputfile, String outputfile) {int type = checkContentType (inputfile); 부울 상태 = 거짓; if (type == 0) {status = processflv (inputfile, outputfile); // avi를 flv 파일로 직접 변환} else (type == 1) {String avifilepath = processavi (type, inputfile); if (avifilepath == null) return false; // avi 파일은 상태 = processflv (avifilepath, outputfile); // avi를 flv로 변환} reture status; } private static int checkContentType (문자열 inputfile) {문자열 유형 = inputfile.substring (inputfile.lastindexof ( ") + 1, inputfile.length ()). tolowscase (); // 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 등)에 대해 구문 분석 할 수없는 파일 형식, // 먼저 다른 도구 (mencoder)를 사용하여 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 등)는 대상 비디오 개인 정적 boolean processflv (String inputfile, string outputfile) {if (! cheeckfile (inputfile)) {inputfile.out.out.out.out.out.out.out.out. 거짓을 반환합니다. } 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"); // 고품질 주석 .add ( "-qscale"); comment.add ( "6"); // 낮은 품질 ///mend.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 (comment.get (i)+""); } system.out.println (테스트); try {processBuilder builder = new ProcessBuilder (); Builder.command (칭찬); builder.start (); 진실을 반환하십시오. } catch (예외 e) {e.printstacktrace (); 거짓을 반환합니다. }} // ffmpeg (wmv9, rm, rmvb 등)에 대해 구문 분석 할 수없는 파일 형식, // 먼저 다른 도구 (mencoder)를 사용하여 avi (ffmpeg can parsed) 형식으로 변환 할 수 있습니다. 개인 정적 문자열 processavi (int type, string inputfile) {file file = 새 파일 (constants.avifilepath); if (file.exists ()) file.delete (); List <string> 주석 = 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 (comment.get (i)+""); } system.out.println (테스트); try {processBuilder builder = new ProcessBuilder (); Builder.command (칭찬); 프로세스 p = builder.start (); 최종 입력 스트림 IS1 = p.getInputStream (); 최종 입력 스트림 IS2 = p.geterRorstream (); 새 스레드 () {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 (); } } } }.시작(); 새 스레드 () {public void run () {bufferedReader br2 = new bufferedReader (new inputStreamReader (is2)); {string linec = 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"); 반환 상수. Avifilepath; } catch (예외 e) {System.err.println (e); 널 리턴; }}} ChangeVideo와 같은 주로 비디오 형식을 변환합니다
패키지 com.sino.test; /** * Constant Class, 주로 변환 프로세스 중에 생성 된 실행 가능한 프로그램, 동적 링크 라이브러리 및 임시 비디오 파일의 위치를 설정합니다. * @Author liuyazhuang */public class constants {// 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을 더 지원하기를 바랍니다.