1. 程式人生 > >JAVA 音訊轉換AMR 轉MP3,OS,Linux cent os 7

JAVA 音訊轉換AMR 轉MP3,OS,Linux cent os 7

場景

近期在做微信開發時,需要獲取使用者發給公眾服務號的語音留言。而從微信服務端下載來的語音格式卻是amr的格式,同樣的你手機錄音、Android語音等也都是生成amr格式檔案。但當你想在web頁面去播放此檔案時,就困難了。因為無論是當前HTML5的<audio>標籤,還是眾多的播放外掛都不支援amr格式檔案的播放。所以,你不得不先把它轉碼為常見的MP3等型別檔案。

maven

 <!-- https://mvnrepository.com/artifact/ws.schild/jave-core -->
        <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-core</artifactId>
            <version>2.4.4</version>
        </dependency>

從我的Mac book 開發環境開始玩。

public class AmrToMp3 {    public static void main(String[] args) throws Exception {
        changeTemp();
    }    public static void changeTemp() throws InputFormatException {
        File source = new File("/Users/daji/Downloads/1.amr");   //原始檔
        File target = new File("/Users/daji/Downloads/1.mp3");   //目標檔案
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("libmp3lame");
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat("mp3");
        attrs.setAudioAttributes(audio);
        Encoder encoder = new Encoder();        try {

            MultimediaObject multimediaObject  = new MultimediaObject(source);
            encoder.encode(multimediaObject,target, attrs);
        } catch (IllegalArgumentException | EncoderException e) {
            e.printStackTrace();
        }

    }

}

跑一下. GG

十二月 05, 2018 6:42:11 下午 ws.schild.jave.DefaultFFMPEGLocator copyFile
嚴重: Could not find ffmpeg executable for native/ffmpeg-x86_64-osx is the correct platform jar included?
Exception in thread "main" java.lang.NullPointerException
    at java.util.Objects.requireNonNull(Objects.java:203)
    at java.nio.file.Files.copy(Files.java:2984)
    at ws.schild.jave.DefaultFFMPEGLocator.copy(DefaultFFMPEGLocator.java:144)
    at ws.schild.jave.DefaultFFMPEGLocator.copyFile(DefaultFFMPEGLocator.java:123)
    at ws.schild.jave.DefaultFFMPEGLocator.<init>(DefaultFFMPEGLocator.java:84)
    at ws.schild.jave.Encoder.<init>(Encoder.java:80)
    at cn.hitstone.media.util.AmrToMp3.changeTemp(AmrToMp3.java:20)
    at cn.hitstone.media.util.AmrToMp3.main(AmrToMp3.java:10)

Process finished with exit code 1

意思就是要安裝一個ffmpeg-x86_64-osx

<!-- https://mvnrepository.com/artifact/ws.schild/jave-native-osx64 -->
        <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-native-osx64</artifactId>
            <version>2.4.4</version>
        </dependency>

搞定 so easy

Windows 版導這個

<!-- https://mvnrepository.com/artifact/ws.schild/jave-native-win64 --><dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-native-win64</artifactId>
    <version>2.4.4</version></dependency>

Linux 版導這個

<!-- https://mvnrepository.com/artifact/ws.schild/jave-native-linux64 --><dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-native-linux64</artifactId>
    <version>2.4.4</version></dependency>

測試結果

901.png