Java 图片、视频生成缩略图

标签: 视频第一帧  图片缩略图  FFmpeg  后台生成缩略图

在后台将图片、视频、生成缩略图

前言
对于视频上传在后台生成缩略图。

1 下载FFmpeg

1.1 参考地址:http://www.ffmpeg.org/download.html在这里插入图片描述

1.2 下载此文件(.zip文件),进行解压;设定无汉字的目录(java中用的到)
在这里插入图片描述
1.3 在java中为此文件设定路径----(ffmpeg_path)
在这里插入图片描述

2 后台代码

public static boolean processImg(String veido_path, String ffmpeg_path, String thumb_path) {
        File file = new File(veido_path);
        if (!file.exists()) {
            System.err.println("路径[" + veido_path + "]对应的视频文件不存在!");
            return false;
        }
        List<String> commands = new java.util.ArrayList<String>();
        commands.add(ffmpeg_path);  //ffmpeg.exe存放路径
        commands.add("-i");
        commands.add(veido_path);  //视频路径--如:(F:\FileRecv\test.mp4)
        commands.add("-y");
        commands.add("-f");
        commands.add("image2");
        commands.add("-t");
        commands.add("0.001");
        commands.add("-s");
        commands.add("320x240");
        commands.add(thumb_path+ ".jpg");  //缩略图路径--如:(F:\FileRecv\test.jpg)
        try {
			/*多线程*/
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commands);
            builder.start();
            return true;
			/*单线程*/
			ProcessBuilder builder = new ProcessBuilder();
            builder.command(commands);
            Process process = builder.start();
            process.waitFor();
            int result = process.exitValue();
            System.out.println("result:" + result);
            if (result != 0) {
                return false;
            }else{
                return true;
            }
			
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    public static boolean sltimg (String veido_path, String thumb_path){
            try {
                File fi = new File(veido_path); //大图文件
                File fo = new File(thumb_path+ ".jpg"); //将要转换出的小图文件
                int nw = 100;
                AffineTransform transform = new AffineTransform();
                BufferedImage bis = ImageIO.read(fi); //读取图片
                int w = bis.getWidth();
                int h = bis.getHeight();
                //double scale = (double)w/h;
                int nh = (nw*h)/w ;
                double sx = (double)nw/w;
                double sy = (double)nh/h;
                transform.setToScale(sx,sy); //setToScale(double sx, double sy) 将此变换设置为缩放变换。
                AffineTransformOp ato = new AffineTransformOp(transform,null);
                BufferedImage bid = new BufferedImage(nw,nh,BufferedImage.TYPE_3BYTE_BGR);
                ato.filter(bis,bid);
                ImageIO.write(bid,"jpeg",fo);
                return true;
            } catch(Exception e) {
                e.printStackTrace();
                return false;
            }
    }

作者:liuqing
来源:CSDN
原文:https://blog.csdn.net/qq_39711356/article/details/90644568
版权声明:本文为博主原创文章,转载请附上博文链接!

原文链接:加载失败,请重新获取