获取微信公众号临时素材音频并转war格式
发表时间:2020-10-19
发布人:葵宇科技
浏览次数:58
获取微信公众号临时素材音频并转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;
}