【视频抽帧】javacv实现视频抽帧

POM

        <!-- https://mvnrepository.com/artifact/org.bytedeco/javacv -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv</artifactId>
            <version>1.0</version>
        </dependency>

以下为全部代码,重要位置有注释说明

package com.kexuekt.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber.Exception;
import org.bytedeco.javacv.Java2DFrameConverter;

/**
 * 视频抽帧
 *
 */
public class VideoProcessor {
    
    

    public static void main(String[] args) throws Exception {
    
    
        String filePath = "C:\\Users\\Administrator\\Desktop\\11\\video";	//批量处理,视频所在文件夹
        String targetPath = "C:\\Users\\Administrator\\Desktop\\11\\pic\\";	//输出图片文件夹
        File file = new File(filePath);
        if (file.isDirectory()){
    
    
            File[] files = file.listFiles();
            for (int i=0; i<files.length; i++) {
    
    
                String fpath = files[i].getParent()+"\\"+files[i].getName();
                System.out.println(fpath);
                String fileName = files[i].getName();
                randomGrabberFFmpegImage(fpath, targetPath, fileName.substring(0, fileName.length()-4));
            }
        }
    }


    /**
     * 视频文件抽帧
     * @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();
        int ffLength = ff.getLengthInFrames();
        System.out.println(ffLength);
        System.out.println("视频长度: " + ff.getLengthInTime());
        System.out.println("帧率: " + ff.getFrameRate());

        String fram = ff.getFrameRate()+"";
        String framSubStr = fram.substring(0, fram.indexOf("."));
        int t = Integer.parseInt(framSubStr) * 2; // 根据帧率动态判断抽帧间隔 * 2表示每隔2秒抽帧
        Frame f;
        for (int k=0; k<ffLength; k++){
    
    
            f = ff.grabImage();
            if (k%t==0){
    
    
                doExecuteFrame(f, targerFilePath, targetFileName, k);
            }
        }
        ff.stop();
    }

    public static void doExecuteFrame(Frame f, String targerFilePath, String targetFileName, int index) {
    
    
        if (null == f || null == f.image) {
    
    
            return;
        }

        Java2DFrameConverter converter = new Java2DFrameConverter();

        String imageMat = "jpg";
        String FileName = targerFilePath + File.separator + targetFileName + "_" + index + "." + imageMat;
        BufferedImage bi = converter.getBufferedImage(f);
        File output = new File(FileName);
        try {
    
    
            ImageIO.write(bi, imageMat, output);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

}


猜你喜欢

转载自blog.csdn.net/qq_36881887/article/details/127120086