视频缩略图处理

最近上传视频和图片时需要展示缩略图的效果图给用户

pom依赖

<dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv</artifactId>
            <version>1.5.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.bytedeco</groupId>
                    <artifactId>flycapture</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.bytedeco</groupId>
                    <artifactId>libdc1394</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.bytedeco</groupId>
                    <artifactId>libfreenect</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.bytedeco</groupId>
                    <artifactId>libfreenect2</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.bytedeco</groupId>
                    <artifactId>librealsense</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.bytedeco</groupId>
                    <artifactId>videoinput</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.bytedeco</groupId>
                    <artifactId>artoolkitplus</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.bytedeco</groupId>
                    <artifactId>flandmark</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.bytedeco</groupId>
                    <artifactId>leptonica</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.bytedeco</groupId>
                    <artifactId>tesseract</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>ffmpeg</artifactId>
            <version>4.1.3-1.5.1</version>
            <classifier>windows-x86_64</classifier>
        </dependency>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>ffmpeg</artifactId>
            <version>4.1.3-1.5.1</version>
            <classifier>linux-x86_64</classifier>
        </dependency>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>openblas</artifactId>
            <version>0.3.6-1.5.1</version>
            <classifier>windows-x86_64</classifier>
        </dependency>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>openblas</artifactId>
            <version>0.3.6-1.5.1</version>
            <classifier>linux-x86_64</classifier>
        </dependency>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>opencv</artifactId>
            <version>4.1.0-1.5.1</version>
            <classifier>windows-x86_64</classifier>
        </dependency>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>opencv</artifactId>
            <version>4.1.0-1.5.1</version>
            <classifier>linux-x86_64</classifier>
        </dependency>

视频缩略图工具类

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

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;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.opencv.global.opencv_core;
import org.bytedeco.opencv.opencv_core.IplImage;

import cn.hutool.core.io.FileUtil;

/**
   * 视频缩略图工具类
 * @author GZR
 *
 */
public class ThumbUtil {
    
    private static final String ROTATE = "rotate";
    
    private static final String JPG = "jpg";
    
    /**
     * 截取视频缩略图
     * @param videoFileInPath    视频文件输入路径
     * @param thumbFileOutPath    缩略图文件输出路径
     * @throws Exception 
     */
    public static void captureVideoThumb(String videoFileInPath, String thumbFileOutPath) throws Exception {
        int width = 320;
        int height = 240;
        captureVideoThumb(videoFileInPath, thumbFileOutPath, width, height);
    }
    
    /**
     * 截取视频缩略图
     * @param videoFileInPath    视频文件输入路径
     * @param thumbFileOutPath    缩略图文件输出路径
     * @param width    缩略图宽(根据视频横向或纵向会自动交换宽高)
     * @param height 缩略图高(根据视频横向或纵向会自动交换宽高)
     * @throws Exception 
     */
    public static void captureVideoThumb(String videoFileInPath, String thumbFileOutPath, int width, int height) throws Exception {
        FFmpegFrameGrabber ff = null;
        try {
            ff = FFmpegFrameGrabber.createDefault(videoFileInPath);
            ff.start();
            ff.getAudioChannels();
            String rotate = ff.getVideoMetadata(ROTATE);    //视频的旋转角度
            int lenght = ff.getLengthInFrames();
            int i = 0;
            Frame f = null;
            int captureFrameIndex = 5;
            while (i < lenght) {
                // 取第5帧,避免出现全黑的图片
                f = ff.grabImage();
                if(i>captureFrameIndex && f.image != null){
                    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)));
                    }
                    if(f.imageWidth < f.imageHeight) {
                        int temp = width;
                        width = height;
                        height = temp;
                    }
                    doExecuteFrameResize(f, thumbFileOutPath, width, height);
                    //doExecuteFrame(f, thumbFileOutPath);
                    break;
                }
                i++;
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } finally {
            if(ff!=null) {
                try {
                    ff.close();
                } catch (java.lang.Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    private 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;
    }
    
    private static void doExecuteFrameResize(Frame f, String targetFileName, int width, int height) {
        if (null == f || null == f.image) {
            return;
        }
        
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage bi = converter.getBufferedImage(f);
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(bi, JPG, os);
            InputStream is = new ByteArrayInputStream(os.toByteArray());
            // 开始读取文件并进行压缩
            Image src = ImageIO.read(is);
            // 构造一个类型为预定义图像类型之一的 BufferedImage
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            //绘制图像  getScaledInstance表示创建此图像的缩放版本,返回一个新的缩放版本Image,按指定的width,height呈现图像
            //Image.SCALE_SMOOTH,选择图像平滑度比缩放速度具有更高优先级的图像缩放算法。
            tag.getGraphics().drawImage(src.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

            FileUtil.mkParentDirs(targetFileName);
            ImageIO.write(tag, JPG, new File(targetFileName));  
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @SuppressWarnings("unused")
    private static void doExecuteFrame(Frame f, String targerFilePath) {
        if (null == f || null == f.image) {
            return;
        }
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage bi = converter.getBufferedImage(f);
        File output = new File(targerFilePath);
        try {
            ImageIO.write(bi, JPG, output);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void captureImage(String filePath, String destFilePath) throws IOException{
        int width = 320;
        int height = 240;
        captureImage(filePath, destFilePath, width, height);
    }
    
    public static void captureImage(String filePath, String destFilePath, int width, int height) throws IOException {
//        FileUtil.copy(filePath, destFilePath, false);
        
        InputStream is = null;
        try {
            is = new FileInputStream(new File(filePath));
            // 开始读取文件并进行压缩
            Image image = ImageIO.read(is);
            
            if(image.getWidth(null)<image.getHeight(null)) {
                int temp = width;
                width = height;
                height = temp;
            }
            
            BufferedImage buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = buffer.createGraphics();
            try {
                g.drawImage(image, 0, 0, width, height, null);
            } finally {
                g.dispose();
            }
            ImageIO.write(buffer, JPG, new File(destFilePath));
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(is!=null) {
                try {
                    is.close();
                } catch (java.lang.Exception e) {
                    e.printStackTrace();
                }
            }
        }
        
    }
    
    
    public static void main(String[] args) throws Exception {
        for(int i=0;i<10;i++) {
            Date start = new Date();
            try {
                captureVideoThumb("E:\\test\\VID_20190602_104444.mp4", "E:\\test\\1.jpg");
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("耗时:" + (new Date().getTime()-start.getTime()));
        }
    }
}
 

ControllerTest

 //文件暂存的临时目录
File f = new File(uploadTempPath);
//获取文件名
String fileName = file.getOriginalFilename();
//存放路径
String savePath = uploadTempPath + File.separatorChar + fileName;
//上传文件到本地
File dest = new File(savePath);
//文件保存到本地暂存
file.transferTo(dest);
String thumbTempPath = uploadTempPath + File.separatorChar + "thumb" + File.separatorChar + id + ".jpg";
//截取视频缩略图
ThumbUtil.captureVideoThumb(savePath, thumbTempPath);
Bytes[] thumb = FileUtil.readBytes(thumbTempPath));
保存到数据库
Thumb.insert(thumb);
//删除临时文件
FileUtil.del(thumbTempPath);
//删除临时文件
dest.delete();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值