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

java調用ffmpeg實現(xiàn)轉換視頻

 更新時間:2018年12月15日 15:07:12   作者:zhengdesheng19930211  
這篇文章主要為大家詳細介紹了java調用ffmpeg實現(xiàn)轉換視頻功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近由于項目需要把不同格式的視頻轉換為ts流,故研究了一下ffmpeg。在網上找了很多資料,主要參考了Java+Windows+ffmpeg實現(xiàn)視頻轉換功能。

期間也加了幾個qq群,咨詢了各大高手,其中在代碼中關于ffmpeg的命令就是來自其中一個qq群里面的大神。

下載相關文件

ffmpeg地址,我下載是windows 64位static版本。

xuggler下載地址

下面的代碼我上傳到了github,需要的可以下載下來看看。

步驟:

1.研究java如何調用外部程序
2.研究ffmpeg轉換視頻格式的命令
3.利用xuggle獲取ffmpeg解析的ts流的時長、分辨率以及文件大小。

下面直接上代碼:

1.ffmpeg轉換實現(xiàn)

package vedio.ffmpeg;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
public class FfmpegUtil {
 
public static Boolean ffmpeg(StringffmpegPath, String inputPath, String outputPath) throwsFFmpegException{
 
if (!checkfile(inputPath)) {
throw newFFmpegException("文件格式不合法");
}
 
int type =checkContentType(inputPath);
List command = getFfmpegCommand(type,ffmpegPath, inputPath, outputPath);
if (null != command &&command.size() > 0) {
return process(command);
 
}
return false;
}
 
private static int checkContentType(StringinputPath) {
String type =inputPath.substring(inputPath.lastIndexOf(".") + 1,inputPath.length()).toLowerCase();
//ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
if (type.equals("avi")) {
return 1;
} else if (type.equals("mpg")){
return 1;
} else if (type.equals("wmv")){
return 1;
} else if (type.equals("3gp")){
return 1;
} else if (type.equals("mov")){
return 1;
} else if (type.equals("mp4")){
return 1;
} else if(type.equals("mkv")){
return 1;
}else if (type.equals("asf")){
return 0;
} else if (type.equals("flv")){
return 0;
}else if (type.equals("rm")){
return 0;
} else if (type.equals("rmvb")){
return 1;
}
return 9;
}
 
private static boolean checkfile(Stringpath) {
File file = new File(path);
if (!file.isFile()) {
return false;
}
return true;
}
 
private static boolean process(Listcommand) throws FFmpegException{
 
try {
 
if (null == command || command.size() ==0)
return false;
Process videoProcess = newProcessBuilder(command).redirectErrorStream(true).start();
 
newPrintStream(videoProcess.getErrorStream()).start();
 
newPrintStream(videoProcess.getInputStream()).start();
 
int exitcode =videoProcess.waitFor();
 
if (exitcode == 1) {
return false;
}
return true;
} catch (Exception e) {
throw new FFmpegException("file uploadfailed",e);
}
 
}
 
private static List getFfmpegCommand(inttype, String ffmpegPath, String oldfilepath, String outputPath)throws FFmpegException {
List command = newArrayList();
if (type == 1) {
command.add(ffmpegPath +"\\ffmpeg");
command.add("-i");
command.add(oldfilepath);
command.add("-c:v");
command.add("libx264");
command.add("-x264opts");
command.add("force-cfr=1");
command.add("-c:a");
command.add("mp2");
command.add("-b:a");
command.add("256k");
command.add("-vsync");
command.add("cfr");
command.add("-f");
command.add("mpegts");
command.add(outputPath);
} else if(type==0){
command.add(ffmpegPath +"\\ffmpeg");
command.add("-i");
command.add(oldfilepath);
command.add("-c:v");
command.add("libx264");
command.add("-x264opts");
command.add("force-cfr=1");
command.add("-vsync");
command.add("cfr");
command.add("-vf");
command.add("idet,yadif=deint=interlaced");
command.add("-filter_complex");
command.add("aresample=async=1000");
command.add("-c:a");
command.add("libmp3lame");
command.add("-b:a");
command.add("192k");
command.add("-pix_fmt");
command.add("yuv420p");
command.add("-f");
command.add("mpegts");
command.add(outputPath);
}else{
throw newFFmpegException("不支持當前上傳的文件格式");
}
return command;
}
}
 
