獲取微信公眾號臨時素材音頻并轉war格式
絕對親測好用支持windows和linux
如果linux使用的話需要去ffmpge官網下載工具
第一步:

第二步: 首先選擇linux系統

第三步: 選擇適合自己的ffmpeg版本

我選擇的是這個版本感覺很好用

第四步:安裝ffmpeg
把下載下來的安裝包放到/usr/local下面執行命令解壓就可以了
tar -xvf ffmpeg-3.3.4-64bit-static.tar.xz
進入ffmpeg包
cd ffmpeg-3.3.4-64bit-static
執行命令測驗工具是否好用
./ffmpeg -i /home/uploadPath/5c31f4a5-f421-407f-b89f-19c7083bf008.amr /home/uploadPath/123.wav
出現這種代碼就是說明轉換成功

Java代碼
首先前端會傳過微信的mediaId直接用這個去微信臨時素材庫獲取音頻
@PostMapping("/downloadVoice")
public AjaxResult downloadVoice(String mediaId) throws Exception {
Map<String, Object> map = new HashMap<>();
System.out.println("mediaId=======:" + mediaId);
Map<String, String> accessTokenMap = GetAccessTokenUtil.getAccessToken(
WxPayConfig.appid, WxPayConfig.AppSecret);
String accessToken = accessTokenMap.get("accessToken");
System.out.println("accessToken:=========" + accessToken);
InputStream inputStream = getInputStream(accessToken, mediaId);
String name = UUID.randomUUID().toString();
//獲取音頻要寫入的路徑
String fileUrl = "/home/uploadPath/" + DateUtils.getYear() + "/" + DateUtils.getMonth() + "/" + DateUtils.getDay() + "/";
File f = new File(fileUrl);
if (!f.exists()) {
f.mkdir();
}
String destination = fileUrl + name + ".amr";
int index;
byte[] bytes = new byte[1024];
FileOutputStream downloadFile = new FileOutputStream(destination);
while ((index = inputStream.read(bytes)) != -1) {
downloadFile.write(bytes, 0, index);
downloadFile.flush();
}
inputStream.close();
downloadFile.close();
//把amr格式的轉換為wav格式并回傳歷經
String s = convertAmr2Wav(destination);
return AjaxResult.success(s);
}
getInputStream類
自己可以抽成工具類,獲取微信臨時素材庫的音頻流
private static InputStream getInputStream(String accessToken, String mediaId) {
InputStream is = null;
String url = WxPayConfig.voice_url +
"?access_token=" + accessToken + "&media_id=" + mediaId;
try {
URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet
.openConnection();
http.setRequestMethod("GET"); // 微信要求是get請求方式
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 連接超時30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 讀取超時30秒
http.connect();
// 獲取檔案轉化為byte流
is = http.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
return is;
}
convertAmr2Wav類
音頻轉換類,自己可以抽出來,我比較懶,真的是懶得弄,哈哈
//amrFilePath amr的源路徑
private static String convertAmr2Wav(String amrFilePath) {
File source = new File(amrFilePath);
String extension = amrFilePath.substring(amrFilePath.lastIndexOf("."));
String targetFilename = amrFilePath.replace(extension, ".wav");
//獲取系統是 windows還是linux
String os = System.getProperties().getProperty("os.name").toLowerCase();
if (os.startsWith("win")) {
File target = new File(targetFilename);
Encoder encoder = new Encoder();
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("wav");
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame");
attrs.setAudioAttributes(audio);
try {
encoder.encode(source, target, attrs);
} catch (Exception e) {
e.printStackTrace();
}
} else {
//配置linux ffmpge
String command = "/usr/local/ffmpeg-3.3.4-64bit-static/ffmpeg -i " + amrFilePath + " " + targetFilename;
try {
Runtime.getRuntime().exec(command);
} catch (IOException e) {
e.printStackTrace();
}
}
//回傳轉換后的路徑
return targetFilename;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/139772.html
標籤:其他
下一篇:撰寫Java程式,使用ThreadLocal類,專案中創建賬戶類 Account,類中包括賬戶名稱name、 ThreadLocal 類的參考變數amount,表示存款
