java调用ffmpeg,mencoder进行视频转换,读取时长等

以前做的一个基于ffmpeg的视频格式转换的程序,现在抽空整理一下,很多地方都是从别的大神那借鉴的,只是把自己的觉得有用的,对别人有帮助的拿出来分享分享,下面是代码

package video;

import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.DefaultFFMPEGLocator;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.EncodingAttributes;
import it.sauronsoftware.jave.FFMPEGLocator;
import it.sauronsoftware.jave.InputFormatException;
import it.sauronsoftware.jave.VideoAttributes;
import it.sauronsoftware.jave.VideoSize;

import java.io.File;
import java.util.List;

public class ConvertVideo {
 
 private static File inFolder;//需要转换的文件夹
 
 private static File outFolder;//保存avi文件的文件夹地址
 
 private static File inFile;//每个文件的file对象
 
 private static String name;//文件名称,用在保存avi文件,name.avi,不带扩展名的
 
 private static String Name;//文件名称,(带扩展名的)
 
 public ConvertVideo(File inFolder,File outFolder){//构造函数,进行测试时传入两个文件夹地址
  this.inFolder=inFolder;
  this.outFolder=outFolder;
 }
 
 public static void Test(){
  String[] fileList=inFolder.list();
  for(int i=0;i<fileList.length;i++){
   inFile=new File(inFolder.getAbsolutePath()+"\\"+fileList[i]);
   Name=inFile.getName();
   name=getFileNameNoEx(Name);//获得没有扩展名的文件名
   System.out.println(name);
   File txtFile=new File(inFolder.getAbsolutePath()+"\\"+name+".txt");//视频上传完毕会生成一个同名的txt文件
   if(txtFile.exists()){//判断是否上传完毕
    convert();
   }
  }
 }
 /**
  * 截取不带扩展名的文件名
  * @param : String
  * @return : String
  *
  */
 public static String getFileNameNoEx(String filename) {
     if ((filename != null) && (filename.length() > 0)) {
         int dot = filename.lastIndexOf('.');
         if ((dot >-1) && (dot < (filename.length()))) {
             return filename.substring(0, dot);
         }
     }
     return filename;
 }
 /**
  * 视频转换调用函数 
  *@return : void
  *
  */
 public static void convert() {
        if(!checkfile(inFile)){
         System.out.println(inFile+" is not file");
         return;
        }    
        if (process()) {    
         System.out.println("ok");
        }
 }
/**
 *视频转换函数
 * @return : boolean
 *
 *
 */
 private static boolean process() { 
 
   int type = checkContentType();
        boolean status = false;
        System.out.println("name="+name+"  "+"Name="+Name+"  "+"type="+type);
        if (type==0) {
          status = processFLV(inFile);// 直接将文件转为avi文件          
        } else if (type==1) {
            processAVI(inFile);
            status=true;
        }else if(type==2){
         System.out.println("你的文件已经是avi格式,无须转换!");
        }
        return status;
    }
 /**
  *检查视频类型
  * @param : void
  * @return : int
  *
  */
    private static int checkContentType() {
     String path=inFile.getName();//path是绝对路径
        String type = path.substring(path.lastIndexOf(".") + 1,
          path.length()).toLowerCase();
        //ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
        if (type.equals("avi")) {
            return 2;
        } else if (type.equals("mpg")) {
            return 0;
        } else if (type.equals("wmv")) {
            return 0;
        } else if (type.equals("3gp")) {
            return 0;
        } else if (type.equals("mov")) {
            return 0;
        } else if (type.equals("mp4")) {
            return 0;
        } else if (type.equals("asf")) {
            return 0;
        } else if (type.equals("asx")) {
            return 0;
        } else if (type.equals("flv")) {
            return 0;
        } else if(type.equals("mpeg")){
         return 0;
        }else if(type.equals("mkv")){
         return 0;
        }
        //对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
        else if (type.equals("wmv9")) {
            return 1;
        } else if (type.equals("rm")) {
            return 1;
        } else if (type.equals("rmvb")) {
            return 1;
        }      
        return 9;
    }
   /**
    * 检查文件是不是File对象
    * @param : File
    * @return : boolean
    *
    */
    private static boolean checkfile(File path){
    
     if(!path.isFile()){
      return false;
     }
     return true;
    }
    /**
     *对于ffmpeg无法解析的wmv9,rm,rmvb视频类型,利用mencoder进行解析,menconder视频转换
     * @param : File
     * @return : String
     *
     *
     */ 
    private static String processAVI(File inFile) {
     String inpath=inFile.getAbsolutePath();//PATH为file对象,先获得绝对路径,赋值给path
     String outpath =outFolder.getAbsolutePath()+"\\"+name+".avi";//为输出的绝对路径
     File Test=new File(outpath);
     if(Test.exists()){
      System.out.println("视频已经转换,无须再次转换!");
      return null;
     }else{
       List<String> commend=new java.util.ArrayList<String>();
             commend.add("e:\\mencoder");
             commend.add(inpath);
             commend.add("-o");
             commend.add(outpath);
             commend.add("-oac");
             commend.add("mp3lame");
             commend.add("-lameopts");
             commend.add("cbr:br=32");
             commend.add("-ovc");
             commend.add("xvid");
             commend.add("-xvidencopts");
             commend.add("bitrate=1024");
             commend.add("-vf");
             commend.add("scale=720:-3");
             //commend.add("-o");
         
             StringBuffer sb=new StringBuffer();
             for(int i=0;i<commend.size();i++){
              sb.append(commend.get(i)+" ");
             }
             try{
              ProcessBuilder builder = new ProcessBuilder();
                 builder.command(commend);
                 builder.start();
                 return "ok";
             }catch(Exception e){
              e.printStackTrace();
              return null;
             }
           
     }
      
    }
/**
 *
 * 对于ffmpeg可以解析的格式(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) ,用ffmpeg进行解析,ffmpeg视频转换
 * @param : File
 * @return : loolean
 *
 */
    private static boolean processFLV(File oldfilepath) {
     FFMPEGLocator locator = new FFMPEGLocator() {
   
   @Override
   protected String getFFMPEGExecutablePath() {
    // TODO Auto-generated method stub
    return "c://ffmpeg//ffmpeg.exe";
   }
  };
  File source = oldfilepath;
  String out=outFolder.getAbsolutePath()+"\\"+name+".avi";//输出的绝对路径
  File target=new File(out);
  //GetVideoInfo getVideoInfo = new GetVideoInfo();
  //String decoder=getVideoInfo.getDecoder(source);
  if(target.exists()/*||decoder.equals("h264")*/){
   System.out.println("视频无需进行转换!");
   return false;
  }else{
   AudioAttributes audio = new AudioAttributes();//设置音频属性
   audio.setCodec("libmp3lame");//音频使用libmp3lame编码
   audio.setBitRate(new Integer(128000));//设置比特率
   audio.setChannels(new Integer(1));//设置通道,缺省为1
   audio.setSamplingRate(new Integer(22050));//
   VideoAttributes video = new VideoAttributes();//设置视频属性
   
   video.setCodec("mpeg4");//设置编码格式
   video.setBitRate(new Integer(1024000));//设置比特率
   video.setFrameRate(new Integer(10));//设置
   /*getVideoSize getvideoSize=new getVideoSize();
   VideoInfo videoInfo=new VideoInfo();
   videoInfo=getvideoSize.getVideoInfo(source);
   VideoSize videoSize=videoInfo.getSize();
   if(videoSize.getHeight()!=1280||videoSize.getWidth()!=400){
    ResourceBundle resource=ResourceBundle.getBundle("parameter",Locale.getDefault());
    String a=resource.getString("height");
    String b=resource.getString("width");
    int height=Integer.valueOf(a);
    int width=Integer.valueOf(b);
    video.setSize(new VideoSize(width,height));//设置大小
   }*/
   //setSize set=new setSize();
   //set.set(video,source);
   video.setSize(new VideoSize(1080,720));
   EncodingAttributes attrs = new EncodingAttributes();
   attrs.setFormat("avi");
   attrs.setAudioAttributes(audio);
   attrs.setVideoAttributes(video);
   Encoder encoder = new Encoder(locator);
   try {
    encoder.encode(source, target, attrs);
    return true;
   } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return false;
   } catch (InputFormatException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return false;
   } catch (EncoderException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return false;
   }
   
  }
  
    }
 
 
}

这是一个简单的视频处理的方法,轻喷

转载于:https://my.oschina.net/u/1034481/blog/221066

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值