Java 呼叫 FFmpeg 實作影片轉檔範例。本站提醒廣大學習愛好者:此文章僅供參考,不一定能完全符合您的需求。以下是完整實作正文
本文範例說明 Java 呼叫 FFmpeg 實作影片轉換的方法。分享給大家供參考。詳細分析如下:
這裡環境是在 Windows 平台下測試的。需要在 e:\ 下有 ffmpeg.exe、mencoder.exe、drv43260.dll、pncrt.dll 共 4 個檔案。
在 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 格式準備的。
再把轉換好的 avi 檔案再用 ffmpeg.exe 轉換成 flv 格式的影片檔案。
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);
} else if (type == 1) {
String avifilepath = processAVI(type);
if (avifilepath == null) return false;
status = processFLV(avifilepath);
}
return status;
}
private static int checkContentType() {
String type = PATH.substring(PATH.lastIndexOf(".") + 1, PATH.length()).toLowerCase();
if (type.equals("avi") || type.equals("mpg") || type.equals("wmv") || type.equals("3gp")
|| type.equals("mov") || type.equals("mp4") || type.equals("asf") || type.equals("asx")
|| type.equals("flv")) {
return 0;
} else if (type.equals("wmv9") || type.equals("rm") || type.equals("rmvb")) {
return 1;
}
return 9;
}
private static boolean checkfile(String path) {
File file = new File(path);
return file.isFile();
}
private static String processAVI(int type) {
List commend = new java.util.ArrayList<>();
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;
}
}
private static boolean processFLV(String oldfilepath) {
if (!checkfile(oldfilepath)) {
System.out.println(oldfilepath + " is not file");
return false;
}
List commend = new java.util.ArrayList<>();
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;
}
}
}
希望本文所述對大家的 Java 程式設計有所幫助。