Windows上实现mp3转amr的方式,我这里提供两种转换方式
方法一:
先导入maven依赖
<dependency>
<groupId>com.github.dadiyang</groupId>
<artifactId>jave</artifactId>
<version>1.0.5</version>
</dependency>
代码实现:
public void mp3AmrTest01(){
File sourceFile = new File("C:\\Users\\Administrator\\Desktop\\project\\cs.mp3");//输入
File targetFile = new File("C:\\Users\\Administrator\\Desktop\\project\\ceshi.amr");//输出
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libamr_nb");//编码器
audio.setBitRate(12200);//比特率
audio.setChannels(1);//声道;1单声道,2立体声
audio.setSamplingRate(8000);//采样率(重要!!!)
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("amr");//格式
attrs.setAudioAttributes(audio);//音频设置
Encoder encoder = new Encoder();
try {
encoder.encode(sourceFile, targetFile, attrs);
} catch (EncoderException e) {
e.printStackTrace();
}
}
注意:使用这个方法实现转换后,运行环境换成Linux后,处理起来很麻烦。
方法二:
maven依赖
<!-- https://mvnrepository.com/artifact/ws.schild/jave-core -->
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-core</artifactId>
<version>2.4.4</version>
</dependency>
<!-- 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>
代码实现:
public void Mp3Amr(){
File source = new File("C:\\Users\\Administrator\\Desktop\\mp3\\ces.mp3");
File target = new File("C:\\Users\\Administrator\\Desktop\\amr\\bbb.amr");
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 (InputFormatException e) {
e.printStackTrace();
} catch (EncoderException e) {
e.printStackTrace();
}
}
注意:使用这个方法转换后,运行环境换成Linux上的话,只需要修改maven依赖即可
如下所示:
<!-- https://mvnrepository.com/artifact/ws.schild/jave-core -->
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-core</artifactId>
<version>2.4.4</version>
</dependency>
<!-- 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>
如果你的是苹果电脑,可以将maven换成以下来尝试一下,因为我的电脑系统是windows,所以没有去尝试。
<!-- https://mvnrepository.com/artifact/ws.schild/jave-core -->
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-core</artifactId>
<version>2.4.4</version>
</dependency>
<!-- 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>