<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.9</version>
</dependency>
<!--ffmpeg的这个版本一定要和后面的这个javacv版本对应-->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>6.0-1.5.9</version>
</dependency>
demo:功能是保存视频的每一个关键帧成图片
package org.example.test;
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.nio.Buffer;
public class JavaCvCoverTest {
public static void main(String[] args) {
FFmpegFrameGrabber fFmpegFrameGrabber = new FFmpegFrameGrabber("C:\\Users\\Downloads\\敢_杀_我_的_马_!.让子弹飞_总2.210738676.mp4");
try {
fFmpegFrameGrabber.start();
// System.out.println(fFmpegFrameGrabber.());
/**
* 视频长度
*/
int length = fFmpegFrameGrabber.getLengthInAudioFrames();
int i = 0 ; // 第几帧
Frame frame = null; // 每一帧的对象
while ((frame = fFmpegFrameGrabber.grabImage()) != null) {
i++;
if (i%20!=0)continue;//20帧保存一次
if (frame.image != null){
File outFile = new File("C:\\Users\\Desktop\\videoGuanJian\\"+i+".jpg"); // 保存目标路径
Java2DFrameConverter converter = new Java2DFrameConverter(); // 转换器
BufferedImage bufferedImage = converter.getBufferedImage(frame); // 获取图片数据
ImageIO.write(bufferedImage,"jpg", outFile); // 保存图片数据
System.out.println("第"+i+"帧");
}
}
} catch (FFmpegFrameGrabber.Exception e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String convertTimestampToTimeWithMicroseconds(long timestampMicroseconds) {
long totalSeconds = timestampMicroseconds / 1000000; // Convert microseconds to seconds
long microseconds = timestampMicroseconds % 1000000; // Remaining microseconds
long hours = totalSeconds / 3600;
long minutes = (totalSeconds % 3600) / 60;
long seconds = totalSeconds % 60;
return String.format("%02dh %02dm %02ds %03dms", hours, minutes, seconds, microseconds);
}
}