1.在 maven 中引入依赖
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-all-deps</artifactId>
<version>2.5.1</version>
</dependency>
2.使用工具类
import ws.schild.jave.Encoder Exception ;
import ws.schild.jave.MultimediaInfo;
import ws.schild.jave.MultimediaObject;
import java .io. File ;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class AudioVideoUtil {
/**
* 获取音视频的大小
* @param filePath
* @return
*/
public static Long getSize(String filePath) {
FileChannel fc= null;
try {
FileInputStream fis = new FileInputStream(filePath);
fc= fis.getChannel();
Long videoSize=fc.size();
return videoSize;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 获取音视频的时长
* @param filePath
* @return
*/
public static Long getTime(String filePath){
try {
MultimediaObject instance=new MultimediaObject(new File(filePath));
MultimediaInfo result=instance.getInfo();
Long videoTime=result.getDuration()/ 1000;
return videoTime;
} catch (EncoderException e) {
e.printStackTrace();
}
return null;
}
}