使用场景:

业务中需要使用微信语音接口,由于微信上传语音只保存3天,所以需要将语音文件下载到服务器。

但是amr格式文件,前端无法识别,需要将其转换为mp3格式。

装换方法如下:

1、需安装ffmpeg软件(安装方法自行百度)

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;
    }

 

收藏 打印