java实现利用第三方软件(ffmpeg)+avs实现视频文件的转换以及添加水印

rel="File-List" href="file:///C:%5CDOCUME%7E1%5Cnaturn%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">

通过调用第三方开源转换软件FFMPEG(version:svn-r12664)+ AviSynth(vesion:260402)对视频文件进行转换为flv;

环境配置 :(windows)下载最新版本的FFMPEG(version:svn-r12664),其包含三个 文件ffmpeg.exe,ffplay,pthreadgc2.dll;

下载AviSynth_260402并且安装;

附语:{如何检测avs是否在系统安装成功,可以新建一个test.avs内容为:

video = DirectShowSource("f:/v-m/e.rm")

路径可以是wmv文件。如果能正常运行放映代表成功;}


其他DLL文件:drv33260.dll,drv43260.dll

以上文件请存放在同一个文件夹下

方案具体操作

通过编写调用程序:ConvertVideo.java(具体代码见文章附录)调用ffmpeg;其主要的作用为将如下命令行进行执行:

Ffmpeg –i Avs文件路径 –ab 64 –acodec libmpelame –ac 2 –ar 22050 –r 24 –y 输出文件路径;

Avs文件视频文件的输入文件其具体格式如下

video = DirectShowSource("f:/v-m/e.rm")

logo = ImageSource("2.jpg")

logomask= ImageSource("1.jpg")

overlay(video,logo,mask=logomask)

下面是对a.avs内容的解释:

video为视频源文件路径;

logo为水印上层文件;

logomash为水印下层文件;

overlay()为覆盖函数;

由于最初要求中没有水印的要求,所以ConvertVideo的调用程序是只针对文件直接转换;后添加水印要求因此引入了Avs通过调用这个系统来做水印添加;从命令行的命令来说其实没有改变;只是调入的文件由视频文件变成了Avs文件;也正是调用了Avs文件使得整个调用程序需要添加对Avs文件的读写操作;以便获得对转换的视频文件,水印文件的调用控制;

总结:网上很多文章都说ffmpeg不能转换rmvb,rm,wmv9,前二者我已经测试过了最新版本的ffmpeg(version:svn-r12664)是可以转换的,最后一个wmv9没测试不好说;在添加水印方面有二中方法;vhook和avs;vhook需要ffmpege版本支持;我下载的版本是--disable-vhook,下载了很多版本都不知道vhook模式所以我就改用了avs 当然了如果你有时间也可以去svn上下载 开源的代码然后自己build;avs的关键是在于avs的编写,粗略的了解下加字幕也是可以的;最后第一次写博文有些粗糙,希望这篇文章对大家能有帮助;

附录:

