知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁(yè) » 新聞資訊 » 公眾號(hào)相關(guān) >
獲取微信公眾號(hào)臨時(shí)素材音頻并轉(zhuǎn)war格式
發(fā)表時(shí)間:2020-10-19
發(fā)布人:葵宇科技
瀏覽次數(shù):73
獲取微信公眾號(hào)臨時(shí)素材音頻并轉(zhuǎn)war格式
絕對(duì)親測(cè)好用支持windows和linux
如果linux使用的話需要去ffmpge官網(wǎng)下載工具
第一步:
第二步: 首先選擇linux系統(tǒng)
第三步: 選擇適合自己的ffmpeg版本
我選擇的是這個(gè)版本感覺很好用
第四步:安裝ffmpeg
把下載下來的安裝包放到/usr/local下面執(zhí)行命令解壓就可以了
tar -xvf ffmpeg-3.3.4-64bit-static.tar.xz
進(jìn)入ffmpeg包
cd ffmpeg-3.3.4-64bit-static
執(zhí)行命令測(cè)試工具是否好用
./ffmpeg -i /home/uploadPath/5c31f4a5-f421-407f-b89f-19c7083bf008.amr /home/uploadPath/123.wav
出現(xiàn)這種代碼就是說明轉(zhuǎn)換成功
Java代碼
首先前端會(huì)傳過微信的mediaId直接用這個(gè)去微信臨時(shí)素材庫(kù)獲取音頻
@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();
//把a(bǔ)mr格式的轉(zhuǎn)換為wav格式并返回歷經(jīng)
String s = convertAmr2Wav(destination);
return AjaxResult.success(s);
}
getInputStream類
自己可以抽成工具類,獲取微信臨時(shí)素材庫(kù)的音頻流
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請(qǐng)求方式
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 連接超時(shí)30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 讀取超時(shí)30秒
http.connect();
// 獲取文件轉(zhuǎn)化為byte流
is = http.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
return is;
}
convertAmr2Wav類
音頻轉(zhuǎn)換類,自己可以抽出來,我比較懶,真的是懶得弄,哈哈
//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");
//獲取系統(tǒng)是 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();
}
}
//返回轉(zhuǎn)換后的路徑
return targetFilename;
}
相關(guān)案例查看更多
相關(guān)閱讀
- 網(wǎng)站建設(shè)開發(fā)
- 云南軟件公司
- 小程序被騙退款成功
- 前端技術(shù)
- 小程序用戶登錄
- 軟件定制公司
- 云南企業(yè)網(wǎng)站
- 網(wǎng)站建設(shè)方法
- 云南小程序開發(fā)
- 云南旅游網(wǎng)站建設(shè)
- 網(wǎng)站建設(shè)首選公司
- 網(wǎng)站建設(shè)報(bào)價(jià)
- 霸屏推廣
- 定制小程序開發(fā)
- 網(wǎng)絡(luò)公司電話
- 微信小程序開發(fā)入門課程
- 云南網(wǎng)絡(luò)公司
- 云南網(wǎng)站建設(shè)服務(wù)公司
- 網(wǎng)站建設(shè)公司哪家好
- 網(wǎng)站制作
- 昆明軟件公司
- 云南etc微信小程序
- 小程序被騙
- 前端
- 網(wǎng)站搭建
- 云南網(wǎng)站建設(shè)價(jià)格
- 汽車拆解管理系統(tǒng)
- 云南網(wǎng)站建設(shè)外包
- painter
- 買小程序被騙