java ffmpeg (Linux)截取视频做封面

转载:https://blog.csdn.net/qq_34479912/article/details/80361874?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param

转载:https://blog.csdn.net/qq_30499453/article/details/104049439

1.安装yasm

 wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz

 tar -zxvf yasm-1.3.0.tar.gz

 cd yasm-1.3.0

./configure

make && make install

2.下载ffmpeg

wget http://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2

tar -xjvf ffmpeg-snapshot.tar.bz2(此步骤编译过程有点长,请耐心等待)

./configure --enable-shared --prefix=/monchickey/ffmpeg;(prefix后面是你想安装的位置)

make install;

3.安装成功后,前往cd /monchickey/ffmpeg(共有bin,include,lib,share这4个目录,其中bin是ffmpeg主程序二进制目录,include是c/c++头文件目录,lib是编译好的库文件目录,share是文档目录)

进入bin目录执行 ./ffmpeg -version 查看当前版本的详细信息,默认情况下一般会报libavdevice.so.57: cannot open shared object file: No such file or directory,原因是lib目录未加载到链接到系统库中,系统ld目录列表在/etc/ld.so.conf中,打开文件会发现,里面引用了/etc/ld.so.conf.d/下面所有的.conf文件,比如mariadb-x86_64.conf我们只需要创建一个文件并写入lib路径即可,执行命令: vim /etc/ld.so.conf.d/ffmpeg.conf 然后添加一行内容: /monchickey/ffmpeg/lib 之后保存并退出,然后执行 ldconfig 使配置生效,现在再次执行 ./ffmpeg -version 显示就正常了

4.执行cmd命令

/monchickey/ffmpeg/bin/./ffmpeg -ss 00:00:00 -i  你的视频地址  -f image2 -y /a.jpg       /a.jpg是指截图的位置

5.java代码

/**
 * java调用FFmpeg截取视频做封面入口
 * @param videoPath 视频路径
 * @param ffmpegPath  FFmpeg安装路径(Windows使用)
 * @param coverPath 封面存储路径
 */
public static void processImgMain(String videoPath,String ffmpegPath,String coverPath){
    if(ProjectConfig.ENV.equals(ProjectConfig.PROJECT_DEVLELOP)){//此处判断是否Windows,自己更改一下呗
        processImg(videoPath,ffmpegPath,coverPath);
    }else {
        String command = "/monchickey/ffmpeg/bin/./ffmpeg -ss 00:00:00 -i " + videoPath
                + " -f image2 -y " + coverPath;
        processImgCmd(command);
    }
}

/**
 * 截取视频封面 java  Windows版本
 * @param videoPath  视频路径
 * @param ffmpegPath  ffmpeg.exe存放路径  截取工具
 * @param imgPath  封面存放路径
 * @return
*/
public static boolean processImg(String videoPath,String ffmpegPath,String imgPath){
    File file = new File(videoPath);
    if (!file.exists()) {
        System.err.println("路径[" + videoPath + "]对应的视频文件不存在!");
        return false;
    }
    List<String> commands = new ArrayList<String>();
    commands.add(ffmpegPath);
    commands.add("-i");
    commands.add(videoPath);
    commands.add("-y");
    commands.add("-f");
    commands.add("image2");
    commands.add("-ss");
    commands.add("0");//这个参数是设置截取视频多少秒时的画面
    //commands.add("-t");
    //commands.add("0.001");
  commands.add("-s");
    commands.add("700x525");
    commands.add(imgPath);
    try {
        ProcessBuilder builder = new ProcessBuilder();
        builder.command(commands);
        builder.start();
        System.out.println("截取成功");
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
/**
 * FFmpeg截取视频封面Linux版本
 * @param command 执行cmd命令
 */
public static void processImgCmd(String command){
    try {
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(command);
        InputStream stderr = proc.getErrorStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        System.out.println("<ERROR>");
        while ((line = br.readLine()) != null){
            System.out.println(line);
        }
        System.out.println("</ERROR>");
        int exitVal = proc.waitFor();
        System.out.println("Process exitValue: " + exitVal);
    } catch (Throwable t) {
        System.out.println(t);
        t.printStackTrace();
    }
}
原文地址:https://www.cnblogs.com/xiaokangk/p/13920305.html