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ā)過程中做的全部,希望大家多多學習,交流!
相關文章
詳解MyBatis-Puls中saveBatch批量添加慢的問題
本文主要介紹了詳解MyBatis-Puls中saveBatch批量添加慢的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01JAVA幫助文檔全系列 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文檔頁邊距的方法詳解
頁邊距是指頁面的邊線到文字的距離。通??稍陧撨吘鄡炔康目纱蛴^(qū)域中插入文字和圖形等。今天這篇文章將為您展示如何通過編程方式,設置Word?文檔頁邊距,感興趣的可以了解一下2023-02-02