继上个版本 对其进行略微升级 🤓
上个版本仅对一个视频进行抽帧处理
此版本可对一个文件夹内的全部视频进行抽帧并对应的文件夹进行帧图片的保存
1️⃣配置pom.xml (保持上次不变)
<dependencies><dependency><groupId>org.bytedeco</groupId><artifactId>javacv-platform</artifactId><version>1.5.7</version></dependency></dependencies>
2️⃣创建Util工具类 (本次所有方法都在次类中)
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;public class Util {//读取文件夹的内容并执行1️⃣创建文件夹2️⃣抽帧public static String ReadFolderFiles(String address) {//存放读取到的路径ArrayList<String> strings = new ArrayList<>();// 文件夹路径String folderPath = address;// 创建File对象File folder = new File(folderPath);// 检查文件夹是否存在if (folder.exists() && folder.isDirectory()) {int num;String strCreate = "";String strFrame = "";// 获取文件夹中的所有文件名File[] files = folder.listFiles();// 将子文件夹的名称添加到ArrayList中并过滤掉当前文件夹名for (File file : files) {if (!file.getName().equals(".DS_Store")) {strings.add(file.getName());}}num = strings.size();// 输出文件名数组for (String fileName : strings) {String oldfileName = address+"/"+fileName;String str = fileName.substring(0,fileName.indexOf('.'));String newFileName = address+"/"+str;strCreate = CreateFolder(newFileName);strFrame = FrameExtractor(oldfileName);}return String.format(strCreate+"\n已对%d个视频进行抽帧\n"+strFrame+"至对应文件夹",num);} else {return "指定的文件夹不存在或不是一个文件夹";}}//1️⃣根据视频名称创建对应文件夹private static String CreateFolder(String fileName){String str;// 文件夹名String folderName = fileName;// 创建一个File对象,表示文件夹路径File folder = new File(folderName);// 使用mkdir()方法创建文件夹if (!folder.exists()) {if (folder.mkdir()) {str = "文件夹创建成功!";} else {str = "文件夹创建失败!";}} else {str = "文件夹已存在!";}return str;}//2️⃣将视频转为帧图片public static String FrameExtractor(String address){String str;// 视频文件路径String videoFilePath = address;// 创建FFmpegFrameGrabber对象FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(videoFilePath);try {// 打开视频文件grabber.start();// 逐帧抓取视频Frame frame;int frameNumber = 1;//保存路径String createaddress = address.substring(0,address.indexOf("."));while ((frame = grabber.grabImage()) != null) {BufferedImage bufferedImage = frameToBufferedImage(frame);// 生成保存帧图像的文件路径String outputFilePath = String.format("%s/frame_%05d.jpg", createaddress, frameNumber);// 保存帧图像到文件try {ImageIO.write(bufferedImage, "jpg", new File(outputFilePath));} catch (IOException e) {e.printStackTrace();str = "保存失败";}frameNumber++;}// 关闭视频文件grabber.stop();str = "保存成功";} catch (org.bytedeco.javacv.FrameGrabber.Exception e) {e.printStackTrace();str = "保存失败";}return str;}//将Frame转换为BufferedImageprivate static BufferedImage frameToBufferedImage(Frame frame) {Java2DFrameConverter converter = new Java2DFrameConverter();return converter.getBufferedImage(frame);}
}
3️⃣运行结果