บทความนี้แชร์รหัสเฉพาะสำหรับการแปลงรูปแบบวิดีโอ Java สำหรับการอ้างอิงของคุณ เนื้อหาเฉพาะมีดังนี้
หลักคือการใช้ FFMPEG เพื่อแปลงวิดีโอ เราไม่ได้เขียนรหัสเพื่อแปลงวิดีโอด้วยตัวเอง แต่เพียงโทรหา FFMPEG ซึ่งจะช่วยให้เราเสร็จสิ้นการแปลงวิดีโอ ประเภทที่สนับสนุนโดย FFMPEG คือ: ASX, ASF, MPG, WMV, 3GP, MP4, MOV, AVI, FLV ฯลฯ ประเภทเหล่านี้สามารถแปลงได้โดยตรงโดยใช้ FFMPEG ประเภทที่ FFMPEG ไม่รองรับคือ: WMV9, RM, RMVB ฯลฯ ประเภทเหล่านี้จะต้องถูกแปลงเป็นรูปแบบ AVI (FFMPEG สามารถแยกวิเคราะห์ได้) รูปแบบแรกโดยใช้เครื่องมืออื่น ๆ (mencoder)
ฉันจะไม่พูดเรื่องไร้สาระมากนัก ก่อนอื่นฉันต้องเตรียมไลบรารีและวิดีโอที่เกี่ยวข้องเพื่อแปลงดังแสดงในรูปต่อไปนี้
ต่อไปนี้เป็นส่วนรหัส
แพ็คเกจ 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 * */คลาสสาธารณะ Changevideo {โมฆะสาธารณะคงที่หลัก (สตริง [] args) {changevideo.convert ("d: //myeclipse//aa.avi" } / ** * @param inputfile: วิดีโอที่ต้องแปลง * @param outputfile: วิดีโอหลังจากการแปลง w * @return * / การแปลงบูลีนแบบคงที่สาธารณะ กลับเท็จ; } if (process (inputFile, outputFile)) {system.out.println ("ตกลง"); กลับมาจริง; } return false; } // ตรวจสอบว่าไฟล์มีอยู่ในบูลีนแบบสแตติกแบบส่วนตัว (พา ธ สตริง) {ไฟล์ไฟล์ = ไฟล์ใหม่ (พา ธ ); if (! file.isfile ()) {return false; } return true; } / ** * @param inputfile * @param outputfile * @return * แปลงไฟล์วิดีโอ * / กระบวนการบูลีนแบบคงที่ส่วนตัว (สตริงอินพุต File, สตริง outputFile) {int type = checkContentType (inputFile); สถานะบูลีน = false; if (type == 0) {status = processFlv (inputfile, outputFile); // แปลงไฟล์ avi เป็น flv โดยตรง} อื่นถ้า (type == 1) {string avifilepath = processavi (type, inputfile); ถ้า (avifilePath == null) ส่งคืน false; // ไฟล์ avi ไม่ได้รับสถานะ = processFLV (avifilePath, outputFile); // แปลง avi เป็น flv} สถานะการส่งคืน; } private static int checkContentType (สตริงอินพุต file) {สตริงประเภท = inputfile.substring (inputfile.lastindexof (".") + 1, inputfile.length ()). toLowerCase (); // รูปแบบที่ FFMPEG สามารถแยกวิเคราะห์: (ASX, ASF, MPG, WMV, 3GP, MP4, MOV, AVI, FLV, ฯลฯ ) ถ้า (type.equals ("avi")) {return 0; } อื่นถ้า (type.equals ("mpg")) {return 0; } อื่นถ้า (type.equals ("wmv")) {return 0; } อื่นถ้า (type.equals ("wmv")) {return 0; } อื่นถ้า (type.equals ("wmv")) {return 0; } อื่นถ้า (type.equals ("wmv")) {return 0; } อื่นถ้า (type.equals ("wmv")) {return 0; } อื่นถ้า (type.equals ("wmv")) {return 0; } อื่นถ้า (type.equals ("wmv")) {return 0; } อื่นถ้า (type.equals ("wmv")) {return 0; } อื่นถ้า (type.equals ("wmv")) {return 0; } อื่นถ้า (type.equals ("wmv")) {return 0; } (type.equals ("3GP")) {return 0; } อื่นถ้า (type.equals ("mov")) {return 0; } อื่นถ้า (type.equals ("mp4")) {return 0; } อื่นถ้า (type.equals ("asf")) {return 0; } อื่นถ้า (type.equals ("asx")) {return 0; } อื่นถ้า (type.equals ("flv")) {return 0; } // รูปแบบไฟล์ที่ไม่สามารถแยกวิเคราะห์ได้สำหรับ FFMPEG (WMV9, RM, RMVB ฯลฯ ), // คุณสามารถใช้เครื่องมืออื่น (mencoder) เพื่อแปลงเป็นรูปแบบ AVI (FFMPEG parsable) ก่อน อื่นถ้า (type.equals ("wmv9")) {return 1; } อื่นถ้า (type.equals ("rm")) {return 1; } อื่นถ้า (type.equals ("rmvb")) {return 1; } return 9; } // รูปแบบที่ FFMPEG สามารถแยกได้: (ASX, ASF, MPG, WMV, 3GP, MP4, MOV, AVI, FLV, ฯลฯ ) จะถูกแปลงโดยตรงเป็นเป้าหมายวิดีโอแบบคงที่แบบคงที่ processFLV กลับเท็จ; } รายการ <String> comment = new ArrayList <String> (); comment.add (ค่าคงที่ fffmpegpath); 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"); // คุณภาพต่ำ /// permend.add ("-b"); //commend.add("512 "); comment.add ("-y"); comment.add (outputfile); StringBuffer Test = new StringBuffer (); สำหรับ (int i = 0; i <commentsize (); i ++) {test.append (remend.get (i)+""); } system.out.println (ทดสอบ); ลอง {ProcessBuilder Builder = new ProcessBuilder (); Builder.Command (ยกย่อง); builder.start (); กลับมาจริง; } catch (exception e) {e.printstacktrace (); กลับเท็จ; }} // รูปแบบไฟล์ที่ไม่สามารถแยกวิเคราะห์ได้สำหรับ FFMPEG (WMV9, RM, RMVB ฯลฯ ), // คุณสามารถใช้เครื่องมืออื่น ๆ (mencoder) เพื่อแปลงเป็นรูปแบบ AVI (FFMPEG สามารถแยกวิเคราะห์ได้) สตริงคงที่ส่วนตัว processavi (ประเภท int, สตริงอินพุต file) {ไฟล์ไฟล์ = ไฟล์ใหม่ (ค่าคงที่ avifilePath); if (file.exists ()) file.delete (); รายการ <String> comment = new ArrayList <String> (); comment.add (ค่าคงที่ 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 ("-จาก"); comment.add ("avi"); comment.add ("-o"); comment.add (ค่าคงที่ avifilepath); StringBuffer Test = new StringBuffer (); สำหรับ (int i = 0; i <commentsize (); i ++) {test.append (remend.get (i)+""); } system.out.println (ทดสอบ); ลอง {ProcessBuilder Builder = new ProcessBuilder (); Builder.Command (ยกย่อง); กระบวนการ p = builder.start (); อินพุตสุดท้าย IS1 = P.GetInputStream (); อินพุตสุดท้าย IS2 = P.GetERRORSTREAM (); เธรดใหม่ () {โมฆะสาธารณะเรียกใช้ () {bufferedreader br = bufferedreader ใหม่ (ใหม่ inputStreamReader (is1)); ลอง {string lineb = null; ในขณะที่ ((lineb = br.readline ())! = null) {ถ้า (lineb! = null) system.out.println (lineb); }} catch (ioexception e) {e.printstacktrace (); } } } }.เริ่ม(); เธรดใหม่ () {โมฆะสาธารณะเรียกใช้ () {bufferedreader br2 = bufferedreader ใหม่ (ใหม่ inputstreamreader (is2)); ลอง {string linec = null; ในขณะที่ ((linec = br2.readline ())! = null) {ถ้า (linec! = null) system.out.println (linec); }} catch (ioexception e) {e.printstacktrace (); } } }.เริ่ม(); // รอกระบวนการ mencoder เพื่อทำการแปลงเสร็จสิ้นจากนั้นเรียกกระบวนการ ffmepg p.waitfor (); System.out.println ("ใครสนใจ"); return constants.avifilepath; } catch (exception e) {system.err.println (e); คืนค่า null; - Changevideo ส่วนใหญ่แปลงรูปแบบวิดีโอ
แพ็คเกจ com.sino.test; /** * คลาสคงที่ส่วนใหญ่ตั้งค่าที่ตั้งของโปรแกรมปฏิบัติการไลบรารีลิงก์แบบไดนามิกและไฟล์วิดีโอชั่วคราวที่สร้างขึ้นในระหว่างกระบวนการแปลง * @author liuyazhuang * */ค่าคงที่คลาสสาธารณะ {// พา ธ ที่จัดเก็บโดย ffmpeg สตริงสุดท้ายคงที่ ffmpegpath = // พา ธ ที่จัดเก็บโดย mencoder สาธารณะคงที่สตริงสุดท้าย mencoderpath = "d: //myeclipse//mencoder.exe"; // เส้นทางการจัดเก็บ avi ที่แปลงผ่าน mencoder สาธารณะคงที่สตริงสุดท้าย avifilepath = "d: //myeclipse//temp.avi"; - ค่าคงที่ซึ่งส่วนใหญ่จะตั้งค่าตำแหน่งของโปรแกรมปฏิบัติการไลบรารีลิงก์แบบไดนามิกและไฟล์วิดีโอชั่วคราวที่สร้างขึ้นระหว่างกระบวนการแปลง
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น