convertVideo.java代码如下:

  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.util.List;   
  8. public class ConvertVideo {   
  9.     
  10.  private  String inpath;
  11.  private  String outpath;
  12.  private  String ffmpegpath;
  13.  private  String totype;
  14.  private  long totaltime;
  15.  private  int complete=0;
  16.  public static void main(String[] args) 
  17.     throws Exception{
  18.      
  19.      ConvertVideo cv=new ConvertVideo("f://v-m//d.wmv","f://v//","f://vcf//");
  20.      System.out.println(cv.start());
  21.  }   
  22.  //输入文件路径 inpath
  23.  //输出文件路径:outpath
  24.  //ffmpeg.exe存放路径
  25.  ConvertVideo(String inpath,String outpath,String ffmpegpath){
  26.      this.inpath=inpath;
  27.      this.outpath=outpath;  
  28.      this.ffmpegpath=ffmpegpath;
  29.      this.totype="flv";
  30.  }
  31.  
  32.  public boolean start() 
  33.  throws Exception{
  34.       if(!checkfile(this.inpath)){   
  35.              System.out.println(inpath+" is not file");   
  36.              return false;   
  37.             } 
  38.      //生成Avs文件
  39.       StringBuffer n=new StringBuffer();
  40.       n.append("video").append("=").append("DirectShowSource").append("(/"").append(this.inpath).append("/")").append("/r/n");
  41.       n.append("logo").append("=").append("ImageSource").append("(/"").append(this.ffmpegpath).append("2.jpg").append("/")").append("/r/n");
  42.       n.append("logomask").append("=").append("ImageSource").append("(/"").append(this.ffmpegpath).append("1.jpg").append("/")").append("/r/n");
  43.       n.append("overlay").append("(").append("video").append(",").append("logo").append(",").append("mask").append("=").append("logomask").append(")").append("/r/n");
  44.       PrintWriter out=null;       
  45.         try {
  46. //  这里设置avs文件的存放路径以及文件名
  47.             out = new PrintWriter(new File("f://vcf//a.avs").getAbsoluteFile());
  48.             out.print(n.toString());            
  49.         } finally {
  50.             out.close();
  51.         }
  52.         //
  53.       if (process()) {                    
  54.                 System.out.println("ok");
  55.                 return true;
  56.             }   
  57.      return false;
  58.  }
  59.  private  boolean process() {          
  60.   int type = checkContentType();   
  61.         boolean status = false;
  62.         if (type==0) {   
  63.             status = processFLV(inpath);// 直接将文件转为flv文件              
  64.         }
  65.         return status;   
  66.     } 
  67.  //确定转换文件的类型并且建立相应的文件路径(如:xxx.rmvb 相应的路径单位:outpath+//+rmvb+//+xxx.flv)
  68.     private int checkContentType() {   
  69.         String type = inpath.substring(inpath.lastIndexOf(".") + 1,   
  70.         inpath.length()).toLowerCase();
  71.         String fname= inpath.substring(inpath.lastIndexOf("//")+1,inpath.lastIndexOf(".")+1);
  72.         File f;
  73.         StringBuffer path=new StringBuffer(this.outpath);
  74.         path.append("//");
  75.         path.append(type);
  76.         path.append("//");        
  77.         System.out.println(path);
  78.         f=new File(path.toString());       
  79.         if(f.mkdir()){
  80.             return 9;
  81.         }
  82.         path.append(fname);
  83.         path.append(this.totype);
  84.         this.outpath=path.toString();
  85.         return 0;
  86.     }   
  87.       
  88.     private static boolean checkfile(String path){   
  89.      File file=new File(path);   
  90.      if(!file.isFile()){   
  91.       return false;   
  92.      }   
  93.      return true;   
  94.     }  
  95. //  生成转换命令行并通过进程执行
  96.     private  boolean processFLV(String oldfilepath) {   
  97.         
  98.       if(!checkfile(inpath)){   
  99.           System.out.println(oldfilepath+" is not file");   
  100.           return false;   
  101.          }
  102.       
  103.         String path=this.ffmpegpath;
  104.         path=path+"//"+"ffmpeg";        
  105.         List<String> commend=new java.util.ArrayList<String>();      
  106.         commend.add(path);
  107.         commend.add("-i");        
  108.         commend.add("f://vcf//a.avs");//avs增加水印
  109.         commend.add("-ab");   
  110.         commend.add("64");   
  111.         commend.add("-acodec");   
  112.         commend.add("libmp3lame");   
  113.         commend.add("-ac");   
  114.         commend.add("2");   
  115.         commend.add("-ar");   
  116.         commend.add("22050");         
  117.         commend.add("-r");   
  118.         commend.add("24");   
  119.         commend.add("-y");   
  120.         commend.add(this.outpath);
  121.         try {
  122.             ProcessBuilder builder = new ProcessBuilder();
  123.             builder.command(commend);
  124.             Process pc = builder.start();
  125.             String errorMsg = readInputStream(pc.getErrorStream(), "error");
  126.             String outputMsg = readInputStream(pc.getInputStream(), "out");
  127.             int c = pc.waitFor();
  128.             if (c != 0) {
  129.                 System.out.println("处理失败:" + errorMsg);
  130.             } else {// print command output
  131.                 System.out.println(this.complete);
  132.             }
  133.             return true;
  134.         } catch (Exception e) {
  135.             e.printStackTrace();
  136.             return false;
  137.         }
  138.     }
  139.     
  140.     //对进程的输出流进行监测 返回的是完成进度百分比;
  141.     private  String readInputStream(InputStream is,String f) throws IOException   
  142.     {   
  143.         BufferedReader br = new BufferedReader(new InputStreamReader(is));   
  144.       
  145.         StringBuffer lines = new StringBuffer();
  146.       
  147.         for (String line = br.readLine(); line != null; line = br.readLine())   
  148.         {   
  149.             lines.append(line);
  150.             int d=line.indexOf("Duration:");            
  151.             if(d>0){
  152.                 String dur=line.replace("Duration:""");
  153.                 dur=dur.trim();
  154.                 dur=dur.substring(0,8);             
  155.                 String time=dur.substring(0,2);             
  156.                 int h=Integer.parseInt(time);
  157.                 time=dur.substring(3,5);
  158.                 int m=Integer.parseInt(time);
  159.                 time=dur.substring(6,8);
  160.                 int s=Integer.parseInt(time);
  161.                 long len=h*3600+m*60+s;
  162.                 this.totaltime=len;
  163.                 System.out.println(len);
  164.             }
  165.             int b=line.indexOf("time=");
  166.             if(b>0){
  167.                 String time=line.substring(b,line.indexOf("bitrate")-1);
  168.                 time=time.substring(time.indexOf("=")+1,time.indexOf("."));            
  169.                 float t=(float)Long.parseLong(time)/(float)this.totaltime;                          
  170.                 t=t*100;                
  171.                 this.complete=(int)Math.ceil(t);                
  172.             }
  173.             System.out.println(line);
  174.            // System.out.println("完成:"+this.complete+"%");
  175.         }   
  176.         return lines.toString();   
  177.     }  
  178. }   

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值