<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.5</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.5</version>
</dependency>
import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import java.util.Map;
public class Test {
public static void generatePreviewVideo(String sourcePath, String targetPath, long startMin, long durationMin) {
try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(sourcePath);
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(targetPath, grabber.getImageWidth(), grabber.getImageHeight(), 2)) {
grabber.start();
recorder.setImageWidth(grabber.getImageWidth());
recorder.setImageHeight(grabber.getImageHeight());
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
recorder.setFormat("mov");
recorder.setFrameRate(grabber.getFrameRate());
recorder.setSampleRate(grabber.getSampleRate());
recorder.setAudioChannels(grabber.getAudioChannels());
recorder.setFrameRate(grabber.getFrameRate());
grabber.setTimestamp(startMin * 60 * 1000000);
Map<String, Object> filter = MapUtil.filter(BeanUtil.beanToMap(grabber));
BeanUtil.copyProperties(filter, recorder);
recorder.start();
Frame inFrame;
int count = 0;
while ((inFrame = grabber.grab()) != null) {
if (count > durationMin*60*100) {
break;
}
count++;
recorder.record(inFrame);
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
public static void main(String[] args) {
String inFile = "/Users/reedfan/Desktop/监控系统.mov";
String outFile = "/Users/reedfan/Desktop/test1.mov";
generatePreviewVideo(inFile, outFile,1,1);
}
}