package com.goldman.launcher;标签:file,封面,ffmpeg,process,截取,File,new,String From: https://www.cnblogs.com/cxxjohnson/p/17910569.html
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();
}
}
}
}
}