zoukankan      html  css  js  c++  java
  • java 实现视频转换通用工具类:视频相互转换-总方法及Mencoder(二)

    1.自动判断格式并调用相应的转换工具,默认方法
    1. /** 
    2.      * 自动判断格式并调用相应的转换工具,默认方法 
    3.      * @param srcVideoPath 
    4.      * @param tarVideoPath 
    5.      * @return 
    6.      */  
    7.     public static boolean videoConver(String srcVideoPath,String tarVideoPath){  
    8.         boolean boo = true;  
    9.         if(StringUtils.isNotEmpty(srcVideoPath) && StringUtils.isNotEmpty(tarVideoPath)){  
    10.             srcVideoPath = BaseCommonUtil.replaceFliePathStr(srcVideoPath);  
    11.             srcVideoPath = srcVideoPath.replaceAll("//""/");  
    12.             tarVideoPath = BaseCommonUtil.replaceFliePathStr(tarVideoPath);  
    13.             tarVideoPath = tarVideoPath.replaceAll("//""/");  
    14.             String extendName = srcVideoPath.substring(srcVideoPath.lastIndexOf(".")+1,srcVideoPath.length());  
    15.             String extendTarName = tarVideoPath.substring(tarVideoPath.lastIndexOf(".")+1,tarVideoPath.length());  
    16.             //判断元资源是不是MP4格式,如果是则直接复制  
    17.             if("MP4".equals(extendName.toUpperCase())){  
    18.                 try{  
    19.                     if(!srcVideoPath.equals(tarVideoPath)){  
    20.                         copyFile(new File(srcVideoPath),new File(tarVideoPath));  
    21.                     }  
    22.                 }catch(IOException e){  
    23.                     boo=false;  
    24.                     logger.error("文件复制失败!",e);  
    25.                 }  
    26.             }else{  
    27.                 // 直接用ffmpeg转换,如果不能转换则用mencoder转换  
    28.                 boo = processFfmpegOther(srcVideoPath, tarVideoPath);  
    29.                 if (!boo) {  
    30.                     boo = processMencoderOther(srcVideoPath, tarVideoPath);  
    31.                     if (!boo) {  
    32.                         logger.error(" videoConver 暂不支持该格式!");  
    33.                         boo = false;  
    34.                     }  
    35.                 }  
    36.             }  
    37.               
    38.             if("MP4".equals(extendTarName.toUpperCase())){  
    39.                 boo = execMp4Box(tarVideoPath);  
    40.             }  
    41.         }  
    42.         return boo;  
    43.     }  


    2.Mp4Box 转换MP4

    1. /** 
    2.      * Mp4Box 转换MP4 
    3.      * @param srcPath 源MP4路径 
    4.      * @return 
    5.      */  
    6.     public static boolean execMp4Box(String srcPath){  
    7.       if (!checkfile(srcPath)) {    
    8.           logger.error("【" + srcPath + "】  不存在 !");   
    9.             return false;    
    10.         }    
    11.           
    12.         try {    
    13.             Process process = Runtime.getRuntime().exec(mp4BoxPath + " -isma " + srcPath);  
    14.             doWaitFor(process);    
    15.             process.destroy();   
    16.             if (!checkfile(srcPath)) {    
    17.               logger.error("【" + srcPath + "】 execMp4Box 转换MP4 metadata 不成功 !");  
    18.                 return false;    
    19.             }    
    20.             return true;    
    21.         } catch (Exception e) {   
    22.           logger.error("【" + srcPath + "】execMp4Box   转换MP4 metadata 不成功 !",e);  
    23.             return false;    
    24.         }    
    25.     }  


    3.判断文件的类型,从而调用不同的解析工具

     

    1. /** 
    2.    * 判断文件的类型,从而调用不同的解析工具 
    3.    * @return filePath 
    4.    */  
    5.    public static int checkContentType(String filePath) {    
    6.        String type = filePath.substring(filePath.lastIndexOf(".") + 1, filePath.length())  .toLowerCase();    
    7.          
    8.        // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)    
    9.          
    10.        if (type.equals("avi")) {    
    11.            return 0;    
    12.        } else if (type.equals("mpg")) {    
    13.            return 0;    
    14.        } else if (type.equals("wmv")) {    
    15.            return 0;    
    16.        } else if (type.equals("3gp")) {    
    17.            return 0;    
    18.        } else if (type.equals("mov")) {    
    19.            return 0;    
    20.        } else if (type.equals("mp4")) {    
    21.            return 0;    
    22.        } else if (type.equals("asf")) {    
    23.            return 0;    
    24.        }else if (type.equals("asx")) {    
    25.            return 0;    
    26.        } else if (type.equals("flv")) {    
    27.            return 0;    
    28.              
    29.        // 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),    
    30.       // 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.       
    31.              
    32.        } else if (type.equals("wmv9")) {    
    33.            return 1;    
    34.        } else if (type.equals("rm")) {    
    35.            return 1;    
    36.        } else if (type.equals("rmvb")) {   
    37.            return 1;    
    38.        } else if (type.equals("swf")) {    
    39.            return 2;    
    40.        }  
    41.        return 9;    
    42.   
    43.    }    


    4.Mencoder 将源文件转换成其他格式 

    1. /** 
    2.      * Mencoder 将源文件转换成其他格式  
    3.      * 如:wmv9,rm,rmvb 
    4.      * @param srcVideoPath 视频文件(原) 
    5.      * @param tarVideoPath 视频文件(新) 
    6.      * @return 
    7.      */  
    8.     public static boolean processMencoderOther(String srcVideoPath,String tarVideoPath) {    
    9.           if (!checkfile(srcVideoPath)) {    
    10.               logger.error("【" + srcVideoPath + "】  不存在 !");  
    11.               return false;    
    12.           }    
    13.         List<String> commend = new java.util.ArrayList<String>();    
    14.           
    15.         commend.add(mencoderPath);    
    16.   
    17.         commend.add(srcVideoPath);    
    18.   
    19.         // 音频采用mp3编码    
    20.         commend.add("-oac");    
    21.         commend.add("mp3lame");    
    22.         // 采用高质DivX视频编码,视频码率为112kbps    
    23.         commend.add("-ovc");    
    24.         commend.add("lavc");    
    25.         commend.add("-lavcopts");    
    26.         commend.add("vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:dia=-1:cmp=3:vb_strategy=1");    
    27.         commend.add("-lameopts");    
    28.         commend.add("abr:br=56");    
    29.         // 声音采样频率设置,现为22K    
    30.         commend.add("-srate");    
    31.         commend.add("22050");    
    32.         // -sws就是用来设置品质的,默认值为2    
    33.         commend.add("-sws");    
    34.         commend.add("3");    
    35.         // 宽度为208,高度自动调整保持比例;    
    36.         // -vf scale=-3:176宽度自动调整保持比例,高度为176;如果想保持原来的大小可以不要这个参数    
    37.        // commend.add("-vf");    
    38.         //commend.add("scale=512:-3");    
    39.         // 帧速率设置    
    40.         commend.add("-ofps");    
    41.         commend.add("18");    
    42.           
    43.         commend.add("-lameopts");    
    44.         commend.add("vbr=3:br=128");    
    45.         commend.add("-o");   
    46.   
    47.         commend.add(tarVideoPath);    
    48.           
    49.           
    50.         try {   
    51.              ProcessBuilder builder = new ProcessBuilder();   
    52.              builder.command(commend);   
    53.              Process process = builder.start();   
    54.              doWaitFor(process);    
    55.              process.destroy();   
    56.             if (!checkfile(tarVideoPath)) {    
    57.                 logger.info(srcVideoPath + " is not exit! processMencoderOther  转换不成功 !");  
    58.                 return false;    
    59.             }    
    60.             return true;  
    61.         } catch (Exception e) {    
    62.             logger.error("【" + srcVideoPath + "】 processMencoderOther  转换不成功 !");  
    63.             return false;  
    64.         }    
    65.     }    


    5.Mencoder 将源文件转换成其他格式 

    1. /** 
    2.      * Mencoder 将源文件转换成其他格式  
    3.      * 如:wmv9,rm,rmvb 
    4.      * @param srcVideoPath 视频文件(原) 
    5.      * @param tarVideoPath 视频文件(新) 
    6.      * @param startTime 开始时间 形如:10秒 
    7.      * @param endTime 结束时间 形如:120秒 
    8.      * @return 
    9.      */  
    10.     public static boolean processMencoderByTime(String srcVideoPath,String tarVideoPath,String startTime,String endTime) {    
    11.           if (!checkfile(srcVideoPath)) {    
    12.               logger.error("【" + srcVideoPath + "】  不存在 !");  
    13.               return false;    
    14.           }    
    15.         List<String> commend = new java.util.ArrayList<String>();    
    16.           
    17.         commend.add(mencoderPath);    
    18.   
    19.         commend.add(srcVideoPath);    
    20.   
    21.         // 音频采用mp3编码    
    22.         commend.add("-oac");    
    23.         commend.add("mp3lame");    
    24.         // 采用高质DivX视频编码,视频码率为112kbps    
    25.         commend.add("-ovc");    
    26.         commend.add("lavc");    
    27.         commend.add("-lavcopts");    
    28.         commend.add("vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:dia=-1:cmp=3:vb_strategy=1");    
    29.         commend.add("-lameopts");    
    30.         commend.add("abr:br=56");    
    31.         // 声音采样频率设置,现为22K    
    32.         commend.add("-srate");    
    33.         commend.add("22050");    
    34.         // -sws就是用来设置品质的,默认值为2    
    35.         commend.add("-sws");    
    36.         commend.add("3");    
    37.         // 宽度为208,高度自动调整保持比例;    
    38.         // -vf scale=-3:176宽度自动调整保持比例,高度为176;如果想保持原来的大小可以不要这个参数    
    39.        // commend.add("-vf");    
    40.         //commend.add("scale=512:-3");    
    41.         // 帧速率设置    
    42.         commend.add("-ofps");    
    43.         commend.add("18");    
    44.           
    45.         commend.add("-ss");    
    46.         if(startTime != null && !startTime.equals("")){  
    47.          commend.add(getSplitStr(startTime));  
    48.        }else{  
    49.          commend.add("0");//默认从开始转换  
    50.        }  
    51.          
    52.         
    53.         commend.add("-endpos");    
    54.          
    55.        if(endTime != null && !endTime.equals("")){  
    56.             commend.add((Long.parseLong(getSplitStr(endTime))-Long.parseLong(getSplitStr(startTime)))+"");  
    57.       }else{  
    58.             //VedioInfoHelps.getVideoInfo(srcVideoPath,"playingAllTime");  
    59.             commend.add("600");//默认为60秒=10分钟  
    60.       }  
    61.           
    62.         /*  
    63.          * mode=3:cbr:br=24单声道 音频码率为24kbps;-lameopts  
    64.          * mode=0:cbr:br=24立体声,音频码率为24kbps; 还可设置音量,-lameopts  
    65.          * mode=3:cbr:br=32:vol=1,设置范置为1~10,但不宜设得太高  
    66.          */    
    67.         commend.add("-lameopts");    
    68.         commend.add("vbr=3:br=128");    
    69.         commend.add("-o");   
    70.   
    71.         commend.add(tarVideoPath);    
    72.           
    73.           
    74.         try {   
    75.              ProcessBuilder builder = new ProcessBuilder();   
    76.              builder.command(commend);   
    77.              Process process = builder.start();   
    78.              doWaitFor(process);    
    79.              process.destroy();   
    80.             if (!checkfile(tarVideoPath)) {    
    81.                 logger.info(tarVideoPath + " is not exit!  processMencoderByTime 转换不成功 !");  
    82.                 return false;    
    83.             }    
    84.             return true;  
    85.         } catch (Exception e) {    
    86.             logger.error("【" + srcVideoPath + "】  processMencoderByTime 转换不成功 !",e);  
    87.             return false;  
    88.         }    
    89.     }    


    6.Mencoder 将源文件转换成其他格式 (自定义执行的shell):

    1. /** 
    2.      * Mencoder 将源文件转换成其他格式 (自定义执行的shell): 
    3.      * 例如:mencoder -i 03.flv 03.avi  
    4.      * @param prefix 前缀 
    5.      * @param srcVideoPath 视频文件(原) 
    6.      * @param middlefix 中间的字符转 
    7.      * @param srcVideoPath 视频文件(转换后的路径)  
    8.      * @param suffix 结束的字符串 
    9.      * @return 
    10.      */  
    11.     public static boolean processMencoderShellScript(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(mencoderPath);    
    20.   
    21.         if(prefix != null && !prefix.equals("")){  
    22.          commend.add(prefix);    
    23.         }  
    24.   
    25.         commend.add(srcVideoPath);    
    26.           
    27.         if(middlefix != null && !middlefix.equals("")){  
    28.          commend.add(middlefix);    
    29.         }  
    30.            
    31.         commend.add(tarVideoPath);    
    32.           
    33.         if(suffix != null && !suffix.equals("")){  
    34.          commend.add(suffix);    
    35.         }  
    36.           
    37.         try {    
    38.              ProcessBuilder builder = new ProcessBuilder();   
    39.              builder.command(commend);   
    40.              Process process = builder.start();   
    41.              doWaitFor(process);    
    42.              process.destroy();   
    43.             if (!checkfile(tarVideoPath)) {    
    44.                 logger.info(tarVideoPath + " is not exit! processMencoderShellScript 转换不成功 !");  
    45.                 return false;    
    46.             }    
    47.             return true;    
    48.         } catch (Exception e) {    
    49.             logger.error("【" + srcVideoPath + "】 processMencoderShellScript  转换不成功 !");  
    50.             return false;    
    51.         }    
    52.     }    
    更多0
  • 相关阅读:
    P1019 单词接龙
    最小生成树模板题POJ
    区间DP
    牛客多校第三场-A-PACM Team-多维背包的01变种
    洛谷P1004 方格取数-四维DP
    牛客多校第二场A run(基础DP)
    P1494 [国家集训队]小Z的袜子(莫队)
    洛谷:过河卒
    Codeforces Round #486 (Div. 3)-B. Substrings Sort
    判断的值是否为空
  • 原文地址:https://www.cnblogs.com/zhwl/p/3645590.html
Copyright © 2011-2022 走看看