java:视频帧截图

程序所需要的java包:https://download.csdn.net/download/tobe_coder/11484718

java:视频帧截图_第1张图片

代码实现: 

package com.example;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.UUID;

public class VideoGetPicUtil {

	//存放截取视频某一帧的图片存放位置
	public static String videoFramesPath = "E:/test/img/";
	/**
	 * 获取视频第五帧的图片
	 * @param videoFileName 视频全路径
	 * @return
	 */
	public static String grabberVideoFramer(String videoFileName){
		//最后获取到的视频的图片的路径
		String videPicture="";
		//Frame对象
		Frame frame = null;
		//标识
		int flag = 0;
		try {
			//获取视频文件
			FFmpegFrameGrabber fFmpegFrameGrabber = new FFmpegFrameGrabber(videoFileName);
			fFmpegFrameGrabber.start();
			
            //获取视频总帧数
			int ftp = fFmpegFrameGrabber.getLengthInFrames();
			System.out.println("时长 " + ftp / fFmpegFrameGrabber.getFrameRate() / 60);
			
			while (flag <= ftp) {
				frame = fFmpegFrameGrabber.grabImage();
				//对视频的第五帧进行处理
				if (frame != null && flag==5) {
					//文件绝对路径+名字
					String fileName = videoFramesPath + UUID.randomUUID().toString()+"_" + String.valueOf(flag) + ".jpg";
					//文件储存对象
					File outPut = new File(fileName);
					ImageIO.write(FrameToBufferedImage(frame), "jpg", outPut);
					
					//视频第五帧图的路径
					String savedUrl = videoFramesPath + outPut.getName();
					videPicture=savedUrl;
					break;
				}
				flag++;
			}
			fFmpegFrameGrabber.stop();
			fFmpegFrameGrabber.close();
		} catch (Exception E) {
			E.printStackTrace();
		}
		return videPicture;
	}

	public static BufferedImage FrameToBufferedImage(Frame frame) {
		//创建BufferedImage对象
		Java2DFrameConverter converter = new Java2DFrameConverter();
		BufferedImage bufferedImage = converter.getBufferedImage(frame);
		return bufferedImage;
	}
	/**
	 * 测试:
	 * @param args
	 */
	public static void main(String[] args) {
		String videoFileName = "E:/test/video/test.mp4";
		grabberVideoFramer(videoFileName);
	}
}

 注:简单记录视频帧截图工具代码;若jar包不好获取,可以创建maven项目,在pom.xml中加入如下即可:


    org.bytedeco
    javacv
    1.4.3



    org.bytedeco.javacpp-presets
    ffmpeg-platform
    4.0.2-1.4.3

 

你可能感兴趣的:(Java)