Java 實現視頻轉FLV,支持完成進度百分比
視頻轉換主要采用ffmpeg,下載去官網即可
轉換主程序:
注:以下標粗的 ffmpeg 是 絕對路徑如:D:/conver/ffmpeg-win.exe/**
*
* @Title: processFLV
* @Description: 轉FLV格式
* ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
* @param
* @return boolean
* @throws
*/
private static boolean coverToFLV(String srcVideoPath,String tarVideoPath) {
if (!checkfile(srcVideoPath)) {
logger.error("【" + srcVideoPath + "】 不存在 !");
return false;
}
List<String> commend = new java.util.ArrayList<String>();
commend.add(</span><strong><span style="font-size:18px;">ffmpegPath</span></strong><span style="font-size:14px;">);
commend.add( "-y");
commend.add( "-i");
commend.add(srcVideoPath);
commend.add("-ab");
commend.add("64");
//commend.add("-sameq");
commend.add("-acodec");
commend.add("mp3");
// commend.add("-vcodec");
// commend.add("xvid");
commend.add("-ac");
commend.add("2");
commend.add("-ar");
commend.add("22050");
commend.add("-qscale");
commend.add("6"); //壓縮大小
// commend.add("-b");
// commend.add("1500");
commend.add("-r");
commend.add("24");
commend.add(tarVideoPath);
try {
ProcessBuilder builder = new ProcessBuilder();
//String cmd = commend.toString();
builder.command(commend);
Process p = builder.start();
doWaitPro(p);
//doWaitFor(p);
p.destroy();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
等待線程完成: //等待線程處理完成
public static void doWaitPro(Process p){
try {
String errorMsg = readInputStream(p.getErrorStream(), "error");
String outputMsg = readInputStream(p.getInputStream(), "out");
int c = p.waitFor();
if (c != 0) {// 如果處理進程在等待
System.out.println("處理失敗:" + errorMsg);
} else {
System.out.println(COMPLETE + outputMsg);
}
} catch (IOException e) {
// tanghui Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// tanghui Auto-generated catch block
e.printStackTrace();
}
}
完成進度百分比:
/** * * @Title: readInputStream * @Description: 完成進度百分比 * @param * @return String * @throws */ private static String readInputStream(InputStream is, String f) throws IOException { // 將進程的輸出流封裝成緩沖讀者對象 BufferedReader br = new BufferedReader(new InputStreamReader(is)); StringBuffer lines = new StringBuffer();// 構造一個可變字符串 long totalTime = 0; // 對緩沖讀者對象進行每行循環 for (String line = br.readLine(); line != null; line = br.readLine()) { lines.append(line);// 將每行信息字符串添加到可變字符串中 int positionDuration = line.indexOf("Duration:");// 在當前行中找到第一個"Duration:"的位置 int positionTime = line.indexOf("time="); if (positionDuration > 0) {// 如果當前行中有"Duration:" String dur = line.replace("Duration:", "");// 將當前行中"Duration:"替換為"" dur = dur.trim().substring(0, 8);// 將替換后的字符串去掉首尾空格后截取前8個字符 int h = Integer.parseInt(dur.substring(0, 2));// 封裝成小時 int m = Integer.parseInt(dur.substring(3, 5));// 封裝成分鐘 int s = Integer.parseInt(dur.substring(6, 8));// 封裝成秒 totalTime = h * 3600 + m * 60 + s;// 得到總共的時間秒數 } if (positionTime > 0) {// 如果所用時間字符串存在 // 截取包含time=的當前所用時間字符串 String time = line.substring(positionTime, line .indexOf("bitrate") - 1); time = time.substring(time.indexOf("=") + 1, time.indexOf("."));// 截取當前所用時間字符串 int h = Integer.parseInt(time.substring(0, 2));// 封裝成小時 int m = Integer.parseInt(time.substring(3, 5));// 封裝成分鐘 int s = Integer.parseInt(time.substring(6, 8));// 封裝成秒 long hasTime = h * 3600 + m * 60 + s;// 得到總共的時間秒數 float t = (float) hasTime / (float) totalTime;// 計算所用時間與總共需要時間的比例 COMPLETE = (int) Math.ceil(t * 100);// 計算完成進度百分比 } System.out.println("完成:" + COMPLETE + "%"); } br.close();// 關閉進程的輸出流 return lines.toString(); }來自:http://blog.csdn.net/tanghui2qinghong/article/details/24304859