用程序自动把视频文件转为flv文件

28 篇文章 0 订阅
23 篇文章 0 订阅
最近看了下相关的文章,发现其实视频类网站的自动上传视频后转为FLV文件,用的其实都是ffpmeg和mencoder两个软件,
它们都是开源的,最好跑在linux下,但windows下跑也是可以的,好象medcoder可以转变的格式更加丰富更加多样,但我们先来看
ffmpeg吧,其中涉及了非常多的参数,可以见之前的http://www.cnblogs.com/jackyrong/archive/2008/07/02/1234356.html
 其中,先搞好两个bat,一个是用来转换的encoder.bat,一个是CUT图的make.bat
其中encoder.bat的内容为
  ffmpeg.exe -i %1 -y -ab 56 -ar 22050 -qscale 8 -r 15 -s 512x384 %2 2>encode.txt
make.bat的内容为
  ffmpeg.exe -i %1 -y -f image2 -ss 5 -t 0.001 -s 120x90 %2 2>encode.txt
这里是从第5秒开始CUT图(-ss 5),延时0.00秒,CUT图的象素为120*90.

还有把ffmpeg.exe这个文件放在c:\windows\system32下面
java的转换代码为:
  

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

public class ChangeVedio {
 
 
//resourcePath为原来视频文件的路径
//filename:要转换成FLV的文件名
//realpath:实际存放FLV的路径
 public static boolean process(String resourcePath,String fileName,String realPath) {  
 
  int type = checkContentType(resourcePath);
 
        boolean status = false;

//如果符合转换文件类型
        if (type==0) {
        
            status = processFLV(resourcePath,fileName,realPath);      
        }
        return status;
    }

//检查文件的类型
    private static int checkContentType(String resourcePath) {
        String type = resourcePath.substring(resourcePath.lastIndexOf(".") + 1,
          resourcePath.length()).toLowerCase();
       

        if (type.equals("avi")) {
            return 0;
        } 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("mpe")){
         return 0;
        }
       
      
        else if (type.equals("wmv9")) {
            return 1;
        } else if (type.equals("rm")) {
            return 1;
        } else if (type.equals("rmvb")) {
            return 1;
        }      
        return 9;
    }
  
    private static boolean checkfile(String path){
     File file=new File(path);
     if(!file.isFile()){
      return false;
     }
     return true;
    }


    private static boolean processFLV(String resourcePath,String fileName,String realPath) {
    
      if(!checkfile(resourcePath)){
          System.out.println(resourcePath+" is not file");
          return false;
         }   
        try {
         Runtime runtime=Runtime.getRuntime();
            Process proce;
            String cmd="";
            String realPath1 = realPath + "//Vedio//";
            String realPath2 = realPath + "//Vedio//";
            Runtime runtime1=Runtime.getRuntime();      
            Process proce1;
            proce1 = runtime1.exec("c://encoder.bat "+resourcePath+" "+realPath2 + fileName);
   proce1.waitFor();
            proce=runtime.exec("c://makeing.bat "+resourcePath+" "+realPath1+fileName+".jpg");
            proce.waitFor();
            File flvFile = new File(realPath2 + fileName);
         
//如果转换成功,文件存在并且长度>0
            boolean success = flvFile.exists()&&flvFile.length()>0;
           
            return success;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }  
   
    public static void main(String[] args) { 
        if (process("c://zhang.avi","huge.flv","D://360"))
        {                
            System.out.println("ok");
        }
 }
 
}
  其中可以看到,在检查文件类型符合后,在主程序中,
用   Runtime runtime=Runtime.getRuntime();去调用CMD命令下执行的.bat文件
  并且这里开了两个进程proce1,proce2,分别处理CUT图和flv转换,效率高.

而在PHP中,也可以的,用exec()去调用.bat文件

<?

$source="c://zhang.avi";

$dest="huge.flv";
$desdisk="D://360//";
$filename=$desdisk.$dest;
exec ("c://encoder.bat ".$source." ".$desdisk.$dest,$sta);

if(file_exists($filename)){
    die ("转换OK!");
}

 

?>


   

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值