直接上代码-依赖ffmpeg.exe运行文件
package com.common.util; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import javax.imageio.IIOImage; import javax.imageio.ImageIO; import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriter; public class VideoThumbTaker { protected String ffmpegApp; public VideoThumbTaker(String ffmpegApp) { this.ffmpegApp = ffmpegApp; } @SuppressWarnings("unused") /**** * 获取指定时间内的图片 * @param videoFilename:视频路径 * @param thumbFilename:图片保存路径 * @param width:图片长 * @param height:图片宽 * @param hour:指定时 * @param min:指定分 * @param sec:指定秒 * @throws IOException * @throws InterruptedException */ public void getThumb(String videoFilename, String thumbFilename, int width, int height, int hour, int min, float sec) throws IOException, InterruptedException { ProcessBuilder processBuilder = new ProcessBuilder(ffmpegApp, "-y", "-i", videoFilename, "-vframes", "1", "-ss", hour + ":" + min + ":" + sec, "-f", "mjpeg", "-s", width + "*" + height, "-an", thumbFilename); Process process = processBuilder.start(); InputStream stderr = process.getErrorStream(); InputStreamReader isr = new InputStreamReader(stderr); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) ; process.waitFor(); if (br != null) br.close(); if (isr != null) isr.close(); if (stderr != null) stderr.close(); } public static void main(String[] args) { String path = UploadFileUtil.class.getClassLoader().getResource("ffmpeg/ffmpeg.exe").getPath(); VideoThumbTaker videoThumbTaker = new VideoThumbTaker(path); try { videoThumbTaker.getThumb("D:\\elwPath\\20170825024823546.mp4", "D:\\mp41.png",300, 200, 0, 0, 1); System.out.println("over"); } catch (Exception e) { e.printStackTrace(); } } }