Java視頻格式轉化的實現方法,java視頻格式轉化

來源:互聯網
上載者:User

Java視頻格式轉化的實現方法,java視頻格式轉化

本文執行個體為大家分享了Java視頻格式轉化的具體代碼,供大家參考,具體內容如下

核心是利用ffmpeg進行視頻轉換,我們自己並不寫轉換視頻的代碼,只是調用ffmpeg,它會幫我們完成視頻的轉換。ffmpeg支援的類型有:asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等,這些類型,可以利用ffmpeg進行直接轉換。ffmpeg不支援的類型有:wmv9,rm,rmvb等,這些類型需要先用別的工具(mencoder)轉換為avi(ffmpeg能解析的)格式。

廢話不大多說了,首先要把相關的庫和要轉化的視頻準備好,如

 

下面就是代碼部分了   

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實現視頻格式的轉化  * @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.println(inputFile + " is nokt file");       return false;     }     if (process(inputFile, outputFile)) {       System.out.println("ok");       return true;     }     return false;   }    // 檢查檔案是否存在   private static boolean checkfile(String path) {     File file = new File(path);     if (!file.isFile()) {       return false;     }     return true;   }    /**    * @param inputFile    * @param outputFile    * @return    * 轉換視頻檔案    */   private static boolean process(String inputFile, String outputFile) {     int type = checkContentType(inputFile);     boolean status = false;     if (type == 0) {       status = processFLV(inputFile, outputFile);// 直接將檔案轉為flv檔案     } else if (type == 1) {       String avifilepath = processAVI(type, inputFile);       if (avifilepath == null)         return false;// avi檔案沒有得到       status = processFLV(avifilepath, outputFile);// 將avi轉為flv     }     return status;   }    private static int checkContentType(String inputFile) {     String type = 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("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能解析的)格式.     else if (type.equals("wmv9")) {       return 1;     } else if (type.equals("rm")) {       return 1;     } else if (type.equals("rmvb")) {       return 1;     }     return 9;   }   // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)直接轉換為目標視頻   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("libmp3lame");     commend.add("-ac");     commend.add("1");     commend.add("-ar");     commend.add("22050");     commend.add("-r");     commend.add("29.97");     //高品質      commend.add("-qscale");     commend.add("6");     //低品質 //   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;     }   }   // 對ffmpeg無法解析的檔案格式(wmv9,rm,rmvb等),   // 可以先用別的工具(mencoder)轉換為avi(ffmpeg能解析的)格式.   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 InputStreamReader(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 lineC = null;             while ((lineC = br2.readLine()) != null) {               if (lineC != null)                 System.out.println(lineC);             }           } catch (IOException e) {             e.printStackTrace();           }         }       }.start();        // 等Mencoder進程轉換結束,再調用ffmepg進程       p.waitFor();       System.out.println("who cares");       return Constants.avifilepath;     } catch (Exception e) {       System.err.println(e);       return null;     }   } } 

 類ChangeVideo主要進行視頻格式的轉化

package com.sino.test;  /**  * 常量類,主要設定可執行程式和動態連結程式庫以及轉化過程中產生的臨時視頻檔案的位置  * @author liuyazhuang  *  */ public class Constants {   //ffmpeg存放的路徑   public static final String 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"; } 

常量類Constants ,主要設定可執行程式和動態連結程式庫以及轉化過程中產生的臨時視頻檔案的位置。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.