1.背景
网上的东西真实够坑的,用不了就算了,还基本是复制粘贴
2,解决
需要导包,必须是这两个版本
<dependency> <groupId>org.bytedeco</groupId> <artifactId>javacv</artifactId> <version>1.4.3</version> </dependency> <dependency> <groupId>org.bytedeco.javacpp-presets</groupId> <artifactId>ffmpeg-platform</artifactId> <version>4.0.2-1.4.3</version> </dependency>
封装的工具【不需要指定文件格式】
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//获取视频的第n帧画面 public static void getVideoSmallImg(String filePath, int n, String targetUrl, String fileType) { try { File file = new File(filePath); FFmpegFrameGrabber fFmpegFrameGrabber = new FFmpegFrameGrabber(file.getAbsolutePath()); // // 设置读取的最大数据,单位字节 // fFmpegFrameGrabber.setOption("probesize", "10000"); // // 设置分析的最长时间,单位微秒 // fFmpegFrameGrabber.setOption("analyzeduration", "10000"); // fFmpegFrameGrabber.setOption("rtsp_transport", "tcp"); fFmpegFrameGrabber.start(); //获取视频总帧数 int ftp = fFmpegFrameGrabber.getLengthInFrames(); System.out.println("时长 " + ftp / fFmpegFrameGrabber.getFrameRate() / 60); //标识 int flag = 0; //Frame对象 Frame frame = null; while (flag <= ftp) { frame = fFmpegFrameGrabber.grabImage(); if (frame != null && flag >= n - 1) { //文件储存对象 File outPut = new File(targetUrl); ImageIO.write(new Java2DFrameConverter().getBufferedImage(frame), fileType.replace(".", ""), outPut); break; } flag++; } fFmpegFrameGrabber.stop(); fFmpegFrameGrabber.close(); } catch (Exception e) { e.printStackTrace(); throw new CustomResultException(ResultEn.FILE_LOADUP_ERROR.getCode(), "视频文件解析缩略图失败:" + ExcBox.getExcMsg(e)); } }View Code
3.测试
结果
标签:java,String,int,flag,wmv,fFmpegFrameGrabber,mp4,File,new From: https://www.cnblogs.com/c2g5201314/p/16634653.html