国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

java調(diào)用ffmpeg實(shí)現(xiàn)轉(zhuǎn)換視頻

2024-07-14 08:43:13
字體:
供稿:網(wǎng)友

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

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

下載相關(guān)文件

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

xuggler下載地址

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

步驟:

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

下面直接上代碼:

1.ffmpeg轉(zhuǎn)換實(shí)現(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("不支持當(dāng)前上傳的文件格式");}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.調(diào)用測試類

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,需要下載對應(yīng)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ā)過程中做的全部,希望大家多多學(xué)習(xí),交流!


注:相關(guān)教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 郁南县| 紫阳县| 汶上县| 中方县| 潍坊市| 平度市| 蒙山县| 焦作市| 和硕县| 梁山县| 大新县| 珲春市| 南充市| 台南县| 蒙城县| 舞钢市| 黄平县| 合水县| 英超| 凤城市| 浙江省| 淮滨县| 土默特右旗| 邵阳市| 南涧| 新宾| 田阳县| 湖口县| 都昌县| 三明市| 佛山市| 连南| 林口县| 乐陵市| 灵山县| 德兴市| 德惠市| 余庆县| 昌都县| 韶山市| 阿合奇县|