public static boolean process(String resourcePath) {
int type = checkContentType(resourcePath);
boolean status = false;
if (type == 0) {
status = processFLV(resourcePath);// 直接將文件轉(zhuǎn)為flv文件
}
return status;
}
private static int checkContentType(String resourcePath) {
String type = resourcePath.substring(resourcePath.lastIndexOf(".") + 1,
resourcePath.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;
} else if (type.equals("mpeg")) {
return 0;
} else if (type.equals("mpe")) {
return 0;
}
// 對ffmpeg無法解析的文件格式(wmv9,rm,rmvb等),
// 可以先用別的工具(mencoder)轉(zhuǎn)換為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能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
private static boolean processFLV(String resourcePath) {
if (!checkfile(resourcePath)) {
System.out.println(resourcePath + " is not file");
return false;
}
// 文件命名
Calendar c = Calendar.getInstance();
String savename = String.valueOf(c.getTimeInMillis())+ Math.round(Math.random() * 100000);
List commend = new ArrayList();
commend.add("e:\\ffmpeg");
commend.add("-i");
commend.add(resourcePath);
// commend.add("-ab");//音頻流碼率:(默認(rèn)是同源文件碼率)
// commend.add("56");
commend.add("-ar");//視頻流采樣率:(大多數(shù)情況下使用44100和48000,分別對用PAL和NTSC制式,根據(jù)需要選擇)
commend.add("22050");
commend.add("-qscale");//視頻量化指標(biāo)
commend.add("8");
commend.add("-r");//視頻流幀數(shù)(一般來書PAL制式同常用25,ntsc制式通常用29)
commend.add("15");
commend.add("-s");//視頻解析度:(分辨率)可以自己定義所需要的大?。?改變視頻流的解析式很耗cpu的
commend.add("600x500");
commend.add("e:\\" + savename + ".flv");
try {
Runtime runtime = Runtime.getRuntime();
Process proce = null;
String cmd = "";
String cut = " e://ffmpeg.exe -i "
+ resourcePath
+ " -y -f image2 -ss 8 -t 0.001 -s 600x500 e:\\"
+ savename + ".jpg";
String cutCmd = cmd + cut;
proce = runtime.exec(cutCmd);
ProcessBuilder builder = new ProcessBuilder(commend);
// builder.command(commend);
builder.start();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
if (!checkfile("e:\\1-android_sdk.flv")) {
System.out.println("" + " is not file");
return;
}
if (process("e:\\1-android_sdk.flv")) {
System.out.println("ok");
}
}
注意:在所用目錄應(yīng)放入ffmpeg.exe 工具 此代碼
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報。