上传视频使用ffmpeg自动截取缩略图

上传视频之后,有的需要显示缩略图,而不是仅仅显示视频名称的列表,这时候就需要对上传的视频截取缩略图。

简单粗暴点,将以下代码作为工具类复制粘贴即可;

package com.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.constant.Constant;

public class FfmpegUtil {
	
	/**
	  * 获取ffmpeg工具路径
	  * **/
	  public static String getFFmpegPath(){
		  String ffpath = "D:\Video\ffmpeg\";
		 return ffpath ;
	  }

	 /**
	  * @author wya
	  *2015年9月8日
	  *使用FFPEG命令需将ffpeg加入到环境变量PATH
	  *@param command 需要执行的cmd命令
	  */
		public void runCmd(String command) {
	     try {
	         Runtime rt = Runtime.getRuntime();
	         Process proc = rt.exec(command);
	         InputStream stderr = proc.getErrorStream();//获取错误信息
	         InputStreamReader isr = new InputStreamReader(stderr);
	         BufferedReader br = new BufferedReader(isr);
	         String line = null;
	
	         while ( (line = br.readLine()) != null)
	             System.out.println(line);
	
	         int exitVal = proc.waitFor();
	         System.out.println("Process exitValue: " + exitVal);
	     } catch (Throwable t) {
	         t.printStackTrace();
	     }
	 }
	
	  private static String inputPath = "";
	  
	  private static String outputPath = "";
	  
	  private static String ffmpegPath = "";
  
	  private static String FFMPEG=getFFmpegPath()+Constant.TOOLS_FFMPEG;
	  private static String FFPLAY=getFFmpegPath()+Constant.TOOLS_FFPLAY;
	  private static String FFPROBE=getFFmpegPath()+Constant.TOOLS_FFPROBE;
	  private static String RTMPDUMP=getFFmpegPath()+Constant.TOOLS_RTMPDUMP;
		
	/**
	 * 测试方法
	 * @author wya
	 *2015年9月8日
	 *@param args
	 */
	public static void main(String[] args) {
		String ss = getFFmpegPath();
		System.out.print(ss);
		boolean a2 = saveVideoImg("D:\sp\1.mp4","D:\sp\1.jpg", "3", "320x240");
	}

	/**
	 * 视频截图
	 * @author wya
	 *2015年9月11日
	 *@param videoFile  rtmp://s2pchzxmtymn2k.cloudfront.net/cfx/st/mp4:sintel.mp4
	 *@param imgFile
	 *@param 第xtime  秒
	 *@param pix  分辨率 320x240
	 *@return  ffmpeg.exe -i rtmp://s2pchzxmtymn2k.cloudfront.net/cfx/st/mp4:sintel.mp4 -r 5 -ss 20 -s 150*100 c:\video.flv
	 */
	public static boolean saveVideoImg(String videoFile,String imgFile,String xtime,String pix){
	//	String command=RTMPDUMP+" -i "+videoFile+" -y -f mjpeg -ss 3 -t 0.001 -s "+pix+"  ";
		
      List<String> commend = new ArrayList<String>();
      commend.add(FFMPEG);
      commend.add("-i");
      commend.add(videoFile);
      commend.add("-y");
      commend.add("-f");
      commend.add("mjpeg");
      commend.add("-ss");
      commend.add(xtime);
      commend.add("-t");
      commend.add("0.001");
      commend.add("-s");
      commend.add(pix);
      commend.add(imgFile);
   try {
          ProcessBuilder builder = new ProcessBuilder();
          Process proc = builder.command(commend).redirectErrorStream(true).start();
          InputStream stderr = proc.getErrorStream();
          InputStreamReader isr = new InputStreamReader(stderr);
          BufferedReader br = new BufferedReader(isr);
          String line = null;

          while ( (line = br.readLine()) != null)
              System.out.println(line);

          int exitVal = proc.waitFor();
          System.out.println("Process exitValue: " + exitVal);
         // proc.destroy();
      } catch (Throwable t) {
          t.printStackTrace();
          return false;
      }
      return true;
	}

}

 以上代码还需要在公共类中命名几个常量,代码如下:

/**
	 * ffmpeg相关exe名称
	 */
	String FFMPEG_PATH="FFMPEG_PATH";
	String TOOLS_FFMPEG="ffmpeg";
	String TOOLS_FFPLAY="ffplay";
	String TOOLS_FFPROBE="ffprobe";
	String RED5_STREAM_PATH="RED5_STREAM_PATH";

	
	String TOOLS_RTMPDUMP="rtmpdump";

 然后在这个网址https://ffmpeg.zeranoe.com/builds/下载ffmpeg,放到某个你自己认为方便的位置,并更改第一段代码中标红处,将你的exe文件的位置修改即可。

然后找一段视频,将其位置放在第二个标红处即测试代码的位置,即可。

以上代码,直接测试即可,基本上就可以用了。

如果还想找更详细的说明,请点击以下链接

http://www.tuicool.com/articles/fMRRZvY

原文地址:https://www.cnblogs.com/qcq0703/p/7652518.html