標籤:
首發:個人部落格,持續更新和錯誤修正
主要使用技術:
1)FFmpeg,用於主流格式之間的轉換,例如AVI,MP4,FLV等。
2)MEncoder,用於奇葩格式轉主流格式,例如RMVB轉AVI。這樣我們可以把奇葩格式先轉AVI,再由FFmpeg把AVI轉成想要的格式。
3)java的執行命令列操作的技術,這樣安裝在伺服器上的↑這兩個轉換器就可以被java調用了。
包括ProcessBuilder和Runtime這兩種調法。
可以參考這篇。
FFmpeg的官網在這裡,其文檔在這裡。
MEncoder的官網在這裡,其中文文檔在這裡。
主要參考:這篇文章,使用的FFmpeg和MEncoder也直接用的這篇文章後面提供的壓縮包。
但正如有的網友指出的,這篇文章中代碼的硬傷在於,當rmvb->avi->flv這樣兩步轉換的時候,需要等待前一步完成,再進行後一步。
所以改了改代碼,如下:
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;
public class Test {
public static final String BASEPATH = "d:\\ftest\\";
public static void main(String[] args) {
String fromFilePath1 = BASEPATH + "from\\test.mp4";
doConvert(fromFilePath1, "flv");
String fromFilePath2 = BASEPATH + "from\\test.rmvb";
doConvert(fromFilePath2, "flv");
}
/**
* 嘗試進行轉換
*/
public static void doConvert(String fromFilePath, String goalType){
if (!checkInput(fromFilePath)) {
System.out.println("檔案" + fromFilePath + "不存在");
}else{
if (process(fromFilePath, goalType)) {
System.out.println("轉換成功");
}else{
System.out.println("轉換失敗");
}
}
}
/**
* 進行轉換
* @return
*/
private static boolean process(String fromFilePath, String goalType) {
int type = checkContentType(fromFilePath);
boolean status = false;
if (type == 0) {
status = getResult(fromFilePath, goalType);
} else if (type == 1) {
String avifilepath = getPreResult(type, fromFilePath);
if (avifilepath == null){
return false;
}
status = getResult(avifilepath, goalType);
}
return status;
}
/**
* 判斷源視頻的種類(主流格式 or 奇葩格式)
*/
private static int checkContentType(String fromFilePath) {
String type = fromFilePath.substring(fromFilePath.lastIndexOf(".") + 1, fromFilePath.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 checkInput(String path) {
File file = new File(path);
if (!file.isFile()) {
return false;
}
return true;
}
/**
* 對ffmpeg無法解析的檔案格式(wmv9,rm,rmvb等), 可以先用別的工具(mencoder)轉換為avi(ffmpeg能解析的)格式.
*/
private static String getPreResult(int type, String fromFilePath) {
String fileName = UUID.randomUUID().toString() + ".avi";
//預先處理指令
List<String> commend = new ArrayList<String>();
commend.add(BASEPATH + "util\\mencoder");
commend.add(fromFilePath);
//commend.add("-oac lavc");
commend.add("-oac");
commend.add("mp3lame");
commend.add("-lameopts");
commend.add("preset=64");
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(BASEPATH + "pre\\" + fileName);
try {
//預先處理進程
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.redirectErrorStream(true);
//進程資訊輸出到控制台
Process p = builder.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
p.waitFor();//直到上面的命令執行完,才向下執行
return BASEPATH + "pre\\" + fileName;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
* @param oldfilepath
* @param goalType
* @return
*/
private static boolean getResult(String oldfilepath, String goalType) {
String fileName = UUID.randomUUID() + "." + goalType;
if (!checkInput(oldfilepath)) {
System.out.println(oldfilepath + "不存在");
return false;
}
// 檔案命名
Calendar c = Calendar.getInstance();
//轉換格式命令
List<String> commend = new ArrayList<String>();
commend.add(BASEPATH + "util\\ffmpeg");
commend.add("-i");
commend.add(oldfilepath);
commend.add("-ab");
commend.add("56");
commend.add("-ar");
commend.add("22050");
commend.add("-qscale");
commend.add("8");
commend.add("-r");
commend.add("15");
commend.add("-y");
commend.add("-s");
commend.add("600x500");
commend.add(BASEPATH + "to\\" + fileName);
try {
Runtime runtime = Runtime.getRuntime();
//命令
String cut = BASEPATH + "util\\ffmpeg.exe -i "
+ oldfilepath
+ " -y -f image2 -ss 8 -t 0.001 -s 600x500 " + BASEPATH + "\\to\\" + fileName + ".jpg";
//進程
Process proce = runtime.exec(cut);
proce.waitFor();//直到上面的命令執行完,才向下執行
//轉換格式進程
ProcessBuilder builder = new ProcessBuilder(commend);
builder.command(commend);
builder.redirectErrorStream(true);
//進程資訊輸出到控制台
Process p = builder.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
p.waitFor();//直到上面的命令執行完,才向下執行
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
長期歡迎項目合作機會介紹,項目收入10%用於酬謝介紹人。新浪微博:@冷鏡,QQ:908789432。
java使用ffmpeg和mencoder做視頻格式轉換