以下程序沒有對命令執行結果進行任何解析處理,如要解析,請自行處理
使用的工具爲ffmpeg,在windows下非常方便,直接將exe文件放在指定文件夾下即可,linux需要安裝 安裝命令有一些地方需注意(本人對linux不熟練,有可能不需要安裝,編譯成一個文件,然後調用?)
windows下的ffmpeg下載地址:
http://download.csdn.net/detail/u013284604/8511575
公共java類:
private static void executeCmd(List<String> cmdList) throws BusinessException{
String line = null;
Process process = null;
try {
process = Runtime.getRuntime().exec(cmdList.toArray(new String[cmdList.size()]));
BufferedReader bw = new BufferedReader(new InputStreamReader(process.getErrorStream()));
StringBuffer returnSb = new StringBuffer();
while((line = bw.readLine())!=null){
returnSb.append(line).append("\n");
}
System.out.println(returnSb);
} catch (Exception e) {
e.printStackTrace();
try {
process.getErrorStream().close();
process.getInputStream().close();
process.getOutputStream().close();
} catch (IOException e1) {
e1.printStackTrace();
}
throw new Exception("轉化失敗");
}
}
一、音頻轉爲mp3格式
目前經測試支持的轉換音頻爲mp3,wma,wav,amr
List<String> cmdList = new ArrayList<String>();
cmdList.add("ffmpeg路徑");
cmdList.add("-i");
cmdList.add("源文件路徑");
cmdList.add("-acodec");
cmdList.add("libmp3lame");
cmdList.add("目標文件路徑");
executeCmd(cmdList);
二、視頻轉爲mp4格式
目前經測試支持的轉換音頻爲rm,rmvb,wmv,avi,mpg,mp4,mpeg
List<String> cmdList = new ArrayList<String>();
cmdList.add("ffmpeg路徑");
cmdList.add("-i");
cmdList.add("源文件路徑");
cmdList.add("-vcodec");
cmdList.add("libx264");
cmdList.add("-vb");
cmdList.add("384k");
cmdList.add("-r");
cmdList.add("18");
cmdList.add("-strict");
cmdList.add("-2");
cmdList.add("-ar");
cmdList.add("22050");
cmdList.add("-ab");
cmdList.add("64k");
cmdList.add("-coder");
cmdList.add("0");
cmdList.add("目標文件路徑");
executeCmd(cmdList);
三、視頻截圖(取第一幀)
cmdList.add("ffmpeg路徑");
cmdList.add("-i");
cmdList.add("源文件路徑");
cmdList.add("-y");
cmdList.add("-f");
cmdList.add("image2");
cmdList.add("-vframes");
cmdList.add("1");
cmdList.add("目標文件路徑");
executeCmd(cmdList);
以上文章如有不對之處,請批評指出,共同進步