下載下來的東西可能有不少,可是完成所須要的其實就這兩個。java
打開cmd,依照圖中的命令,進入ffmpeg的路徑下,輸入ffmpeg.exe -i 視頻路徑地址編程
若是出現以下信息,代表插件可用。信息中會記載時長,開始點,文件格式,等等app
/** * 獲取視頻總時間 * * @param videoPath 視頻路徑 * @param ffmpegPath ffmpeg路徑 * @return */ public static int getVideoTime(String videoPath, String ffmpegPath) throws Exception { List<String> commands = new java.util.ArrayList<String>(); commands.add(ffmpegPath); commands.add("-i"); commands.add(videoPath); //模擬cmd指令發送 ProcessBuilder builder = new ProcessBuilder(); builder.command(commands); final Process p = builder.start(); //從輸入流中讀取視頻信息 BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream())); StringBuffer sb = new StringBuffer(); String line; while ((line = br.readLine()) != null) { sb.append(line); } br.close(); //從視頻信息中解析時長 String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s"; Pattern pattern = Pattern.compile(regexDuration); Matcher m = pattern.matcher(sb.toString()); if (m.find()) { int time = getTimelen(m.group(1)); // System.out.println(video_path+",視頻時長:"+time+", 開始時間:"+m.group(2)+",比特率:"+m.group(3)+"kb/s"); return time; } return 0; } //格式:"00:00:10.68" 轉化爲秒數 public static int getTimelen(String timelen) { int min = 0; String strs[] = timelen.split(":"); if (strs[0].compareTo("0") > 0) { min += Integer.valueOf(strs[0]) * 60 * 60;//秒 } if (strs[1].compareTo("0") > 0) { min += Integer.valueOf(strs[1]) * 60; } if (strs[2].compareTo("0") > 0) { min += Math.round(Float.valueOf(strs[2])); } return min; }
/** * 獲取視頻第幾秒的截圖 * * @param videoPath * @param ffmpegPath * @param time * @param savePath */ public static void getVideoCover(String videoPath, String ffmpegPath, int time, String savePath) throws Exception { String videoName = videoPath.substring(videoPath.lastIndexOf("\\") + 1); String videoCoverName = videoName.substring(0, videoName.lastIndexOf(".")) + ".jpg"; List<String> cmd = new ArrayList<>(); cmd.add(ffmpegPath); cmd.add("-i"); cmd.add(videoPath); cmd.add("-y"); cmd.add("-f"); cmd.add("mjpeg"); cmd.add("-ss"); cmd.add(String.valueOf(time)); cmd.add("-t"); cmd.add("0.001"); cmd.add("-s"); cmd.add("1920*1080"); cmd.add(savePath + "\\" + videoCoverName); ProcessBuilder builder = new ProcessBuilder(); builder.command(cmd); builder.start(); }