1.软件安装

 

  • mac

brew install ffmpeg

安装后路径:"/usr/local/Cellar/ffmpeg/4.0.1/bin

ffmpeg –version 查看版本

 

  • linux

暂缺,后补

  • windwos

   下载地址:

   https://ffmpeg.zeranoe.com/builds/

解压即可

配置path环境变量

ffmpeg –version 查看版本

 

2.具体的java类

//获取视频相关信息

package com.cmcc.core.Util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 获取视频的信息 FFMPEG homepage http://ffmpeg.org/about.html
 */
public class VideoInfo {
    // 视频路径
    private String ffmpegApp;
    // 视频时a
    private int hours;
    // 视频分
    private int minutes;
    // 视频秒
    private float seconds;
    // 视频width
    private int width;
    // 视频height
    private int heigt;

    public VideoInfo() {
    }

    public VideoInfo(String ffmpegApp) {
        this.ffmpegApp = ffmpegApp;
    }

    public String toString() {
        return "time: " + hours + ":" + minutes + ":" + seconds + ", width = " + width + ", height= " + heigt;
    }

    public void getInfo(String videoFilename) throws IOException, InterruptedException {
        String tmpFile = videoFilename + ".tmp.png";
        ProcessBuilder processBuilder = new ProcessBuilder(ffmpegApp, "-y", "-i", videoFilename, "-vframes", "1", "-ss", "0:0:0", "-an", "-vcodec", "png", "-f", "rawvideo", "-s", "100*100", tmpFile);

        Process process = processBuilder.start();

        InputStream stderr = process.getErrorStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        String line;
        // 打印 sb,获取更多信息。 如 bitrate、width、heigt
        StringBuffer sb = new StringBuffer();
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

        new File(tmpFile).delete();

        System.out.println("video info:\n" + sb);
        Pattern pattern = Pattern.compile("Duration: (.*?),");
        Matcher matcher = pattern.matcher(sb);

        if (matcher.find()) {
            String time = matcher.group(1);
            calcTime(time);
        }

        pattern = Pattern.compile("w:\\d+ h:\\d+");
        matcher = pattern.matcher(sb);

        if (matcher.find()) {
            String wh = matcher.group();
            // w:100 h:100
            String[] strs = wh.split("\\s+");
            if (strs != null && strs.length == 2) {
                width = Integer.parseInt(strs[0].split(":")[1]);
                heigt = Integer.parseInt(strs[1].split(":")[1]);
            }
        }

        process.waitFor();
        if (br != null)
            br.close();
        if (isr != null)
            isr.close();
        if (stderr != null)
            stderr.close();
    }

    private void calcTime(String timeStr) {
        String[] parts = timeStr.split(":");
        hours = Integer.parseInt(parts[0]);
        minutes = Integer.parseInt(parts[1]);
        seconds = Float.parseFloat(parts[2]);
    }

    public String getFfmpegApp() {
        return ffmpegApp;
    }

    public void setFfmpegApp(String ffmpegApp) {
        this.ffmpegApp = ffmpegApp;
    }

    public int getHours() {
        return hours;
    }

    public void setHours(int hours) {
        this.hours = hours;
    }

    public int getMinutes() {
        return minutes;
    }

    public void setMinutes(int minutes) {
        this.minutes = minutes;
    }

    public float getSeconds() {
        return seconds;
    }

    public void setSeconds(float seconds) {
        this.seconds = seconds;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeigt() {
        return heigt;
    }

    public void setHeigt(int heigt) {
        this.heigt = heigt;
    }

    public static void main(String[] args) {
        VideoInfo videoInfo = new VideoInfo("D:\\hbk\\ffmpeg-20170718-012620a-win64-static\\bin\\ffmpeg.exe");
        try {
            videoInfo.getInfo("D:\\hbk\\hbk.mp4");
            System.out.println(videoInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
	
//截取视频几时几分几秒的图片


	package com.cmcc.core.Util;
	
	import java.io.BufferedReader;
	import java.io.IOException;
	import java.io.InputStream;
	import java.io.InputStreamReader;
	
	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) {
	        VideoThumbTaker videoThumbTaker = new VideoThumbTaker("/usr/local/Cellar/ffmpeg/4.0.1/bin/ffmpeg");
	        try {
	            videoThumbTaker.getThumb("/yidong/work_day/11/1106/aa.avi", "/yidong/work_day/11/1106/aa.png", 800, 600, 0, 1, 5);
	            System.out.println("over");
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }
	}


//封装工具类,截取中间时间的视频截图

package com.cmcc.core.Util;

import java.io.IOException;
/**
 * 视频、图片工具类
 * @author lvyangfan
 *
 */
public class VideoUtils {

	public static void main(String[] args) {
	
		
		try {
			getCenterImg("/usr/local/Cellar/ffmpeg/4.0.1/bin/ffmpeg","/yidong/work_day/11/1106/aa.avi","/yidong/work_day/11/1106/aa.png");
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
		}
	}
	/**
	* 获取视频中间时间点的截图
	* @param ffmpegPath 本地ffmpeg工具位置
	* @param videoPath 视频位置
	* @param imgPath 生成图片位置
	* @throws IOException
	* @throws InterruptedException
	*/
	public static void getCenterImg(String ffmpegPath,String videoPath,String imgPath) throws IOException, InterruptedException {

		VideoInfo videoInfo = new VideoInfo(ffmpegPath);
		videoInfo.getInfo(videoPath);
		System.out.println(videoInfo);
		int hours = videoInfo.getHours();
		int minutes = videoInfo.getMinutes();
		float seconds = videoInfo.getSeconds();
		float total = hours * 3600 + minutes * 60 + seconds;
		total = total / 2;

		hours = (int) (total / 3600);
		minutes = (int) ((total % 3600) / 60);
		seconds = total - hours * 3600 - minutes * 60;
		
	    VideoThumbTaker videoThumbTaker = new VideoThumbTaker(ffmpegPath);
		videoThumbTaker.getThumb(videoPath, imgPath, 800, 600, hours, minutes, seconds);
         System.out.println("over");

	}

}



 

Logo

更多推荐