java 實現視頻轉換通用工具類:獲取視頻元數據信息(一)
這節主要是ffmpeg的相關方法封裝,在實際調用中主要使用ffmpeg的方法,Mencoder方法暫時沒有用上,同時ffmpeg都是采用的編譯好的靜態文件。視頻轉換時沒有加額外的參數,如果有需要,可添加擴展就ok。
- /**
- * 判斷系統支持那些編碼
- * @param srcVideoPath
- * @return
- */
- public static void processFfmpegCodeFormat() {
- List<String> commend = new java.util.ArrayList<String>();
- commend.add(ffmpegPath);
- commend.add("-formats");
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- builder.redirectErrorStream(true);
- Process p= builder.start();
- BufferedReader buf = null; // 保存ffmpeg的輸出結果流
- String line = null;
- buf = new BufferedReader(new InputStreamReader(p.getInputStream()));
- StringBuffer sb= new StringBuffer();
- while ((line = buf.readLine()) != null) {
- sb.append(line + "\n");
- continue;
- }
- p.waitFor();//這里線程阻塞,將等待外部轉換進程運行成功運行結束后,才往下執行
- logger.info("【系統支持的視頻編碼】" + sb.toString());
- } catch (Exception e) {
- logger.error("獲取失敗 !");
- }
- }
2.ffmpeg將其他格式轉換成FLV格式文件(未指定其他任何參數)
- /**
- * ffmpeg將其他格式轉換成FLV格式文件(未指定其他任何參數)
- * ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
- * @param srcVideoPath 視頻文件(原)
- * @param tarVideoPath 視頻文件(新)
- * @return
- */
- public static boolean processFfmpegOther(String srcVideoPath,String tarVideoPath) {
- if (!checkfile(srcVideoPath)) {
- logger.error("【" + srcVideoPath + "】 不存在 !");
- return false;
- }
- List<String> commend = new java.util.ArrayList<String>();
- // String type =tarVideoPath.substring(tarVideoPath.lastIndexOf(".")+1, tarVideoPath.length());
- commend.add(ffmpegPath);
- commend.add( "-y");
- commend.add( "-i");
- commend.add(srcVideoPath);
- // if(type.toUpperCase().equals("MP4")){
- // commend.add( " -f h264 ");
- // }else{
- //
- // }
- commend.add(tarVideoPath);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process process = builder.start();
- doWaitFor(process);
- process.destroy();
- if (!checkfile(tarVideoPath)) {
- logger.info(tarVideoPath + " is not exit! processFfmpegOther 轉換不成功 !");
- return false;
- }
- return true;
- } catch (Exception e) {
- logger.error("【" + srcVideoPath + "】processFfmpegOther 轉換不成功 !");
- return false;
- }
- }
- /**
- * ffmpeg按照時間段進行截取
- * @param srcVideoPath 視頻文件(原)
- * @param tarImgPath 圖片文件Path
- * @return
- */
- public static boolean processFfmpegBySureTime(String srcVideoPath,String tarImgPath) {
- if (!checkfile(srcVideoPath)) {
- logger.error("【" + srcVideoPath + "】 不存在 !");
- return false;
- }
- List<String> commend = new java.util.ArrayList<String>();
- commend.add(ffmpegPath);
- commend.add("-i");
- commend.add(srcVideoPath);
- commend.add("-y"); //覆蓋
- commend.add("-f");
- commend.add("image2");
- commend.add("-ss");
- commend.add(BaseCommonUtil.STARTTIME);
- commend.add("-t");
- commend.add(BaseCommonUtil.ENDTIME);
- //commend.add("-s"); //指定圖片大小
- //commend.add("500x400");
- commend.add(tarImgPath);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process process = builder.start();
- doWaitFor(process);
- process.destroy();
- if (!checkfile(tarImgPath)) {
- logger.error("【" + srcVideoPath + "】 processFfmpegBySureTime 截圖不成功 !");
- return false;
- }
- return true;
- } catch (Exception e) {
- logger.error("【" + srcVideoPath + "】 processFfmpegOther 截圖不成功 !");
- return false;
- }
- }
4.ffmpeg按照時間段進行截取(加時間段)
- /**
- * ffmpeg按照時間段進行截取
- * ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
- * @param srcVideoPath 視頻文件(原)
- * @param tarVideoPath 視頻文件(新)
- * @param startTime 開始時間 形如:00:12:20
- * @param endTime 結束時間 形如:01:20:10
- * @return
- */
- public static boolean processFfmpegByTime(String srcVideoPath,String tarVideoPath,String startTime,String endTime) {
- if (!checkfile(srcVideoPath)) {
- logger.error("【" + srcVideoPath + "】 不存在 !");
- return false;
- }
- List<String> commend = new java.util.ArrayList<String>();
- commend.add(ffmpegPath);
- commend.add("-ss");
- if(startTime != null && !startTime.equals("")){
- commend.add(startTime);
- }else{
- commend.add(BaseCommonUtil.STARTTIME);
- }
- commend.add("-t");
- if(endTime != null && !endTime.equals("")){
- if(!calTime(getSplitStr(startTime),getSplitStr(endTime)).equals("")){
- commend.add(calTime(getSplitStr(startTime),getSplitStr(endTime)));
- }else{
- return false;
- }
- }else{
- commend.add(BaseCommonUtil.ENDTIME);
- }
- commend.add("-y"); //覆蓋
- commend.add("-i");
- commend.add(srcVideoPath);
- commend.add(tarVideoPath);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process process = builder.start();
- doWaitFor(process);
- process.destroy();
- if (!checkfile(tarVideoPath)) {
- logger.error("【" + tarVideoPath + "】processFfmpegByTime 截圖不成功 !");
- return false;
- }
- return true;
- } catch (Exception e) {
- logger.error("【" + srcVideoPath + "】 processFfmpegByTime 截圖不成功 !");
- return false;
- }
- }
5.ffmpeg將其他格式轉換成其他格式文件(未指定其他任何參數)
- /**
- * ffmpeg將其他格式轉換成其他格式文件(未指定其他任何參數)
- * ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
- * @param prefix 前綴
- * @param srcVideoPath 視頻文件(原)
- * @param middlefix 中間的字符轉
- * @param srcVideoPath 視頻文件(轉換后的路徑)
- * @param suffix 結束的字符串
- * @return
- */
- public static boolean processFfmpegShellScript(String prefix,String srcVideoPath,String middlefix,String tarVideoPath,String suffix) {
- if (!checkfile(srcVideoPath)) {
- logger.error("【" + srcVideoPath + "】 不存在!");
- return false;
- }
- List<String> commend = new java.util.ArrayList<String>();
- commend.add(ffmpegPath);
- commend.add("-y");
- commend.add("-i");
- if(prefix != null && !prefix.equals("")){
- commend.add(prefix);
- }
- commend.add(srcVideoPath);
- if(middlefix != null && !middlefix.equals("")){
- commend.add(middlefix);
- }
- commend.add(tarVideoPath);
- if(suffix != null && !suffix.equals("")){
- commend.add(suffix);
- }
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process process = builder.start();
- doWaitFor(process);
- process.destroy();
- if (!checkfile(tarVideoPath)) {
- logger.error("【" + tarVideoPath + "】 processFfmpegShellScript 轉換不成功 !");
- return false;
- }
- return true;
- } catch (Exception e) {
- logger.error("【" + srcVideoPath + "】 processFfmpegShellScript 轉換不成功 !");
- return false;
- }
- }
6.ffmpeg轉換成swf文件
- /**
- * ffmpeg轉換成swf文件
- * @param srcVideoPath
- * @param tarVideoPath
- * @return
- */
- public static boolean processFfmpegSwf(String srcVideoPath,String tarVideoPath) {
- if (!checkfile(srcVideoPath)) {
- logger.error("【" + srcVideoPath + "】不存在!");
- return false;
- }
- List<String> commend = new java.util.ArrayList<String>();
- commend.add(ffmpegPath);
- commend.add("-y");
- commend.add("-i");
- commend.add(srcVideoPath);
- commend.add("-b");
- commend.add("360");
- commend.add("-r");
- commend.add("25");
- commend.add("-s");
- commend.add("640x480");
- commend.add("-ab");
- commend.add("56");
- commend.add("-ar");
- commend.add("22050");
- commend.add("-ac");
- commend.add("1");
- commend.add(tarVideoPath);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process process = builder.start();
- doWaitFor(process);
- process.destroy();
- if (!checkfile(tarVideoPath)) {
- logger.error("【" + tarVideoPath + "】 processFfmpegSwf 轉換不成功 !");
- return false;
- }
- return true;
- } catch (Exception e) {
- logger.error("【" + srcVideoPath + "】 processFfmpegSwf 轉換不成功 !");
- return false;
- }
- }
7.ffmpeg將其他格式轉換成WEBM格式文件
- /**
- * ffmpeg將其他格式轉換成WEBM格式文件
- * ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,avi,flv等)
- * @param srcVideoPath 視頻文件(原)
- * @param tarVideoPath 視頻文件(新)
- * @return
- */
- public static boolean processFfmpegToWebm(String srcVideoPath,String tarVideoPath) {
- if (!checkfile(srcVideoPath)) {
- logger.error("【" + srcVideoPath + "】 不存在 !");
- return false;
- }
- List<String> commend = new java.util.ArrayList<String>();
- commend.add(ffmpegPath);
- commend.add( "-y");
- commend.add( "-i");
- commend.add(srcVideoPath);
- commend.add("-f");
- commend.add("webm");
- commend.add("-vcodec");
- commend.add("libvpx");
- commend.add("-acodec");
- commend.add("libvorbis");
- commend.add("-vb");
- commend.add("1600000");
- commend.add(tarVideoPath);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process process = builder.start();
- doWaitFor(process);
- process.destroy();
- if (!checkfile(tarVideoPath)) {
- logger.info(tarVideoPath + " is not exit! processFfmpegToWebm 轉換不成功 !");
- return false;
- }
- return true;
- } catch (Exception e) {
- logger.error("【" + srcVideoPath + "】processFfmpegToWebm 轉換不成功 !");
- return false;
- }
- }
8.ffmpeg將其他格式轉換成WEBM格式文件(1)
- /**
- * ffmpeg將其他格式轉換成WEBM格式文件
- * ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,avi,flv等)
- * @param srcVideoPath 視頻文件(原)
- * @param tarVideoPath 視頻文件(新)
- * @return
- */
- public static boolean processFfmpegToOggOrOgv(String srcVideoPath,String tarVideoPath) {
- if (!checkfile(srcVideoPath)) {
- logger.error("【" + srcVideoPath + "】 不存在 !");
- return false;
- }
- List<String> commend = new java.util.ArrayList<String>();
- commend.add(ffmpeg2theoraPath);
- commend.add( "-V");
- commend.add( "4000");
- commend.add("-A");
- commend.add("128");
- commend.add(srcVideoPath);
- commend.add("-o");
- commend.add(tarVideoPath);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process process = builder.start();
- doWaitFor(process);
- process.destroy();
- if (!checkfile(tarVideoPath)) {
- logger.info(tarVideoPath + " is not exit! processFfmpegToOggOrOgv 轉換不成功 !");
- return false;
- }
- return true;
- } catch (Exception e) {
- logger.error("【" + srcVideoPath + "】processFfmpegToOggOrOgv 轉換不成功 !");
- return false;
- }
- }
9.ffmpeg將其他音頻格式轉換成ogg格式文件
- /**
- * ffmpeg將其他音頻格式轉換成ogg格式文件
- * ffmpeg能解析的格式:(aac;ac3;au;wav;wma等)
- * @param srcVideoPath 音頻文件(原)
- * @param tarVideoPath 音頻文件(新)
- * @return
- */
- public static boolean processFfmpegToOgg(String srcVideoPath,String tarVideoPath) {
- if (!checkfile(srcVideoPath)) {
- logger.error("【" + srcVideoPath + "】 不存在 !");
- return false;
- }
- List<String> commend = new java.util.ArrayList<String>();
- commend.add(ffmpegPath);
- commend.add( "-i");
- commend.add(srcVideoPath);
- commend.add("-acodec");
- commend.add("libvorbis");
- commend.add("-ab");
- commend.add("64k");
- commend.add(tarVideoPath);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process process = builder.start();
- doWaitFor(process);
- process.destroy();
- if (!checkfile(tarVideoPath)) {
- logger.info(tarVideoPath + " is not exit! processFfmpegToOggOrOgv 轉換不成功 !");
- return false;
- }
- return true;
- } catch (Exception e) {
- logger.error("【" + srcVideoPath + "】processFfmpegToOggOrOgv 轉換不成功 !");
- return false;
- }
- }