java读取mp3文件_java 读取音频/视频 文件时长

这是一个Java类,用于获取不同格式的音频和视频文件(如mp3、mp4、mov、wav、m4a)的播放时长。通过`getDuration`方法,根据文件路径或指定格式来计算文件的时长,单位为毫秒或秒。同时,提供了从URL下载文件到临时路径并计算时长的功能。
摘要由CSDN通过智能技术生成

1 packagecom.puxinwangxiao.mts.util;2

3

4 importcom.coremedia.iso.IsoFile;5

6 importjava.io.File;7 importjava.io.IOException;8 importjava.net.UnknownHostException;9

10

11 public classVideoUtil {12

13

14 /**

15 * 获取视频文件的播放长度(mp4、mov格式)16 *@paramvideoPath17 *@return单位为毫秒18 */

19 public static long getMp4Duration(String videoPath) throwsIOException {20 IsoFile isoFile = newIsoFile(videoPath);21 long lengthInSeconds =

22 isoFile.getMovieBox().getMovieHeaderBox().getDuration() /

23 isoFile.getMovieBox().getMovieHeaderBox().getTimescale();24 returnlengthInSeconds;25 }26

27

28 /**

29 * 得到语音或视频文件时长,单位秒30 *@paramfilePath31 *@return

32 *@throwsIOException33 */

34 public static long getDuration(String filePath) throwsIOException {35 String format =getVideoFormat(filePath);36 long result = 0;37 if("wav".equals(format)){38 result =AudioUtil.getDuration(filePath).intValue();39 }else if("mp3".equals(format)){40 result =AudioUtil.getMp3Duration(filePath).intValue();41 }else if("m4a".equals(format)) {42 result =VideoUtil.getMp4Duration(filePath);43 }else if("mov".equals(format)){44 result =VideoUtil.getMp4Duration(filePath);45 }else if("mp4".equals(format)){46 result =VideoUtil.getMp4Duration(filePath);47 }48

49 returnresult;50 }51

52 /**

53 * 得到语音或视频文件时长,单位秒54 *@paramfilePath55 *@return

56 *@throwsIOException57 */

58 public static long getDuration(String filePath,String format) throwsIOException {59 long result = 0;60 if("wav".equals(format)){61 result =AudioUtil.getDuration(filePath).intValue();62 }else if("mp3".equals(format)){63 result =AudioUtil.getMp3Duration(filePath).intValue();64 }else if("m4a".equals(format)) {65 result =VideoUtil.getMp4Duration(filePath);66 }else if("mov".equals(format)){67 result =VideoUtil.getMp4Duration(filePath);68 }else if("mp4".equals(format)){69 result =VideoUtil.getMp4Duration(filePath);70 }71

72 returnresult;73 }74

75

76 /**

77 * 得到文件格式78 *@parampath79 *@return

80 */

81 public staticString getVideoFormat(String path){82 return path.toLowerCase().substring(path.toLowerCase().lastIndexOf(".") + 1);83 }84

85 public static voidmain(String[] args){86 /*

87 //网络文件88 String path = "https://resource.puxinwangxiao.com/gaozhongbibeiwenyanwengushici_5.mp3" ;89 long result = 0;90 try {91 String tmppath = getFileByUrl(path);92 result = getDuration(tmppath);93 } catch (IOException e) {94 e.printStackTrace();95 }96 System.out.println(result);97 */

98

99

100 //本地文件

101 String path = "/Users/liuwen/Downloads/temp/语音测试文件/xiaoshizi.wav";102 long result = 0;103 try{104 result =getDuration(path);105 } catch(IOException e) {106 e.printStackTrace();107 }108 System.out.println(result);109

110 }111 public static String getFileByUrl(String url) throwsUnknownHostException, IOException{112 File tmpFile = File.createTempFile("temp", url.substring(url.lastIndexOf(".")));//创建临时文件

113 Image2Binary.toBDFile(url, tmpFile.getCanonicalPath());114 returntmpFile.getCanonicalPath();115 }116

117 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值