通过调用第三方开源转换软件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代码如下:
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.util.List;
- public class ConvertVideo {
- private String inpath;
- private String outpath;
- private String ffmpegpath;
- private String totype;
- private long totaltime;
- private int complete=0;
- public static void main(String[] args)
- throws Exception{
- ConvertVideo cv=new ConvertVideo("f://v-m//d.wmv","f://v//","f://vcf//");
- System.out.println(cv.start());
- }
- //输入文件路径 inpath
- //输出文件路径:outpath
- //ffmpeg.exe存放路径
- ConvertVideo(String inpath,String outpath,String ffmpegpath){
- this.inpath=inpath;
- this.outpath=outpath;
- this.ffmpegpath=ffmpegpath;
- this.totype="flv";
- }
- public boolean start()
- throws Exception{
- if(!checkfile(this.inpath)){
- System.out.println(inpath+" is not file");
- return false;
- }
- //生成Avs文件
- StringBuffer n=new StringBuffer();
- n.append("video").append("=").append("DirectShowSource").append("(/"").append(this.inpath).append("/")").append("/r/n");
- n.append("logo").append("=").append("ImageSource").append("(/"").append(this.ffmpegpath).append("2.jpg").append("/")").append("/r/n");
- n.append("logomask").append("=").append("ImageSource").append("(/"").append(this.ffmpegpath).append("1.jpg").append("/")").append("/r/n");
- n.append("overlay").append("(").append("video").append(",").append("logo").append(",").append("mask").append("=").append("logomask").append(")").append("/r/n");
- PrintWriter out=null;
- try {
- // 这里设置avs文件的存放路径以及文件名
- out = new PrintWriter(new File("f://vcf//a.avs").getAbsoluteFile());
- out.print(n.toString());
- } finally {
- out.close();
- }
- //
- if (process()) {
- System.out.println("ok");
- return true;
- }
- return false;
- }
- private boolean process() {
- int type = checkContentType();
- boolean status = false;
- if (type==0) {
- status = processFLV(inpath);// 直接将文件转为flv文件
- }
- return status;
- }
- //确定转换文件的类型并且建立相应的文件路径(如:xxx.rmvb 相应的路径单位:outpath+//+rmvb+//+xxx.flv)
- private int checkContentType() {
- String type = inpath.substring(inpath.lastIndexOf(".") + 1,
- inpath.length()).toLowerCase();
- String fname= inpath.substring(inpath.lastIndexOf("//")+1,inpath.lastIndexOf(".")+1);
- File f;
- StringBuffer path=new StringBuffer(this.outpath);
- path.append("//");
- path.append(type);
- path.append("//");
- System.out.println(path);
- f=new File(path.toString());
- if(f.mkdir()){
- return 9;
- }
- path.append(fname);
- path.append(this.totype);
- this.outpath=path.toString();
- return 0;
- }
- private static boolean checkfile(String path){
- File file=new File(path);
- if(!file.isFile()){
- return false;
- }
- return true;
- }
- // 生成转换命令行并通过进程执行
- private boolean processFLV(String oldfilepath) {
- if(!checkfile(inpath)){
- System.out.println(oldfilepath+" is not file");
- return false;
- }
- String path=this.ffmpegpath;
- path=path+"//"+"ffmpeg";
- List<String> commend=new java.util.ArrayList<String>();
- commend.add(path);
- commend.add("-i");
- commend.add("f://vcf//a.avs");//avs增加水印
- commend.add("-ab");
- commend.add("64");
- commend.add("-acodec");
- commend.add("libmp3lame");
- commend.add("-ac");
- commend.add("2");
- commend.add("-ar");
- commend.add("22050");
- commend.add("-r");
- commend.add("24");
- commend.add("-y");
- commend.add(this.outpath);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process pc = builder.start();
- String errorMsg = readInputStream(pc.getErrorStream(), "error");
- String outputMsg = readInputStream(pc.getInputStream(), "out");
- int c = pc.waitFor();
- if (c != 0) {
- System.out.println("处理失败:" + errorMsg);
- } else {// print command output
- System.out.println(this.complete);
- }
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- //对进程的输出流进行监测 返回的是完成进度百分比;
- private String readInputStream(InputStream is,String f) throws IOException
- {
- BufferedReader br = new BufferedReader(new InputStreamReader(is));
- StringBuffer lines = new StringBuffer();
-
- for (String line = br.readLine(); line != null; line = br.readLine())
- {
- lines.append(line);
- int d=line.indexOf("Duration:");
- if(d>0){
- String dur=line.replace("Duration:", "");
- dur=dur.trim();
- dur=dur.substring(0,8);
- String time=dur.substring(0,2);
- int h=Integer.parseInt(time);
- time=dur.substring(3,5);
- int m=Integer.parseInt(time);
- time=dur.substring(6,8);
- int s=Integer.parseInt(time);
- long len=h*3600+m*60+s;
- this.totaltime=len;
- System.out.println(len);
- }
- int b=line.indexOf("time=");
- if(b>0){
- String time=line.substring(b,line.indexOf("bitrate")-1);
- time=time.substring(time.indexOf("=")+1,time.indexOf("."));
- float t=(float)Long.parseLong(time)/(float)this.totaltime;
- t=t*100;
- this.complete=(int)Math.ceil(t);
- }
- System.out.println(line);
- // System.out.println("完成:"+this.complete+"%");
- }
- return lines.toString();
- }
- }