ffmpeg的相关方法封装

来源:互联网 发布:百视通网络电视下载 编辑:程序博客网 时间:2024/05/18 03:35

转载自:http://www.cnblogs.com/zhwl/p/3645593.html

1.判断系统支持那些编码

  1. /** 
  2.      * 判断系统支持那些编码 
  3.      * @param srcVideoPath 
  4.      * @return 
  5.      */  
  6.     public static void processFfmpegCodeFormat() {    
  7.         List<String> commend = new java.util.ArrayList<String>();    
  8.           
  9.         commend.add(ffmpegPath);    
  10.           
  11.         commend.add("-formats");    
  12.           
  13.         try {    
  14.                 ProcessBuilder builder = new ProcessBuilder();    
  15.                 builder.command(commend);    
  16.                 builder.redirectErrorStream(true);    
  17.                 Process p= builder.start();    
  18.                 BufferedReader buf = null// 保存ffmpeg的输出结果流    
  19.                 String line = null;    
  20.                 buf = new BufferedReader(new InputStreamReader(p.getInputStream()));    
  21.                 StringBuffer sb= new StringBuffer();    
  22.                 while ((line = buf.readLine()) != null) {    
  23.                      sb.append(line + "\n");    
  24.                      continue;    
  25.                 }    
  26.                 p.waitFor();//这里线程阻塞,将等待外部转换进程运行成功运行结束后,才往下执行  
  27.                 logger.info("【系统支持的视频编码】" + sb.toString());  
  28.         } catch (Exception e) {    
  29.             logger.error("获取失败 !");  
  30.         }    
  31.     }    


2.ffmpeg将其他格式转换成FLV格式文件(未指定其他任何参数)

  1. /** 
  2.      * ffmpeg将其他格式转换成FLV格式文件(未指定其他任何参数) 
  3.      * ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)   
  4.      * @param srcVideoPath 视频文件(原) 
  5.      * @param tarVideoPath 视频文件(新) 
  6.      * @return 
  7.      */  
  8.     public static boolean processFfmpegOther(String srcVideoPath,String tarVideoPath) {    
  9.         if (!checkfile(srcVideoPath)) {    
  10.             logger.error("【" + srcVideoPath + "】  不存在 !");  
  11.             return false;    
  12.         }    
  13.   
  14.         List<String> commend = new java.util.ArrayList<String>();    
  15.            
  16. //        String type =tarVideoPath.substring(tarVideoPath.lastIndexOf(".")+1, tarVideoPath.length());  
  17.           
  18.         commend.add(ffmpegPath);  
  19.           
  20.         commend.add( "-y");    
  21.           
  22.         commend.add( "-i");   
  23.           
  24.         commend.add(srcVideoPath);    
  25.           
  26. //         if(type.toUpperCase().equals("MP4")){  
  27. //           commend.add( " -f h264 ");    
  28. //         }else{  
  29. //             
  30. //         }  
  31.            
  32.          commend.add(tarVideoPath);  
  33.           
  34.         try {    
  35.              ProcessBuilder builder = new ProcessBuilder();   
  36.              builder.command(commend);  
  37.              Process process = builder.start();   
  38.              doWaitFor(process);    
  39.              process.destroy();  
  40.              if (!checkfile(tarVideoPath)) {    
  41.                  logger.info(tarVideoPath + " is not exit! processFfmpegOther 转换不成功 !");  
  42.                  return false;    
  43.              }    
  44.             return true;    
  45.         } catch (Exception e) {    
  46.             logger.error("【" + srcVideoPath + "】processFfmpegOther 转换不成功 !");  
  47.             return false;    
  48.         }    
  49.     }    
 
