云存储一般都提供有获取音频详细信息的api
http://resource.puxinwangxiao.com/71d03d54014e5545e04a83d116c75c9b.wav?avinfo
文件保存在七牛云上,通过avinfo获取音频信息,部分信息如下:
"format": {
"nb_streams": 2,
"nb_programs": 0,
"format_name": "mp3",
"format_long_name": "MP2/3 (MPEG audio layer 2/3)",
"start_time": "0.025056",
"duration": "186.383673",
"size": "3463288",
"bit_rate": "148651",
"probe_score": 51,
"tags": {
"encoder": "Lavf57.71.100",
"comment": "163 key(Don't modify):L64FU3W4YxX3ZFTmbZ+8/WZdCobAYszsKwMY7rBUXdrqvxlDtoDwfXN0svnbokohrm954OJ2g5nz73AIsntvoi/1IPfHOFR5lwoH+zLEpkKdxQX2NGUHoPtCPDjxcI0ntcUnTW1oTpRmPNfXcgVbbmGnynGeGXwGZKwOXzTf89ZsLUs3i5pfakCGaiRQROUC2g1u+ycFqqXS2pmwbmCcSZeJwkJ/gW/0+fPqGckjhxalu8DfF1m1jIev8uvS2NH5juhYKW4UiABOCVPv86YPiclnXrj2OV3vIhLrmGS1P50mlJnmZSCLVkYs8kdfhhNXo9bNrbg4PTmp8R0Mkb+J22laG2ab1ZENxreGtmF9BcNoe8yScOhJVSLmNfSvjbK8a6C8io1nQcAU6AkYkXeoIj6/jVRj1ibCU3vB0oklPIAvl31yLkkoeDqn9q/xKoGGxpEx1Zz6CaOQNVkwPOcuWQJ6hfoWnfqWWyALwwXySkOxpjm4dKsroenUkjtrmyUgy3cJiCaxoZezSWwHT7+PtsdS0zhD2T61vLQFwmBtO1HphREAHKJEdDoIjm2+3HNC",
"album": "陪着你走 Accompany you go",
"title": "陪着你走",
"artist": "Gibb-Z/ICE",
"track": "1"
}}
其中duration为186秒。
1 /** 2 * 获取七牛音频时长 3 * @param audioUrl 4 * @return 5 */ 6 @PostMapping("/get_audio_time") 7 public Result getAudioTime(@RequestParam("audioUrl") String audioUrl) { 8 String audioTime = ""; 9 if(null != audioUrl && "".equals(audioUrl)) 10 return Result.get(Result.ERROR, "音频路径为空,无法获取时长", ""); 11 try { 12 HttpGet req = new HttpGet(audioUrl + "?avinfo"); 13 String rep = HttpClientUtils.execute(req); 14 if(StringUtils.isNotBlank(rep)){ 15 JSONObject repjson = JSONObject.fromObject(rep); 16 JSONObject repjson2 = (JSONObject) repjson.get("format"); 17 audioTime = String.valueOf(repjson2.get("duration")==null?"":repjson2.get("duration")); 18 if(audioTime == null) audioTime = ""; 19 if(audioTime.indexOf(".") > 0) audioTime = audioTime.substring(0,audioTime.indexOf(".")); 20 } 21 }catch (Exception e) { 22 logger.error(e.getMessage(),e); 23 return Result.get(Result.ERROR, e.getMessage(), e); 24 } 25 return Result.get(Result.OK, "",audioTime ); 26 }
1 package com.puxinwangxiao.mts.util; 2 3 import org.apache.http.HttpEntity; 4 import org.apache.http.StatusLine; 5 import org.apache.http.client.ClientProtocolException; 6 import org.apache.http.client.methods.CloseableHttpResponse; 7 import org.apache.http.client.methods.HttpUriRequest; 8 import org.apache.http.impl.client.CloseableHttpClient; 9 import org.apache.http.impl.client.HttpClients; 10 import org.apache.http.util.EntityUtils; 11 import org.slf4j.Logger; 12 import org.slf4j.LoggerFactory; 13 14 import java.io.IOException; 15 16 17 public class HttpClientUtils { 18 private static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class); 19 20 public static String execute(HttpUriRequest request){ 21 CloseableHttpClient client = HttpClients.createDefault(); 22 String responseStr =null; 23 CloseableHttpResponse response = null; 24 if(client!=null){ 25 try { 26 response = client.execute(request); 27 StatusLine status = response.getStatusLine(); 28 Integer code = status.getStatusCode(); 29 if(code==200){ 30 HttpEntity entity = response.getEntity(); 31 responseStr = EntityUtils.toString(entity); 32 33 } 34 } catch (ClientProtocolException e) { 35 logger.error("HttpClientUtils--execute:",e); 36 } catch (IOException e) { 37 logger.error("HttpClientUtils--execute:",e); 38 }finally{ 39 try { 40 response.close(); 41 } catch (IOException e1) { 42 e1.printStackTrace(); 43 } 44 // 关闭连接,释放资源 45 try { 46 client.close(); 47 } catch (IOException e) { 48 logger.error("HttpClientUtils--execute:",e); 49 } 50 } 51 } 52 return responseStr; 53 } 54 }