最近看了下相关的文章,发现其实视频类网站的自动上传视频后转为FLV文件,用的其实都是ffpmeg和mencoder两个软件,
它们都是开源的,最好跑在linux下,但windows下跑也是可以的,好象medcoder可以转变的格式更加丰富更加多样,但我们先来看
ffmpeg吧,其中涉及了非常多的参数
 其中,先搞好两个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!");
}
?>