獲取七牛網絡音頻時長


雲存儲一般都提供有獲取音頻詳細信息的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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM