场景近期在做微信开发时,需要获取用户发给公众服务号的语音留言。而从微信服务端下载来的语音格式却是amr的格式,同样的你手机录音、Android语音等也都是生成amr格式文件。但当你想在web页面去播放此文件时,就困难...
场景
近期在做微信开发时,需要获取用户发给公众服务号的语音留言。而从微信服务端下载来的语音格式却是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>
测试结果
沃梦达教程
本文标题为:JAVA 音频转换AMR 转MP3,OS,Linux cent os 7
基础教程推荐
猜你喜欢
- java基础知识之FileInputStream流的使用 2023-08-11
- JDK数组阻塞队列源码深入分析总结 2023-04-18
- ConditionalOnProperty配置swagger不生效问题及解决 2023-01-02
- springboot自定义starter方法及注解实例 2023-03-31
- java实现多人聊天系统 2023-05-19
- Java文件管理操作的知识点整理 2023-05-19
- Java实现线程插队的示例代码 2022-09-03
- Java实现查找文件和替换文件内容 2023-04-06
- Java数据结构之对象比较详解 2023-03-07
- Java并发编程进阶之线程控制篇 2023-03-07