欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java視頻格式轉(zhuǎn)化的實(shí)現(xiàn)方法

 更新時(shí)間:2018年02月28日 15:14:33   作者:liuyazhuang  
這篇文章主要為大家詳細(xì)介紹了Java視頻格式轉(zhuǎn)化的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java視頻格式轉(zhuǎn)化的具體代碼,供大家參考,具體內(nèi)容如下

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

廢話不大多說了,首先要把相關(guān)的庫和要轉(zhuǎn)化的視頻準(zhǔn)備好,如下圖

 

下面就是代碼部分了   

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實(shí)現(xiàn)視頻格式的轉(zhuǎn)化 
 * @author liuyazhuang 
 * 
 */ 
public class ChangeVideo { 
  public static void main(String[] args) { 
    ChangeVideo.convert("d:\\myeclipse\\aa.avi", "d:\\myeclipse\\bb.mp4"); 
  } 
 
  /** 
   * @param inputFile:需要轉(zhuǎn)換的視頻 
   * @param outputFile:轉(zhuǎn)換后的視頻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 
   * 轉(zhuǎn)換視頻文件 
   */ 
  private static boolean process(String inputFile, String outputFile) { 
    int type = checkContentType(inputFile); 
    boolean status = false; 
    if (type == 0) { 
      status = processFLV(inputFile, outputFile);// 直接將文件轉(zhuǎn)為flv文件 
    } else if (type == 1) { 
      String avifilepath = processAVI(type, inputFile); 
      if (avifilepath == null) 
        return false;// avi文件沒有得到 
      status = processFLV(avifilepath, outputFile);// 將avi轉(zhuǎn)為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)轉(zhuǎn)換為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等)直接轉(zhuǎn)換為目標(biāo)視頻 
  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"); 
    //高品質(zhì)  
    commend.add("-qscale"); 
    commend.add("6"); 
    //低品質(zhì) 
//   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)轉(zhuǎn)換為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進(jìn)程轉(zhuǎn)換結(jié)束,再調(diào)用ffmepg進(jìn)程 
      p.waitFor(); 
      System.out.println("who cares"); 
      return Constants.avifilepath; 
    } catch (Exception e) { 
      System.err.println(e); 
      return null; 
    } 
  } 
} 

 類ChangeVideo主要進(jìn)行視頻格式的轉(zhuǎn)化

package com.sino.test; 
 
/** 
 * 常量類,主要設(shè)置可執(zhí)行程序和動(dòng)態(tài)鏈接庫以及轉(zhuǎn)化過程中生成的臨時(shí)視頻文件的位置 
 * @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轉(zhuǎn)換成的avi存放路徑 
  public static final String avifilepath = "d:\\myeclipse\\temp.avi"; 
} 

常量類Constants ,主要設(shè)置可執(zhí)行程序和動(dòng)態(tài)鏈接庫以及轉(zhuǎn)化過程中生成的臨時(shí)視頻文件的位置。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • mybatis源碼解讀之executor包懶加載功能?

    mybatis源碼解讀之executor包懶加載功能?

    這篇文章主要介紹了mybatis源碼解讀之executor包懶加載功能,mybatis的懶加載的實(shí)現(xiàn)由executor包的loader子包支持,下面文章詳細(xì)內(nèi)容需要的小伙伴可以參考一下
    2022-02-02
  • SpringBoot自動(dòng)配置之自定義starter的實(shí)現(xiàn)代碼

    SpringBoot自動(dòng)配置之自定義starter的實(shí)現(xiàn)代碼

    這篇文章主要介紹了SpringBoot自動(dòng)配置之自定義starter的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Json字符串與Object、List、Map的互轉(zhuǎn)工具類

    Json字符串與Object、List、Map的互轉(zhuǎn)工具類

    今天小編就為大家分享一篇關(guān)于Json字符串與Object、List、Map的互轉(zhuǎn)工具類,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Java中多種循環(huán)Map的常見方式詳解

    Java中多種循環(huán)Map的常見方式詳解

    Java中的Map是一種鍵值對存儲(chǔ)的數(shù)據(jù)結(jié)構(gòu),其中每個(gè)鍵都唯一,與一個(gè)值相關(guān)聯(lián),下面這篇文章主要給大家介紹了關(guān)于Java中多種循環(huán)Map的常見方式,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下
    2024-01-01
  • Springboot配置過濾器實(shí)現(xiàn)過程解析

    Springboot配置過濾器實(shí)現(xiàn)過程解析

    這篇文章主要介紹了Springboot配置過濾器實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 2021年最新Redis面試題匯總(1)

    2021年最新Redis面試題匯總(1)

    在程序員面試過程中redis相關(guān)的知識(shí)是常被問到的話題。這篇文章主要介紹了幾道Redis面試題,整理一下分享給大家,感興趣的小伙伴們可以參考一下
    2021-07-07
  • SpringCloud項(xiàng)目集成Feign、Hystrix過程解析

    SpringCloud項(xiàng)目集成Feign、Hystrix過程解析

    這篇文章主要介紹了SpringCloud項(xiàng)目集成Feign、Hystrix過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • 在IDEA使用中directory和package的操作

    在IDEA使用中directory和package的操作

    這篇文章主要介紹了在IDEA使用中directory和package的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 在SpringBoot中如何利用Redis實(shí)現(xiàn)互斥鎖

    在SpringBoot中如何利用Redis實(shí)現(xiàn)互斥鎖

    當(dāng)我們利用Redis存儲(chǔ)熱點(diǎn)數(shù)據(jù)時(shí),突然就過期失效或者被刪除了,導(dǎo)致大量請求同時(shí)訪問數(shù)據(jù)庫,增加了數(shù)據(jù)庫的負(fù)載,為減輕數(shù)據(jù)庫的負(fù)載我們利用互斥鎖,本文重點(diǎn)介紹在SpringBoot中如何利用Redis實(shí)現(xiàn)互斥鎖,感興趣的朋友一起看看吧
    2023-09-09
  • MyBatis批量插入(insert)數(shù)據(jù)操作

    MyBatis批量插入(insert)數(shù)據(jù)操作

    本文給大家分享MyBatis批量插入(insert)數(shù)據(jù)操作知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧
    2016-06-06

最新評(píng)論