今天在做需求的时候遇到一个问题,就是获取上传音频文件、视频文件的播放时长。虽然时长问题可以在前段通过加载获取到。但是最后还是决定使用Java,来获取时长。百度了很多,但是发现都不完整,所以用这篇博客来记录一下。
该工程使用的是SpringBoot。由于在maven仓库中没有 jave-1.0.2.jar 这个jar,所以需要自己去下载到本地。
jave-1.0.2.jar 下载地址:http://www.java2s.com/Code/Jar/j/Downloadjave102jar.htm
下面是具体实现代码
pom.xml 文件
<dependency>
<groupId>it.sauronsoftware</groupId>
<artifactId>jave</artifactId>
<version>1.0.2</version>
<!-- 下面两个是使用system在pom.xml中导入jar -->
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/jave-1.0.2.jar</systemPath>
</dependency>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--将本地使用的jar打包的时候引入lib-->
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
<!-- 下面是为了打包的时候能够将jave-1.0.2.jar 排查,防止静态资源打进去 -->
<resources>
<resource>
<directory>${basedir}src/main/resources</directory>
<excludes>
<exclude>lib/*.jar</exclude>
</excludes>
</resource>
</resources>
UploadFileController.java
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
/**
* 上传文件接口
*
* @author Ink-足迹
* @create 2018-06-01 16:58
**/
@RestController
@RequestMapping("common")
public class UploadFileController {
private static final String FOLDER_PATH = "course/";
/**
* 上传文件
*
* @param folder 文件夹名, iamge=图片,voice=音频,video=视频,ppt=ppt...
* @param file 上传的文件
* @return
* @throws Exception
* @author Ink-足迹
*/
@PostMapping(value = "/uploadFile/{folder}")
public ServerResponse<JSONObject> uploadFile(@PathVariable("folder") String folder, @RequestParam("file") MultipartFile file) throws Exception {
long duration = 0;//音频长度,秒
String fileName = file.getOriginalFilename();
String suffix = fileName.substring(fileName.lastIndexOf("."));
StringBuilder sb = new StringBuilder();
sb.append(FOLDER_PATH).append(folder).append("/");
String path = "";
// 获取音频文件的时长
if (StringUtils.equals("voice", folder) || StringUtils.equals("video", folder)) {
duration = FileUploadUtil.getVideoDuration(file);
}
JSONObject result = new JSONObject();
result.put("url", path);
result.put("name", fileName);
result.put("size", file.getSize() + "");
result.put("duration", duration);
return ServerResponse.createBySuccess(result);
}
}
获取时长工具类 FileUploadUtil.java
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
/**
* 文件上传工具类
*
* @author Ink-足迹
* @create 2018-06-13 20:13
**/
public class FileUploadUtil {
private static final Logger log = LoggerFactory.getLogger(FileUtil.class);
/**
* 获取时长
*
* @param file 音频/视频文件
* @return
*/
public static long getVideoDuration(MultipartFile file) {
long duration = 0;
try {
File source = new File(getFilePath() + file.getOriginalFilename());
file.transferTo(source);
Encoder encoder = new Encoder();
MultimediaInfo info = encoder.getInfo(source);
duration = info.getDuration();
if (source.exists()) {
source.delete();
}
return duration / 1000;
} catch (Exception e) {
log.error("获取时长", e);
return duration;
}
}
/**
* 获取路径
*
* @return
* @throws Exception
*/
private static String getFilePath() throws Exception {
//获取当前文件的根路径
File path = new File(ResourceUtils.getURL("classpath:").getPath());
if (!path.exists()) path = new File("");
//盘符路径
StringBuilder codeUrl = new StringBuilder();
codeUrl.append(path.getAbsolutePath()).append("/static/video/");
File file = new File(codeUrl.toString());
if (!file.exists()) {
file.mkdirs();
}
return codeUrl.toString();
}
}
————————————
转载自:https://blog.csdn.net/suifengerye/article/details/97113146