El entorno de Linux java intercepta un cuadro de video y lo guarda como una miniatura

1. Introducir dependencias
Dado que todo el paquete es demasiado grande, aquí solo se introducen algunos recursos relacionados con las funciones.

<!-- 视频截图工具START -->
	<dependency>
	    <groupId>org.bytedeco</groupId>
	    <artifactId>javacv</artifactId>
	    <version>1.4.4</version>
	    <exclusions>
	        <exclusion>
	            <groupId>org.bytedeco</groupId>
	            <artifactId>javacpp</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>flycapture</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>libdc1394</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>libfreenect</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>libfreenect2</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>librealsense</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>videoinput</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>opencv</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>tesseract</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>leptonica</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>flandmark</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>artoolkitplus</artifactId>
	        </exclusion>
	    </exclusions>
	</dependency>
	<dependency>
	    <groupId>org.bytedeco</groupId>
	    <artifactId>javacv-platform</artifactId>
	    <version>1.4.4</version>
	    <exclusions>
	        <exclusion>
	            <groupId>org.bytedeco</groupId>
	            <artifactId>javacv</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>flycapture-platform</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>libdc1394-platform</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>libfreenect-platform</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>libfreenect2-platform</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>librealsense-platform</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>videoinput-platform</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>opencv-platform</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>tesseract-platform</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>leptonica-platform</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>flandmark-platform</artifactId>
	        </exclusion>
	        <exclusion>
	            <groupId>org.bytedeco.javacpp-presets</groupId>
	            <artifactId>artoolkitplus-platform</artifactId>
	        </exclusion>
	    </exclusions>
	</dependency>
<!-- 视频截图工具END -->

Si no es un proyecto mvn, puede descargar el siguiente paquete jar por separado e importarlo, o hacer clic aquí para empaquetar y descargar

# ffmpeg-4.1-1.4.4*.jar 主要是为了适配各种环境,可根据实际需求修改
ffmpeg-4.1-1.4.4.jar
ffmpeg-4.1-1.4.4-windows-x86_64.jar
ffmpeg-4.1-1.4.4-windows-x86.jar
ffmpeg-4.1-1.4.4-macosx-x86_64.jar
ffmpeg-4.1-1.4.4-linux-ppc64le.jar
ffmpeg-4.1-1.4.4-linux-armhf.jar
ffmpeg-4.1-1.4.4-linux-x86_64.jar
ffmpeg-4.1-1.4.4-linux-x86.jar
ffmpeg-4.1-1.4.4-android-x86_64.jar
ffmpeg-4.1-1.4.4-android-x86.jar
ffmpeg-4.1-1.4.4-android-arm64.jar
ffmpeg-4.1-1.4.4-android-arm.jar
ffmpeg-platform-4.1-1.4.4.jar
javacpp-1.4.4.jar
javacv-1.4.4.jar
javacv-platform-1.4.4.jar

2. programa de captura de pantalla java

package com.rangz.common.util;

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

import javax.imageio.ImageIO;

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

/**
 * 视频工具类
 * @author RANGZHI
 */
public class VideoUtils {
    
    
    /**
     * 获取指定视频的帧并保存为图片至指定目录
     * @param videofile  源视频文件路径
     * @param framefile  截取帧的图片存放位置
     * @throws Exception
     */
    public static void fetchFrame(String videofile, String framefile) throws Exception {
    
    
        long start = System.currentTimeMillis();
        File targetFile = new File(framefile);
        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videofile); 
        ff.start();
        int lenght = ff.getLengthInFrames();
        int i = 0;
        Frame frame = null;
        while (i < lenght) {
    
     
            //此处网上分享经验,保证不会截取到黑屏,大都从第5帧截取,根据实际需求自行修改
            frame = ff.grabFrame();
            if(frame.image != null) break;
            i++;
        }
        BufferedImage img = FrameToBufferedImage(frame);
        int owidth = img.getWidth();
        int oheight = img.getHeight();
        // 对截取的帧进行等比例缩放
        int width = 350;
        int height = (int) (((double) width / owidth) * oheight);
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        bi.getGraphics().drawImage(img.getScaledInstance(width, height, Image.SCALE_SMOOTH),
                0, 0, null);
        ImageIO.write(bi, "jpg", targetFile);
        ff.flush();
        ff.stop();
        ff.close();
        System.out.println("生成视频缩略图,耗时:"+(System.currentTimeMillis() - start)+"ms");
    }
    /**
     * 图片资源Frame转BufferedImage
     * @param frame
     * @return
     */
    public static BufferedImage FrameToBufferedImage(Frame frame) {
    
    
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage bufferedImage = converter.getBufferedImage(frame);
        return bufferedImage;
    }
    
    
    public static void main(String[] args) {
    
    
        String videofile = "/home/rangz/upload/202303/202303018997.mp4";
        String framefile = "/home/rangz/upload/202303/202303018997scale.jpg";
        try {
    
    
            fetchFrame(videofile, framefile);
        } catch (Exception e) {
    
    
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

3. Ejemplo de interceptación
inserte la descripción de la imagen aquí

Supongo que te gusta

Origin blog.csdn.net/qq_42049516/article/details/129280063
Recomendado
Clasificación