3.ffmpeg按照时间段进行截取

  1. /** 
  2.     * ffmpeg按照时间段进行截取 
  3.     * @param srcVideoPath 视频文件(原) 
  4.     * @param tarImgPath 图片文件Path 
  5.     * @return 
  6.     */  
  7.     public static boolean processFfmpegBySureTime(String srcVideoPath,String tarImgPath) {    
  8.         if (!checkfile(srcVideoPath)) {    
  9.             logger.error("【" + srcVideoPath + "】  不存在 !");   
  10.             return false;    
  11.         }    
  12.           
  13.         List<String> commend = new java.util.ArrayList<String>();    
  14.   
  15.         commend.add(ffmpegPath);    
  16.   
  17.         commend.add("-i");    
  18.           
  19.         commend.add(srcVideoPath);  
  20.           
  21.         commend.add("-y");  //覆盖  
  22.           
  23.         commend.add("-f");    
  24.           
  25.         commend.add("image2");  
  26.   
  27.         commend.add("-ss");  
  28.           
  29.         commend.add(BaseCommonUtil.STARTTIME);  
  30.   
  31.         commend.add("-t");  
  32.           
  33.         commend.add(BaseCommonUtil.ENDTIME);  
  34.           
  35.         //commend.add("-s"); //指定图片大小  
  36.           
  37.         //commend.add("500x400");  
  38.   
  39.         commend.add(tarImgPath);    
  40.           
  41.         try {    
  42.             ProcessBuilder builder = new ProcessBuilder();   
  43.             builder.command(commend);   
  44.             Process process = builder.start();   
  45.             doWaitFor(process);    
  46.             process.destroy();   
  47.             if (!checkfile(tarImgPath)) {    
  48.                 logger.error("【" + srcVideoPath + "】 processFfmpegBySureTime  截图不成功 !");  
  49.                 return false;    
  50.             }    
  51.             return true;    
  52.         } catch (Exception e) {    
  53.             logger.error("【" + srcVideoPath + "】  processFfmpegOther  截图不成功 !");  
  54.             return false;    
  55.         }    
  56.     }    

4.ffmpeg按照时间段进行截取(加时间段)
  1. /** 
  2.      * ffmpeg按照时间段进行截取 
  3.      * ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)   
  4.      * @param srcVideoPath 视频文件(原) 
  5.      * @param tarVideoPath 视频文件(新) 
  6.      * @param startTime 开始时间 形如:00:12:20 
  7.      * @param endTime 结束时间 形如:01:20:10 
  8.      * @return 
  9.      */  
  10.      public static boolean processFfmpegByTime(String srcVideoPath,String tarVideoPath,String startTime,String endTime) {    
  11.          if (!checkfile(srcVideoPath)) {    
  12.             logger.error("【" + srcVideoPath + "】  不存在 !");   
  13.              return false;    
  14.          }    
  15.            
  16.          List<String> commend = new java.util.ArrayList<String>();    
  17.   
  18.          commend.add(ffmpegPath);    
  19.   
  20.          commend.add("-ss");  
  21.            
  22.          if(startTime != null && !startTime.equals("")){  
  23.             commend.add(startTime);  
  24.          }else{  
  25.              commend.add(BaseCommonUtil.STARTTIME);  
  26.          }  
  27.            
  28.          commend.add("-t");  
  29.            
  30.          if(endTime != null && !endTime.equals("")){  
  31.             if(!calTime(getSplitStr(startTime),getSplitStr(endTime)).equals("")){  
  32.                 commend.add(calTime(getSplitStr(startTime),getSplitStr(endTime)));  
  33.             }else{  
  34.                 return false;  
  35.             }  
  36.         }else{  
  37.                 commend.add(BaseCommonUtil.ENDTIME);  
  38.         }  
  39.            
  40.          commend.add("-y");  //覆盖  
  41.            
  42.          commend.add("-i");    
  43.   
  44.          commend.add(srcVideoPath);  
  45.   
  46.          commend.add(tarVideoPath);    
  47.            
  48.          try {    
  49.             ProcessBuilder builder = new ProcessBuilder();   
  50.              builder.command(commend);   
  51.              Process process = builder.start();   
  52.              doWaitFor(process);    
  53.              process.destroy();   
  54.              if (!checkfile(tarVideoPath)) {    
  55.                 logger.error("【" + tarVideoPath + "】processFfmpegByTime  截图不成功 !");  
  56.                  return false;    
  57.              }    
  58.              return true;    
  59.          } catch (Exception e) {    
  60.             logger.error("【" + srcVideoPath + "】 processFfmpegByTime  截图不成功 !");  
  61.              return false;    
  62.          }    
  63.      }    

