微信公衆號錄音文件(amr文件轉mp3)

音頻轉碼工具,主要用於將微信語音 amr 格式轉換爲 mp3 格式以便在 html5 的 audio 標籤中進行播放php

1.調用微信提供的接口獲取錄音的InputStream字節流
public InputStream getInputStream(String mediaId) {
    InputStream is = null;
    try {
        String URL_DOWNLOAD_TEMP_MEDIA = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID";
        String url = URL_DOWNLOAD_TEMP_MEDIA.replace("ACCESS_TOKEN", "本身寫代碼獲取accessToken").replace("MEDIA_ID", mediaId);
        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;
}
複製代碼
2.將獲取到的字節流保存爲amr文件
public String downloadMediaId(HttpServletRequest request, String mediaId) {
    String relfilePath = null;
    InputStream inputStream = getInputStream(mediaId);
    FileOutputStream fileOutputStream = null;
    try {
        //服務器資源保存路徑
        String savePath = request.getSession().getServletContext().getRealPath("/") + "upload/" + DateUtil.getYear() + "/wxmedia/audio/";
        savePath = savePath + "audio/"; 
        String filename = String.valueOf(System.currentTimeMillis()) + ".amr";
        relfilePath = "upload/" + DateUtil.getYear() + "/wxmedia/audio/" + filename;
        File file = new File(savePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        byte[] data = new byte[1024];
        int len = 0;
        fileOutputStream = new FileOutputStream(savePath + filename);
        while ((len = inputStream.read(data)) != -1) {
            // 判斷結果是否有錯
            if (new String(data).indexOf("errmsg") > -1) {
                return null;
            }
            fileOutputStream.write(data, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fileOutputStream != null) {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return relfilePath;
}
複製代碼

3.將保存的amr文件轉成mp3文件html

public void amrToMp3(String sourcePath, String targetPath) {
    File source = new File(sourcePath);
    File target = new File(targetPath);
    AudioUtils.amrToMp3(source, target);
}
複製代碼

4.所需的jar包依賴html5

<!--amr文件轉音頻map文件-->
<dependency>
    <groupId>com.github.dadiyang</groupId>
    <artifactId>jave</artifactId>
    <version>1.0.3</version>
</dependency>
複製代碼

音頻轉碼工具

支持 Linux/Windows/Mac 平臺java

由於是基於 JAVE 項目的修改,而 JAVE 是依賴 ffmpeg 因此能夠適用於全部 ffmpeg 所支持的文件格式的轉換。具體能夠查看 JAVE 官方文檔git

原理

  1. 初始化時判斷當前運行環境,將bin目錄中對應的 ffmpeg 可執行文件拷貝到臨時目錄中
  2. 根據文件類型及配置經過 Runtime.getRuntime().exec(cmd) 執行 ffmpeg 對應的轉碼命令

JAVE 項目的問題

ffmpeg 是依賴運行環境的,JAVE 項目封裝了ffmpeg,它經過上述的原理使 java 能夠調用ffmpeg並且支持跨平臺。github

  1. 項目老舊沒再維護。官網最近版本是2009年發佈的,其依賴的ffmpeg早已過期,不少狀況下用不了。
  2. 轉碼一直報異常 EncoderException: Stream mapping
  3. 沒有發佈maven倉庫,並且 JAVE 自己也不是一個maven項目
  4. 不支持mac

本項目特色

本項目爲解決上述問題而生。api

  • 這是一個maven項目,並且已發佈到中央倉庫
  • 項目依賴的 ffmpeg 可執行文件通過驗證可使用(單元測試中提供了一個簡單的檢驗方法)
  • 解決了amr轉mp3出現的 EncoderException: Stream mapping
  • 支持 Linux/Windows/Mac 平臺

擴展

若是程序沒法經過拷貝資源文件的方式獲取到 ffmpeg 的可執行文件或者內置的 ffmpeg 不支持你所使用的操做系統服務器

你能夠經過環境變量或者在 java 中設置 System.setProperty("ffmpeg.home", "ffmpeg可執行文件所在的目錄") 的方式指定你的系統中安裝的可用的 ffmpeg 文件的目錄微信

如 System.setProperty("ffmpeg.home", "/usr/local/bin/")markdown

相關文章
相關標籤/搜索