1.引入依赖,由于在Github上面的maven依赖,下载不下来,需要手动下载jar,然后安装到本地的maven仓库
it.sauronsoftware
jave
1.0.2
2.安装jar到本地
mvn install:install-file -Dfile=C:\Users\Administrator\Downloads\jave-1.0.2.jar -DgroupId=it.sauronsoftware -DartifactId=jave -Dversion=1.0.2 -Dpackaging=jar
3.进行获取视频的时长
Encoder encoder = new Encoder();
MultimediaInfo m = encoder.getInfo(targetFile);
long ms = m.getDuration();
int ss = 1000;
int mi = ss * 60;
int hh = mi * 60;
int dd = hh * 24;
long day = ms / dd;
long hour = (ms - day * dd) / hh;
long minute = (ms - day * dd - hour * hh) / mi;
long second = (ms - day * dd - hour * hh - minute * mi) / ss;
String strHour = hour < 10 ? "0" + hour : "" + hour;//小时
String strMinute = minute < 10 ? "0" + minute : "" + minute;//分钟
String strSecond = second < 10 ? "0" + second : "" + second;//秒
if (strHour.equals("00")) {
return strMinute + ":" + strSecond;
} else {
return strHour + ":" + strMinute + ":" + strSecond;
}
4.VideoUtil(摘录)
@Slf4j
@Component
public class VideoUtil {
/**
* @Description: 获取视频时长(时分秒)
* @author: Hanweihu
* @date: 2019/7/30 8:35
* @params: [file]
* @return: java.lang.String
*/
public String ReadVideoTimeMs(MultipartFile file) {
Encoder encoder = new Encoder();
long ms = 0;
try {
// 获取文件类型
String fileName = file.getContentType();
// 获取文件后缀
String pref = fileName.indexOf("/") != -1 ? fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length()) : null;
String prefix = "." + pref;
// 用uuid作为文件名,防止生成的临时文件重复
final File excelFile = File.createTempFile(UUID.randomUUID().toString().replace("-", ""), prefix);
// MultipartFile to File
file.transferTo(excelFile);
MultimediaInfo m = encoder.getInfo(excelFile);
ms = m.getDuration();
//程序结束时,删除临时文件
VideoUtil.deleteFile(excelFile);
} catch (Exception e) {
e.printStackTrace();
}
int ss = 1000;
int mi = ss * 60;
int hh = mi * 60;
int dd = hh * 24;
long day = ms / dd;
long hour = (ms - day * dd) / hh;
long minute = (ms - day * dd - hour * hh) / mi;
long second = (ms - day * dd - hour * hh - minute * mi) / ss;
String strHour = hour < 10 ? "0" + hour : "" + hour;//小时
String strMinute = minute < 10 ? "0" + minute : "" + minute;//分钟
String strSecond = second < 10 ? "0" + second : "" + second;//秒
if (strHour.equals("00")) {
return strMinute + ":" + strSecond;
} else {
return strHour + ":" + strMinute + ":" + strSecond;
}
}
/**
* @Description: 删除文件
* @author: Hanweihu
* @date: 2019/7/30 8:35
* @params: [files]
* @return: void
*/
private static void deleteFile(File... files) {
for (File file : files) {
if (file.exists()) {
file.delete();
}
}
}
}
5.图片获取 宽高
//获取视频的宽高
BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
if (bufferedImage!=null){
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
map.put("imageWidth",width);
map.put("imageHeight",height);
}
6.获取视频封面
public static String getTempPath(String tempPath, File file2, Map map) {
long currentTimeMillis = System.currentTimeMillis();
String[] split = tempPath.split("/");
String path="";
for (String s : split) {
if (!s.equals(split[split.length-1])){
path+=s+"/";
}
}
path+=currentTimeMillis+".jpg";
File targetFile = new File(path);
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
try {
if (file2.exists()) {
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(file2);
ff.start();
int ftp = ff.getLengthInFrames();
int flag = 0;
Frame frame = null;
while (flag <= ftp) {
//获取帧
frame = ff.grabImage();
//过滤前3帧,避免出现全黑图片
if ((flag > 3) && (frame != null)) {
break;
}
flag++;
}
ImageIO.write(FrameToBufferedImage(frame), "jpg", targetFile);
ff.close();
ff.stop();
System.out.println(path);
map.put("snapshoot",path);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static RenderedImage FrameToBufferedImage(Frame frame) {
//创建BufferedImage对象
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bufferedImage = converter.getBufferedImage(frame);
return bufferedImage;
}