5.ffmpeg将其他格式转换成其他格式文件(未指定其他任何参数)
  1. /** 
  2.       * ffmpeg将其他格式转换成其他格式文件(未指定其他任何参数) 
  3.       * ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)  
  4.       * @param prefix 前缀 
  5.       * @param srcVideoPath 视频文件(原) 
  6.       * @param middlefix 中间的字符转 
  7.       * @param srcVideoPath 视频文件(转换后的路径)  
  8.       * @param suffix 结束的字符串 
  9.       * @return 
  10.       */  
  11.      public static boolean processFfmpegShellScript(String prefix,String srcVideoPath,String middlefix,String tarVideoPath,String suffix) {    
  12.          if (!checkfile(srcVideoPath)) {    
  13.              logger.error("【" + srcVideoPath + "】  不存在!");  
  14.              return false;    
  15.          }    
  16.   
  17.          List<String> commend = new java.util.ArrayList<String>();    
  18.   
  19.          commend.add(ffmpegPath);    
  20.   
  21.          commend.add("-y");    
  22.            
  23.          commend.add("-i");    
  24.   
  25.          if(prefix != null && !prefix.equals("")){  
  26.              commend.add(prefix);    
  27.          }  
  28.   
  29.          commend.add(srcVideoPath);    
  30.            
  31.          if(middlefix != null && !middlefix.equals("")){  
  32.              commend.add(middlefix);    
  33.          }  
  34.             
  35.          commend.add(tarVideoPath);    
  36.            
  37.          if(suffix != null && !suffix.equals("")){  
  38.              commend.add(suffix);    
  39.          }  
  40.            
  41.          try {    
  42.              ProcessBuilder builder = new ProcessBuilder();   
  43.              builder.command(commend);   
  44.              Process process = builder.start();   
  45.              doWaitFor(process);    
  46.              process.destroy();   
  47.              if (!checkfile(tarVideoPath)) {    
  48.                  logger.error("【" + tarVideoPath + "】 processFfmpegShellScript  转换不成功 !");  
  49.                  return false;    
  50.              }    
  51.              return true;    
  52.          } catch (Exception e) {    
  53.              logger.error("【" + srcVideoPath + "】 processFfmpegShellScript  转换不成功 !");  
  54.              return false;    
  55.          }    
  56.      }    

6.ffmpeg转换成swf文件
  1. /** 
  2.      * ffmpeg转换成swf文件 
  3.      * @param srcVideoPath 
  4.      * @param tarVideoPath 
  5.      * @return 
  6.      */  
  7.      public static boolean processFfmpegSwf(String srcVideoPath,String tarVideoPath) {    
  8.          if (!checkfile(srcVideoPath)) {    
  9.              logger.error("【" + srcVideoPath + "】不存在!");  
  10.              return false;    
  11.          }    
  12.          List<String> commend = new java.util.ArrayList<String>();    
  13.   
  14.          commend.add(ffmpegPath);    
  15.   
  16.          commend.add("-y");    
  17.            
  18.          commend.add("-i");    
  19.   
  20.          commend.add(srcVideoPath);    
  21.            
  22.          commend.add("-b");  
  23.            
  24.          commend.add("360");  
  25.            
  26.          commend.add("-r");  
  27.            
  28.          commend.add("25");  
  29.            
  30.          commend.add("-s");  
  31.            
  32.          commend.add("640x480");  
  33.            
  34.          commend.add("-ab");  
  35.            
  36.          commend.add("56");  
  37.            
  38.          commend.add("-ar");  
  39.            
  40.          commend.add("22050");  
  41.            
  42.          commend.add("-ac");  
  43.            
  44.          commend.add("1");  
  45.   
  46.          commend.add(tarVideoPath);    
  47.   
  48.          try {    
  49.              ProcessBuilder builder = new ProcessBuilder();   
  50.              builder.command(commend);   
  51.              Process process = builder.start();   
  52.              doWaitFor(process);    
  53.              process.destroy();   
  54.              if (!checkfile(tarVideoPath)) {    
  55.                  logger.error("【" + tarVideoPath + "】 processFfmpegSwf  转换不成功 !");  
  56.                  return false;    
  57.              }    
  58.              return true;    
  59.          } catch (Exception e) {    
  60.              logger.error("【" + srcVideoPath + "】 processFfmpegSwf  转换不成功 !");  
  61.              return false;    
  62.          }    
  63.      }    

7.ffmpeg将其他格式转换成WEBM格式文件
  1. /** 
  2.      * ffmpeg将其他格式转换成WEBM格式文件 
  3.      * ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,avi,flv等)   
  4.      * @param srcVideoPath 视频文件(原) 
  5.      * @param tarVideoPath 视频文件(新) 
  6.      * @return 
  7.      */  
  8.     public static boolean processFfmpegToWebm(String srcVideoPath,String tarVideoPath) {    
  9.         if (!checkfile(srcVideoPath)) {    
  10.             logger.error("【" + srcVideoPath + "】  不存在 !");  
  11.             return false;    
  12.         }    
  13.   
  14.         List<String> commend = new java.util.ArrayList<String>();    
  15.            
  16.         commend.add(ffmpegPath);  
  17.           
  18.         commend.add( "-y");    
  19.           
  20.         commend.add( "-i");   
  21.           
  22.         commend.add(srcVideoPath);    
  23.          commend.add("-f");  
  24.          commend.add("webm");  
  25.          commend.add("-vcodec");  
  26.          commend.add("libvpx");  
  27.          commend.add("-acodec");  
  28.          commend.add("libvorbis");  
  29.          commend.add("-vb");  
  30.          commend.add("1600000");  
  31.          commend.add(tarVideoPath);  
  32.           
  33.         try {    
  34.              ProcessBuilder builder = new ProcessBuilder();   
  35.              builder.command(commend);  
  36.              Process process = builder.start();   
  37.              doWaitFor(process);    
  38.              process.destroy();  
  39.              if (!checkfile(tarVideoPath)) {    
  40.                  logger.info(tarVideoPath + " is not exit! processFfmpegToWebm 转换不成功 !");  
  41.                  return false;    
  42.              }    
  43.             return true;    
  44.         } catch (Exception e) {    
  45.             logger.error("【" + srcVideoPath + "】processFfmpegToWebm 转换不成功 !");  
  46.             return false;    
  47.         }    
  48.     }    

