解決H5播放視訊黑屏只有聲音沒有影象的問題,Java呼叫ffmpeg轉碼成h264的mp4格式
阿新 • • 發佈:2019-01-04
前端用的是HTML5的<video>標籤播放的視訊。由於其並不能支援所有視訊型別的播放,所以我們需要把視訊轉碼為其支援的編碼如h264。否則會出現黑屏只有聲音沒有影象的問題。
網上的方法大多是轉成flv的,這裡,我把它轉成mp4。直接呼叫Conver的run(filepath);就可
引數解釋:
-acodec //音訊的編碼 這裡使用copy直接用原來的不用轉提供速率
-vcodec //視訊的編碼 這裡使用libx264
-preset //該引數主要調節編碼速度和質量的平衡 其的值從快到慢有 ultrafast、superfast、veryfast、faster、
fast、medium、slow、slower、veryslow和placebo這10個。這裡選個比較快的superfast
ConverVideo.java
import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Date; import java.util.List; public class ConverVideo { private Date dt; private long begintime; private String PATH; private String filerealname; // 檔名 不包括副檔名 private String filename; // 包括副檔名 private String videofolder = "F:\\upload\\video\\other\\"; // 別的格式視訊的目錄 private String mp4folder = "F:\\upload\\mp4video\\"; // mp4成功後視訊的目錄 private String ffmpegpath = "F:\\upload\\ffmpeg\\bin\\ffmpeg.exe"; // ffmpeg.exe的目錄 private String mencoderpath = "F:\\video\\mencoder\\"; // mencoder的目錄 private String videoRealPath = "F:\\upload\\mp4video\\mp4\\"; // 截圖的視訊目錄; private String imageRealPath = "F:\\upload\\mp4video\\img\\"; // 截圖的存放目錄 public ConverVideo() { } public ConverVideo(String path) { PATH = path; } public String getPATH() { return PATH; } public void setPATH(String path) { PATH = path; } public boolean beginConver() { File fi = new File(PATH); filename = fi.getName(); filerealname = filename.substring(0, filename.lastIndexOf(".")) .toLowerCase(); System.out.println("----接收到檔案(" + PATH + ")需要轉換-------------------------- "); if (!checkfile(PATH)) { System.out.println(PATH + "檔案不存在" + " "); return false; } dt = new Date(); begintime = dt.getTime(); System.out .println("----開始轉檔案(" + PATH + ")-------------------------- "); if (process()) { Date dt2 = new Date(); System.out.println("轉換成功 "); long endtime = dt2.getTime(); long timecha = (endtime - begintime); String totaltime = sumTime(timecha); System.out.println("共用了:" + totaltime + " "); if (processImg()) { System.out.println("截圖成功了 "); } else { System.out.println("截圖不成功了 "); } PATH = null; return true; } else { PATH = null; return false; } } public boolean processImg() { List commend = new java.util.ArrayList(); commend.add(ffmpegpath); commend.add("-i"); commend.add(videoRealPath + filerealname + ".flv"); commend.add("-y"); commend.add("-f"); commend.add("image2"); commend.add("-ss"); commend.add("38"); commend.add("-t"); commend.add("0.001"); commend.add("-s"); commend.add("320x240"); commend.add(imageRealPath + filerealname + ".jpg"); try { ProcessBuilder builder = new ProcessBuilder(); builder.command(commend); builder.start(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } private boolean process() { int type = checkContentType(); boolean status = false; if (type == 0) { status = processMp4(PATH); } else if (type == 1) { String avifilepath = processAVI(type); if (avifilepath == null) return false; // avi檔案沒有得到 else { System.out.println("kaishizhuang"); status = processMp4(avifilepath);// 將avi轉為mp4 } } return status; } private int checkContentType() { String type = PATH.substring(PATH.lastIndexOf(".") + 1, PATH.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 boolean checkfile(String path) { File file = new File(path); if (!file.isFile()) { return false; } else { return true; } } // 對ffmpeg無法解析的檔案格式(wmv9,rm,rmvb等), 可以先用別的工具(mencoder)轉換為avi(ffmpeg能解析的)格式. private String processAVI(int type) { List commend = new java.util.ArrayList(); commend.add(mencoderpath); commend.add(PATH); commend.add("-oac"); commend.add("mp3lame"); commend.add("-lameopts"); commend.add("preset=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(videofolder + filerealname + ".avi"); // 命令型別:mencoder 1.rmvb -oac mp3lame -lameopts preset=64 -ovc xvid // -xvidencopts bitrate=600 -of avi -o rmvb.avi try { ProcessBuilder builder = new ProcessBuilder(); builder.command(commend); Process p = builder.start(); doWaitFor(p); return videofolder + filerealname + ".avi"; } catch (Exception e) { e.printStackTrace(); return null; } } // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) private boolean processMp4(String oldfilepath) { if (!checkfile(PATH)) { System.out.println(oldfilepath + " is not file"); return false; } List commend = new java.util.ArrayList(); commend.add(ffmpegpath); commend.add("-i"); commend.add(oldfilepath); commend.add("-acodec"); commend.add("copy"); commend.add("-vcodec"); commend.add("libx264"); commend.add("-preset"); commend.add("superfast"); commend.add("-y"); commend.add(mp4folder + filerealname + ".mp4"); try { ProcessBuilder builder = new ProcessBuilder(); String cmd = commend.toString(); builder.command(commend); Process p = builder.start(); doWaitFor(p); p.destroy(); deleteFile(oldfilepath); return true; } catch (Exception e) { e.printStackTrace(); return false; } } public int doWaitFor(Process p) { InputStream in = null; InputStream err = null; int exitValue = -1; // returned to caller when p is finished try { System.out.println("comeing"); in = p.getInputStream(); err = p.getErrorStream(); boolean finished = false; // Set to true when p is finished while (!finished) { try { while (in.available() > 0) { Character c = new Character((char) in.read()); System.out.print(c); } while (err.available() > 0) { Character c = new Character((char) err.read()); System.out.print(c); } exitValue = p.exitValue(); finished = true; } catch (IllegalThreadStateException e) { Thread.currentThread().sleep(500); } } } catch (Exception e) { System.err.println("doWaitFor();: unexpected exception - " + e.getMessage()); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { System.out.println(e.getMessage()); } if (err != null) { try { err.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } } return exitValue; } public void deleteFile(String filepath) { File file = new File(filepath); if (PATH.equals(filepath)) { if (file.delete()) { System.out.println("檔案" + filepath + "已刪除"); } } else { if (file.delete()) { System.out.println("檔案" + filepath + "已刪除 "); } File filedelete2 = new File(PATH); if (filedelete2.delete()) { System.out.println("檔案" + PATH + "已刪除"); } } } public String sumTime(long ms) { int ss = 1000; long mi = ss * 60; long hh = mi * 60; long dd = hh * 24; long day = ms / dd; long hour = (ms - day * dd) / hh; long minute = (ms - day * dd - hour * hh) / mi; long second = (ms - day * dd - hour * hh - minute * mi) / ss; long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss; String strDay = day < 10 ? "0" + day + "天" : "" + day + "天"; String strHour = hour < 10 ? "0" + hour + "小時" : "" + hour + "小時"; String strMinute = minute < 10 ? "0" + minute + "分" : "" + minute + "分"; String strSecond = second < 10 ? "0" + second + "秒" : "" + second + "秒"; String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond; strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond + "毫秒" : "" + strMilliSecond + " 毫秒"; return strDay + " " + strHour + ":" + strMinute + ":" + strSecond + " " + strMilliSecond; } }
Conver.java
package com.lrm.common.utils;
public class Conver {
public void run(String filePath) {
try {
ConverVideo cv = new ConverVideo(filePath);
cv.beginConver();
} catch (Exception e) {
e.printStackTrace();
}
}
}
成功轉碼如圖: