作者:井然洞中别样天 | 来源:互联网 | 2023-07-30 12:18
使用场景:
业务中需要使用微信语音接口,由于微信上传语音只保存3天,所以需要将语音文件下载到服务器。
但是amr格式文件,前端无法识别,需要将其转换为mp3格式。
装换方法如下:
1、需安装ffmpeg软件(安装链接https://www.cnblogs.com/zhangyin6985/p/6513483.html)
2、转换代码:
public static boolean amrToMp3(String localPath, String targetFilePath) {try {java.lang.Runtime rt = Runtime.getRuntime();String command = "ffmpeg -i " + localPath + " " + targetFilePath;System.out.println("command = " + command);Process proc = rt.exec(command);InputStream stderr = proc.getErrorStream();InputStreamReader isr = new InputStreamReader(stderr);BufferedReader br = new BufferedReader(isr);String line = null;StringBuffer sb = new StringBuffer();while ((line = br.readLine()) != null) {sb.append(line);}System.out.println("ffmpeg Process errorInfo: " + sb.toString());int exitVal = proc.waitFor();System.out.println("ffmpeg Process exitValue: " + exitVal);return true;} catch (Exception e) {System.out.println("ffmpeg exec cmd Exception " + e.toString());}return false;}