用java小例题说明更直观:(可以直接编译运行)
环境我在 windows平台下测试的。。。
需要在e:\下有 ffmpeg.exe;mencoder.exe;drv43260.dll;pncrt.dll共4个文件。
如何得到这4个文件参考文章http://blog.sina.com.cn/u/4a424eca010005kb
还要在e:\input下放各种文件名为a的以下各种视频文件;还要e: \output;java程序执行后能得到一个a.flv的已转换的文件。
ffmpeg.exe能解析的格式: (asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
对ffmpeg.exe无法解析的文件格式 (wmv9,rm,rmvb等),
可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式;
mencoder.exe;drv43260.dll;pncrt.dll 这3个文件是为文件格式(wmv9,rm,rmvb等)
转换为avi(ffmpeg能解析的)格式准备的;再把转换好的avi文件再用 ffmpeg.exe转换成flv格式的视频文件。。。
java文件的内容如下:
import java.io.File;
import java.util.List;
public class ConvertVideo {
private final static String PATH = "c:\\test\\a.mpg";
public static void main(String[] args) {
if(!checkfile(PATH)){
System.out.println(PATH+" is not file");
return;
}
if (process()) {
System.out.println("ok");
}
}
private static boolean process() {
int type = checkContentType();
boolean status = false;
if (type==0) {
status = processFLV(PATH);// 直接将文件转为flv文件
} else if (type==1) {
String avifilepath = processAVI(type);
if (avifilepath == null)
return false;// avi文件没有得到
status = processFLV(avifilepath);// 将avi转为flv
}
return status;
}
private static int checkContentType() {
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 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;
}
//对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;
}
private static boolean checkfile(String path){
File file=new File(path);
if(!file.isFile()){
return false;
}
return true;
}
// 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), 可 以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
private static String processAVI(int type) {
List<String> commend=new java.util.ArrayList<String>();
commend.add("e:\\mencoder");
commend.add(PATH);
commend.add("-oac");
commend.add("lavc");
commend.add("-lavcopts");
commend.add("acodec=mp3:abitrate=64");
commend.add("-ovc");
commend.add("xvid");
commend.add("-xvidencopts");
commend.add("bitrate=600");
commend.add("-of");
commend.add("avi");
commend.add("-o");
commend.add("c:\\home\\a.avi");
try{
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.start();
return "c:\\home\\a.avi";
}catch(Exception e){
e.printStackTrace();
return null;
}
}
// ffmpeg能解析的格式: (asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
private static boolean processFLV(String oldfilepath) {
if(!checkfile(PATH)){
System.out.println(oldfilepath+" is not file");
return false;
}
List<String> commend=new java.util.ArrayList<String>();
commend.add("e:\\ffmpeg");
commend.add("-i");
commend.add(oldfilepath);
commend.add("-ab");
commend.add("64");
commend.add("-acodec");
commend.add("mp3");
commend.add("-ac");
commend.add("2");
commend.add("-ar");
commend.add("22050");
commend.add("-b");
commend.add("230");
commend.add("-r");
commend.add("24");
commend.add("-y");
commend.add("c:\\home\\a.flv");
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.start();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
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!");
}
?>