java获取视频第一帧

参考链接:
http://www.zuidaima.com/blog/3702403061419008.htm

方案一(未在项目中使用)

①需要到jar包

      
       org.bytedeco  
       javacv-platform  
       1.3.1  
   

②代码


import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber.Exception;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author xiaoqiang
 * @Description javacv获取视频第一帧图片工具类
 * @date 2019/5/8 10:38
 */
public class FrameGrabberKit {

    public static void main(String[] args) throws Exception {
// 视频路径换成自己的http://www.baidu.com/group1/M00/00/9E/rBIuClzSQHaAPClMAA5OVT6OM7w328.mp4

        randomGrabberFFmpegImage("http://www.baidu.com/group1/M00/00/9E/rBIuClzSQHaAPClMAA5OVT6OM7w328.mp4", "F:\\test", "111");
    }

    /**
     *
     * @param filePath 视频路径
     * @param targerFilePath 第一帧图片存储位置
     * @param targetFileName 图片名称
     * @throws Exception
     */
    public static void randomGrabberFFmpegImage(String filePath, String targerFilePath, String targetFileName)
            throws Exception {

        FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(filePath);
        ff.start();
        String rotate =ff.getVideoMetadata("rotate");
        Frame f;
        int i = 0;
        while (i <1) {
            f =ff.grabImage();
            IplImage src = null;
            if(null !=rotate &&rotate.length() > 1) {
                OpenCVFrameConverter.ToIplImage converter =new OpenCVFrameConverter.ToIplImage();
                src =converter.convert(f);
                f =converter.convert(rotate(src, Integer.valueOf(rotate)));
            }
            doExecuteFrame(f,targerFilePath,targetFileName);
            i++;
        }

        ff.stop();

    }

    /*
     * 旋转角度的
     */
    public static IplImage rotate(IplImage src, int angle) {
        IplImage img = IplImage.create(src.height(), src.width(), src.depth(), src.nChannels());
        opencv_core.cvTranspose(src, img);
        opencv_core.cvFlip(img, img, angle);
        return img;
    }

    public static void doExecuteFrame(Frame f, String targerFilePath, String targetFileName) {

        if (null ==f ||null ==f.image) {
            return;
        }
        Java2DFrameConverter converter =new Java2DFrameConverter();
        String imageMat ="jpg";
        String FileName =targerFilePath + File.separator +targetFileName +"." +imageMat;
        BufferedImage bi =converter.getBufferedImage(f);
        System.out.println("width:" + bi.getWidth());
        System.out.println("height:" + bi.getHeight());
        File output =new File(FileName);
        try {
            ImageIO.write(bi,imageMat,output);
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

方案二(FFmpeg 项目中已使用)

1.安装FFmpeg 参考官网http://www.ffmpeg.org/download.html
linux安装

2.代码

/**
 * @author xiaoqiang
 * @Description FFmpeg 获取第一帧(路径可以根据需要自己修改)
 * @date 2019/5/29 10:21
 */
public class FirstFrameUtil {
    //windows
    //private static  String FFMPEG_PATH = "D:/soft/ffmpeg/bin/ffmpeg.exe";
    //centos
    private static  String FFMPEG_PATH = "/usr/bin/ffmpeg";
    /**
     * FFmpeg
     * @param veidoPath
     * @return
     */
    public static boolean processImg(String veidoPath) {
        File file = new File(veidoPath);
        if (!file.exists()) {
            System.err.println("路径[" + veidoPath + "]对应的视频文件不存在!");
            return false;
        }
        List commands = new java.util.ArrayList();
        commands.add(FFMPEG_PATH);
        commands.add("-i");
        commands.add(veidoPath);
        commands.add("-y");
        commands.add("-f");
        commands.add("image2");
        commands.add("-ss");
        // 这个参数是设置截取视频多少秒时的画面
        commands.add("1");
        // commands.add("-t");
        // commands.add("0.001");
        commands.add("-s");
        commands.add("700x525");
        commands.add(veidoPath.substring(0, veidoPath.lastIndexOf("."))
                .replaceFirst("vedio", "file") + ".jpg");
        try {
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commands);
            builder.start();
            System.out.println("截取成功");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

你可能感兴趣的:(java获取视频第一帧)