ffmpeg截取视频封面图

centos上安装ffmpeg  
CentOS 、Ubuntu安装ffmpeg(超简单,只需两步)

package com.goldman.launcher;


import java.io.*;

public class VideoThumbnail {
    public static void main(String[] args) {
        File folder = new File("D://");
        File[] files = folder.listFiles(); // 获取目录下所有文件和文件夹
        for (File file : files) {
            if (file.isFile() && file.getName().endsWith(".mp4")) {
                String videoPath = file.getAbsoluteFile().getPath();
                String thumbnailPath = videoPath.replace(".mp4", ".jpg");
                File jpgfile = new File(thumbnailPath);
                if (jpgfile.exists()) {
                    jpgfile.delete();
                }
                try {
                    ProcessBuilder processBuilder = new ProcessBuilder("ffmpeg", "-i", videoPath, "-ss", String.valueOf(2), "-vframes", "1", thumbnailPath);
                    processBuilder.redirectErrorStream(true);
                    Process process = processBuilder.start();

                    // 读取子进程的输出流
                    InputStream inputStream = process.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        System.out.println(line);
                    }

                    int exitCode = process.waitFor();
                    if (exitCode == 0) {
                        System.out.println("视频图片截取成功!");
                    } else {
                        System.out.println("视频图片截取失败!");
                    }
                } catch (IOException | InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
View Code

 

posted @ 2023-12-18 10:57  chenxiangxiang  阅读(58)  评论(0编辑  收藏  举报