8.ffmpeg将其他格式转换成WEBM格式文件(1)
  1. /** 
  2.      * ffmpeg将其他格式转换成WEBM格式文件 
  3.      * ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,avi,flv等)   
  4.      * @param srcVideoPath 视频文件(原) 
  5.      * @param tarVideoPath 视频文件(新) 
  6.      * @return 
  7.      */  
  8.     public static boolean processFfmpegToOggOrOgv(String srcVideoPath,String tarVideoPath) {    
  9.         if (!checkfile(srcVideoPath)) {    
  10.             logger.error("【" + srcVideoPath + "】  不存在 !");  
  11.             return false;    
  12.         }    
  13.   
  14.         List<String> commend = new java.util.ArrayList<String>();    
  15.            
  16.         commend.add(ffmpeg2theoraPath);  
  17.           
  18.         commend.add( "-V");    
  19.           
  20.         commend.add( "4000");   
  21.         commend.add("-A");  
  22.         commend.add("128");  
  23.         commend.add(srcVideoPath);    
  24.          commend.add("-o");  
  25.          commend.add(tarVideoPath);  
  26.           
  27.         try {    
  28.              ProcessBuilder builder = new ProcessBuilder();   
  29.              builder.command(commend);  
  30.              Process process = builder.start();   
  31.              doWaitFor(process);    
  32.              process.destroy();  
  33.              if (!checkfile(tarVideoPath)) {    
  34.                  logger.info(tarVideoPath + " is not exit! processFfmpegToOggOrOgv 转换不成功 !");  
  35.                  return false;    
  36.              }    
  37.             return true;    
  38.         } catch (Exception e) {    
  39.             logger.error("【" + srcVideoPath + "】processFfmpegToOggOrOgv 转换不成功 !");  
  40.             return false;    
  41.         }    
  42.     }    


9.ffmpeg将其他音频格式转换成ogg格式文件
  1. /** 
  2.      * ffmpeg将其他音频格式转换成ogg格式文件 
  3.      * ffmpeg能解析的格式:(aac;ac3;au;wav;wma等)   
  4.      * @param srcVideoPath 音频文件(原) 
  5.      * @param tarVideoPath 音频文件(新) 
  6.      * @return 
  7.      */  
  8.     public static boolean processFfmpegToOgg(String srcVideoPath,String tarVideoPath) {    
  9.         if (!checkfile(srcVideoPath)) {    
  10.             logger.error("【" + srcVideoPath + "】  不存在 !");  
  11.             return false;    
  12.         }    
  13.   
  14.         List<String> commend = new java.util.ArrayList<String>();    
  15.            
  16.         commend.add(ffmpegPath);  
  17.           
  18.         commend.add( "-i");    
  19.           
  20.         commend.add(srcVideoPath);    
  21.         commend.add("-acodec");  
  22.         commend.add("libvorbis");  
  23.         commend.add("-ab");  
  24.          commend.add("64k");  
  25.          commend.add(tarVideoPath);  
  26.           
  27.         try {    
  28.              ProcessBuilder builder = new ProcessBuilder();   
  29.              builder.command(commend);  
  30.              Process process = builder.start();   
  31.              doWaitFor(process);    
  32.              process.destroy();  
  33.              if (!checkfile(tarVideoPath)) {    
  34.                  logger.info(tarVideoPath + " is not exit! processFfmpegToOggOrOgv 转换不成功 !");  
  35.                  return false;    
  36.              }    
  37.             return true;    
  38.         } catch (Exception e) {    
  39.             logger.error("【" + srcVideoPath + "】processFfmpegToOggOrOgv 转换不成功 !");  
  40.             return false;    
  41.         }    
  42.     }    
原创粉丝点击