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

猜你喜欢

转载自blog.51cto.com/14112940/2326726