麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > Java > 正文

java調用ffmpeg實現轉換視頻

2024-07-14 08:43:13
字體:
來源:轉載
供稿:網友

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

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

下載相關文件

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

xuggler下載地址

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

步驟:

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

下面直接上代碼:

1.ffmpeg轉換實現

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());    }   }  }

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


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: www.99av| 亚洲第一成网站 | 九九热视频免费观看 | 国产亚洲精品久久久久久久久久 | 国产伦精品一区二区三区在线 | 精品一区二区三区中文字幕 | 国产羞羞网站 | 久久久中 | 中文字幕综合 | 黄色大片免费看 | 亚洲成人激情av | 欧美精品v国产精品v日韩精品 | 成人午夜在线免费观看 | 黄在线看 | 一级毛片真人免费播放视频 | 国产成年人视频网站 | 日日草夜夜操 | 欧美成人一区二区三区电影 | 国产精品久久久久久久模特 | 亚洲欧美国产高清 | 久久精品亚洲一区二区三区观看模式 | 色视频在线 | 久久久国产精品免费观看 | 精品国产一区二区久久 | 911色_911色sss主站色播 | 一区二区久久久久草草 | 毛片免费观看完整版 | 国产午夜免费 | 羞羞视频免费观看网站 | 一本视频在线观看 | 免费黄色欧美视频 | 成年人精品视频 | 日本一区二区高清不卡 | 中国美女一级黄色大片 | 久久久免费观看完整版 | 在线a毛片免费视频观看 | 一色桃子av大全在线播放 | 特片网久久 | 欧美zoofilia杂交videos | 一级黄色毛片a | 国产乱一区二区三区视频 |