读取视频文件输出到指定目录

  • 最近公司要求把一堆视频文件放到指定数据库里 最后实在是太多了 受不了了 写个程序偷个懒
需求 : 把视频名称及对应的在文件服务器的路径和视频第一帧图片放到对应数据库中
  • 下面吧添加数据库的代码删除了 有需要可小伙伴可以自己根据自己的业务添加

        <!--获取视频第一帧-->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv</artifactId>
            <version>0.8</version>
        </dependency>


package com.example.upload.controller;


import com.example.upload.mapper.LivebroadcastMapper;
import com.example.upload.pojo.Livebroadcast;
import org.apache.commons.lang.RandomStringUtils;
import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;


@RestController
public class IOutil {

     @GetMapping("/test")
    public void test(String inpath,String outPathImg,String outPathVideo) throws Exception {
        String path = "F:\\download\\压缩片\\" + inpath;
        String outPath = "F:\\download\\" + outPathImg + "\\";
        String outPathVi = "F:\\download\\" + outPathVideo + "\\";
        File file = new File(path);
        //获取所有文件和文件夹
        File[] files = file.listFiles();
        for (File fi : files) {
            String random = RandomStringUtils.randomNumeric(16);
            String fileName = random + ".mp4";
            String imageName = random + ".jpg";
            //判断是否是文件
            if (fi.isFile()) {
                //获取视频第一帧并输出到指定文件夹
                IOutil.fetchFrame(fi.toString(),outPath + imageName);
                //读取流
                FileInputStream fr = new FileInputStream(fi);
                //输出流
                FileOutputStream fw = new FileOutputStream(outPathVi + fileName);

                byte[] chars = new byte[1024];
                int len;
                while ((len = fr.read(chars)) != -1){
                    fw.write(chars,0,len);
                }
                fr.close();
                fw.close();
            }
        }
    }

    /**
     *
     * @param videofile : 视频路径  C:\Users\dongdong\Desktop\压缩片\test\image\video.mp4
     * @param framefile : 图片存储路径 C:\Users\dongdong\Desktop\压缩片\test\image\image.jpg
     * @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 f = null;
        while (i < lenght) {
            // 过滤前5帧,避免出现全黑的图片,依自己情况而定
            f = ff.grabFrame();
            if ((i > 5) && (f.image != null)) {
                break;
            }
            i++;
        }
        opencv_core.IplImage img = f.image;
        int owidth = img.width();
        int oheight = img.height();
        // 对截取的帧进行等比例缩放
        int width = 800;
        int height = (int) (((double) width / owidth) * oheight);
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        bi.getGraphics().drawImage(f.image.getBufferedImage().getScaledInstance(width, height, Image.SCALE_SMOOTH),
                0, 0, null);
        ImageIO.write(bi, "jpg", targetFile);
        //ff.flush();
        ff.stop();
        System.out.println(System.currentTimeMillis() - start);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值