class PrintStream extends Thread{
java.io.InputStream __is =null;
 
public PrintStream(java.io.InputStream is){
__is = is;
}
 
public void run() {
try {
while (this != null) {
int _ch = __is.read();
if (_ch == -1) {
break;
} else {
System.out.print((char) _ch);
}
 
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

2.調用測試類

package vedio.ffmpeg;
 
public class ConvertVedio {
public static void convertVedio(StringinputPath){
String ffmpegPath =getFfmpegPath();
String outputPath =getOutputPath(inputPath);
try {
FfmpegUtil.ffmpeg(ffmpegPath, inputPath,outputPath);
} catch (FFmpegException e) {
e.printStackTrace();
}
 
}
 
private static String getFfmpegPath(){
return "ffmpeg";
}
 
private static String getOutputPath(StringinputPath) {
return inputPath.substring(0,inputPath.lastIndexOf(".")).toLowerCase() + ".ts";
}
}

3.自定義的異常類

package vedio.ffmpeg;
 
public class FFmpegException extendsException {
 
private static final long serialVersionUID= 1L;
 
public FFmpegException() {
super();
}
 
public FFmpegException(String message){
super(message);
}
 
public FFmpegException(Throwable cause){
super(cause);
}
 
public FFmpegException(String message,Throwable cause) {
super(message, cause);
}
}

4.獲取ts流的時長、大小以及分辨率(用到了Xuggle,需要下載對應jar包)

importcom.xuggle.xuggler.ICodec;
importcom.xuggle.xuggler.IContainer;
importcom.xuggle.xuggler.IStream;
importcom.xuggle.xuggler.IStreamCoder;
 
*/
 public static void getVedioInfo(String filename){
 
 
   // first we create a Xuggler containerobject
   IContainer container =IContainer.make();
 
   // we attempt to open up thecontainer
   int result = container.open(filename,IContainer.Type.READ, null);
 
   // check if the operation wassuccessful
   if (result<0)
    return;
   
   // query how many streams the call to openfound
   int numStreams =container.getNumStreams();
   // query for the total duration
   long duration =container.getDuration();
   // query for the file size
   long fileSize =container.getFileSize();
   long secondDuration =duration/1000000;
   
   System.out.println("時長:"+secondDuration+"秒");
   System.out.println("文件大小:"+fileSize+"M");
  
  
   for (int i=0; i
    IStreamstream = container.getStream(i);
    IStreamCoder coder = stream.getStreamCoder();
    if(coder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO){
    System.out.println("視頻寬度:"+coder.getWidth());
     System.out.println("視頻高度:"+coder.getHeight());
    }
   }
 
 }

以上就是在開發(fā)過程中做的全部,希望大家多多學習,交流!

相關文章

  • Java 正確終止線程的方法

    Java 正確終止線程的方法

    這篇文章主要介紹了Java 正確終止線程的方法,幫助大家更好的理解和學習java 多線程的相關知識,感興趣的朋友可以了解下
    2020-12-12
  • java實現(xiàn)五子棋大戰(zhàn)

    java實現(xiàn)五子棋大戰(zhàn)

    這篇文章主要為大家詳細介紹了java實現(xiàn)五子棋大戰(zhàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • springboot省去配置Tomcat的步驟問題

    springboot省去配置Tomcat的步驟問題

    這篇文章主要介紹了springboot省去配置Tomcat的步驟問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • maven手動上傳jar包示例及圖文步驟過程

    maven手動上傳jar包示例及圖文步驟過程

    這篇文章主要為大家介紹了maven手動上傳jar包示例及圖文步驟過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-03-03
  • Java?數(shù)據結構與算法系列精講之漢諾塔

    Java?數(shù)據結構與算法系列精講之漢諾塔

    漢諾塔是源于印度一個古老傳說的益智玩具。大梵天創(chuàng)造世界時做了三根石柱,在一根柱子上從下往上按大小順序摞著64片黃金圓盤。大梵天命令婆羅門把圓盤從下面開始按大小順序重新擺放在另一根柱子上。并且規(guī)定,在小圓盤上不能放大圓盤,三根柱子之間一次只能移動一個圓盤
    2022-02-02
  • 詳解MyBatis-Puls中saveBatch批量添加慢的問題

    詳解MyBatis-Puls中saveBatch批量添加慢的問題

    本文主要介紹了詳解MyBatis-Puls中saveBatch批量添加慢的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-01-01
  • JAVA幫助文檔全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版整理

    JAVA幫助文檔全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版整理

    JDK(Java Development Kit,Java開發(fā)包,Java開發(fā)工具)是一個寫Java的applet和應用程序的程序開發(fā)環(huán)境。它由一個處于操作系統(tǒng)層之上的運行環(huán)境還有開發(fā)者編譯,調試和運行用Java語言寫的applet和應用程序所需的工具組成
    2014-01-01
  • 通過Java實現(xiàn)設置Word文檔頁邊距的方法詳解

    通過Java實現(xiàn)設置Word文檔頁邊距的方法詳解

    頁邊距是指頁面的邊線到文字的距離。通??稍陧撨吘鄡炔康目纱蛴^(qū)域中插入文字和圖形等。今天這篇文章將為您展示如何通過編程方式,設置Word?文檔頁邊距,感興趣的可以了解一下
    2023-02-02
  • IDEA如何實現(xiàn)顯示類的所有方法

    IDEA如何實現(xiàn)顯示類的所有方法

    這篇文章主要介紹了IDEA如何顯示類的所有方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • IDEA中Maven依賴下載失敗的完美解決方案

    IDEA中Maven依賴下載失敗的完美解決方案

    使用IDEA進行Maven項目開發(fā)時,時不時會遇到pom.xml報錯的情況,其中很大概率是因為Maven依賴的jar包下載失敗,找來找去也沒有找到是什么問題,困擾了很多程序猿,這里給出IDEA中Maven依賴下載失敗解決方案,給大家參考,實測有用
    2020-04-04

最新評論