本文實例講述了Java實現對視頻進行截圖的方法。分享給大家供大家參考,具體如下:
之前介紹過Java使用ffmpeg進行視頻轉換,這里演示一下ffmpeg進行視頻截圖的方法。
具體代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
import java.io.File; import java.util.List; //生成視頻文件的首幀為圖片 //windows下的版本 public class CreatePh { // public static final String FFMPEG_PATH = "E:/ffmpeg/ffmpeg.exe"; public static boolean processImg(String veido_path, String ffmpeg_path) { File file = new File(veido_path); if (!file.exists()) { System.err.println( "路徑[" + veido_path + "]對應的視頻文件不存在!" ); return false ; } List<String> commands = new java.util.ArrayList<String>(); commands.add(ffmpeg_path); commands.add( "-i" ); commands.add(veido_path); commands.add( "-y" ); commands.add( "-f" ); commands.add( "image2" ); commands.add( "-ss" ); commands.add( "8" ); // 這個參數是設置截取視頻多少秒時的畫面 // commands.add("-t"); // commands.add("0.001"); commands.add( "-s" ); commands.add( "700x525" ); commands.add(veido_path.substring( 0 , veido_path.lastIndexOf( "." )) .replaceFirst( "vedio" , "file" ) + ".jpg" ); try { ProcessBuilder builder = new ProcessBuilder(); builder.command(commands); builder.start(); System.out.println( "截取成功" ); return true ; } catch (Exception e) { e.printStackTrace(); return false ; } } public static void main(String[] args) { processImg( "C:/video1.avi" , "C:/ffmpeg.exe" ); } } |
運行后的截圖如下:
附:
ffmpeg.exe點擊此處本站下載。
測試用avi格式視頻點擊此處本站下載。
希望本文所述對大家java程序設計有所幫助。
原文鏈接:http://blog.csdn.net/yangnianbing110/article/details/33738469