需要引入jar包 下载地址
http://www.sauronsoftware.it/projects/jave/download.php
maven首先要引入
<groupId>jave
<artifactId>jave
<version>1.0.2
<scope>system
<systemPath>${basedir}/lib/jave-1.0.2.jar
</dependency>
//这是一个外部的包,在根目录下创建lib文件夹,将包放进去
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItem;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
/**
* 用于处理视频时间/大小
*/
public class VideoUtil {
/**
* 获取视频时长(时分秒)
* @param file
* @return
*/
public static StringReadVideoTime(MultipartFile file) {
Encoder encoder =new Encoder();
String length ="";
try {
CommonsMultipartFile cf= (CommonsMultipartFile)file;
DiskFileItem fi = (DiskFileItem)cf.getFileItem();
File source = fi.getStoreLocation();
MultimediaInfo m = encoder.getInfo(source);
long ls = m.getDuration()/1000;
//时
int hour = (int) ls / (60 *60 *1000);
//分
int minute = (int) (ls % (60 *60 *1000)) /60000;
//秒
int second = (int) ((ls % (60 *60 *1000)) %60000) /1000;
//毫秒
// int minsecond = (int)(((ls % (60 * 60 * 1000)) % 60000) % 1000);
length = hour+":"+minute+":"+second+":";
}catch (Exception e) {
e.printStackTrace();
}
return length;
}
/**
* 获取视频时长(毫秒)
* @param file
* @return
*/
public static StringReadVideoTimeMs(MultipartFile file) {
Encoder encoder =new Encoder();
String length ="";
try {
// 获取文件类型
//这里解释下,因为前台传这个文件给起了别名,如果你的前台传值正常传直接用getOriginalFilename()
String fileName = file.getContentType();
//同理 正常情况下使用substring(fileName.lastIndexOf("."))即可
// 获取文件后缀
String pref = fileName.indexOf("/") != -1 ? fileName.substring(fileName.lastIndexOf("/") +1, fileName.length()) :null;
String prefix ="." + pref;
// 用uuid作为文件名,防止生成的临时文件重复
final File excelFile = File.createTempFile(UUIDUtil.getUUID(), prefix);
// MultipartFile to File
file.transferTo(excelFile);
MultimediaInfo m = encoder.getInfo(excelFile);
long ls = m.getDuration()/1000;
length = String.valueOf(ls);
//程序结束时,删除临时文件
VideoUtil.deleteFile(excelFile);
}catch (Exception e) {
e.printStackTrace();
}
return length;
}
/**
* 删除临时文件
* @param files
*/
private static void deleteFile(File... files) {
for (File file : files) {
if (file.exists()) {
file.delete();
}
}
}
}
如有侵权,联系我
网友评论