java利用ffmpeg截取视频一帧保存图片


public class VedioUtils {

	 private static String ffmpegEXE = "/usr/local/ffmpeg/bin/ffmpeg";//ffmpeg程序路径
	 
	 private static final Logger log = LoggerFactory.getLogger(VedioUtils.class);

	/**
	 * 开启线程处理Ffmpeg处理流
	 * 
	 * @param process
	 */
	private static void dealStream(Process process) {
		if (process == null) {
			return;
		}
		// 处理InputStream的线程
		new Thread() {
			@Override
			public void run() {
				BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
				String line = null;
				try {
					while ((line = in.readLine()) != null) {
	                    log.warn("output: " + line);
	                }
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
		// 处理ErrorStream的线程
		new Thread() {
			@Override
			public void run() {
				BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
				String line = null;
				try {
					while ((line = err.readLine()) != null) {
	                    log.warn("output: " + line);
	                }
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					try {
						err.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
	}


	/**
	 * 截取一帧作为图片
	 * 
	 * @param fileName
	 *            ffmpeg -i /Users/jinx/Downloads/test.mp4 -y -f image2 -ss
	 *            00:00:01 -vframes 1 /Users/jinx/Downloads/test.jpeg
	 * @return
	 */
	public static boolean covPic(String fileName, String time, String outName, String size) {
		List command = new ArrayList();
		command.add(ffmpegEXE);
		command.add("-i");
		command.add(fileName);//文件输入源
		command.add("-y");
		command.add("-f");
		command.add("image2");
		command.add("-ss");
		command.add(time);//截取的时间点格式为 00:00:01
		command.add("-vframes");
		command.add("1");
		command.add("-s");
		command.add(size);//截取的图片大小,格式为:1920x1080
		command.add(outName);//文件输出路径,格式为 11.jpg

		try {
			Process videoProcess = new ProcessBuilder(command).start();
			dealStream(videoProcess);
			videoProcess.waitFor();
			return true;
		} catch (Exception e) {
		}
		return false;
	}


}

你可能感兴趣的:(java,ffmpeg)