Java将视频转为缩略图--ffmpeg

Java生成视频缩略图

对于上传视频生成缩略图使用的是ffmpeg进行生成的。

自己在网上找了如何进行编译ffmpeg的方法

但是感觉太复杂了

就到csdn上找到了编译好的ffmpeg文件



体会到ffmpeg非常强大,大部分流行的视频格式的都能生成缩略图

现在开始

首先下载ffmpeg解压

方法一:

建立一个bat文件

start

E:/ffmpeg/bin/ffmpeg.exe -i %1 -ss 20 -vframes 1 -r 1 -ac 1 -ab 2 -s 160*120 -f image2 %2

exit

说明下使用红色吧标记的意义

E:/ffmpeg/bin/ffmpeg.exe ffmpeg的路径

%1 %2 和C语言有点相似是为传参数保留位置

20 要截取多少秒后的图片

打开MyEclipse,建立一个工程 ,以及一个java文件

package test;

import java.io.IOException;

public class Ffmpeg {
public static void main(String[] args) {
// 视频文件
String videoRealPath = "F://ffmpeg//Wildlife.wmv";
// 截图的路径(输出路径)
String imageRealPath = "F://ffmpeg//a.jpg";
String basePath = "F://ffmpeg";
try {
// 调用批处理文件
Runtime.getRuntime().exec(
"cmd /c start " + basePath + "//ffmpeg.bat " + basePath + " " + videoRealPath + " "
+ imageRealPath);
} catch (IOException e) {
e.printStackTrace();
}
}

}

方法二: 

不需建立bat文件

/**
* 将视频转为缩略图 成功返回true,失败返回false
* @param exePath 可执行程序路径
* @param videoPath 视频路径
* @param imagepath 转换后图片路径
* @return
*/
public static boolean videoToImg(String exePath,String videoPath,String imagepath) {
File file = new File(videoPath);
if (!file.exists()) {
System.err.println("路径[" + videoPath + "]对应的视频文件不存在!");
return false;
}
List<String> commands = new java.util.ArrayList<String>();
commands.add(exePath);
commands.add("-i");
commands.add(videoPath);
commands.add("-ss");
// 要截取多少秒后的图片(在此截取1秒后的图片)
commands.add("1");
commands.add("-vframes");
commands.add("1");
commands.add("-r");
commands.add("1");
commands.add("-ac");
commands.add("1");
commands.add("-ab");
commands.add("2");
commands.add("-s");
commands.add("320*240");
commands.add("-f");
commands.add("image2");
commands.add(imagepath);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commands);
builder.start();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

 public static void main(String[] args)throws Exception{
        //1.获取当前工程目录,获取ffmpeg.exe
        File directory = new File(".");
        String root = directory.getCanonicalPath();
        String ffmpegPath = root + "\ffmpeg\ffmpeg.exe";
         
        //2.看不懂的童鞋们去读幼儿园吧(执行了后,刷新一下工程,注意观察你的工程目录中的image文件夹里,和控制台打印内容,成功了吧!)
        String videoPath=root + "\video\demo.mp4";
        String imagePath=root + "\image\ceshi.jpg";
         
        System.out.println("执行结果"+getVideoImage(ffmpegPath,videoPath,imagePath));
         
    }
/**
     * 获得视频缩略图,获取成功返回true,获取失败返回false
     * @param ffmpegPath  是ffmpeg.exe存放的路径
     * @param path   是视频文件的存放路径
     * @param outImagePath 输出缩略图的保存路径
     * @return
     */
    public static boolean getVideoImage(String ffmpegPath,String path,String outImagePath) {
        File file = new File(path);
        if (!file.exists()) {//判断视频文件是否存在
            System.err.println("路径[" + path + "]对应的视频文件不存在!");
            return false;
        }
        //设置参数
        List<string> commands = new java.util.ArrayList<string>();
        commands.add(ffmpegPath);//这里设置ffmpeg.exe存放的路径
        commands.add("-i");
        commands.add(path);//这里是设置要截取缩略图的视频的路径
        commands.add("-y");
        commands.add("-f");
        commands.add("image2");
        commands.add("-ss");
        commands.add("2");//这里设置的是要截取视频开始播放多少秒后的图,可以自己设置时间
        commands.add("-t");
        commands.add("0.001");
        commands.add("-s");
        commands.add("320x240");//这里设置输出图片的大小
        commands.add(outImagePath);//这里设置输出的截图的保存路径
     
        try {
            //截取缩略图并保存 
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commands);
            builder.start();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
原文地址:https://www.cnblogs.com/shijiaoyun/p/4861716.html