导入依赖
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>4.4-1.5.6</version>
</dependency>
代码示例
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.util.*;
public class Test {
public static void main (String [] args) throws Exception{
String fileUrl = "C:\\Users\\Administrator\\Desktop\\《摩擦力》说课.mp4";
FFmpegFrameGrabber fFmpegFrameGrabber = new FFmpegFrameGrabber(fileUrl);
fFmpegFrameGrabber.start();
Frame frame = null;
int frames = fFmpegFrameGrabber.getLengthInFrames();
int i = 0;
while (i <= frames){
frame = fFmpegFrameGrabber.grabImage();
if(frame!=null && i==10){
String fileName = "C:\\Users\\Administrator\\Desktop\\"+System.currentTimeMillis()+".jpg";
File file = new File(fileName);
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bufferedImage = converter.getBufferedImage(frame);
ImageIO.write(bufferedImage,"jpg",file);
break;
}
i++;
}
fFmpegFrameGrabber.stop();
fFmpegFrameGrabber.close();
}
运行效果
|