java后台amr格式转mp3格式方法

方法一: 使用ffmpeg 软件。

public void changeAmrToMp3(String sourcePath, String targetPath) throws IllegalArgumentException, EncoderException {

                // 获取文件地址。追踪到软件所在文件夹ji

                String webroot = configParamService.getpValueBypName("ffmpeg_path");

Runtime run = null;
try {
run = Runtime.getRuntime();
long start = System.currentTimeMillis();
System.out.println(new File(webroot).getAbsolutePath());
// 执行ffmpeg.exe,前面是ffmpeg.exe的地址,中间是需要转换的文件地址,后面是转换后的文件地址。-i是转  换方式,意思是可编码解码,mp3编码方式采用的是libmp3lame
Process p = run.exec(new File(webroot).getAbsolutePath() + "/ffmpeg -i " + sourcePath
+ " -acodec libmp3lame " + targetPath);
// 释放进程
p.getOutputStream().close();
p.getInputStream().close();
p.getErrorStream().close();
p.waitFor();
long end = System.currentTimeMillis();
System.out.println(sourcePath + " convert success, costs:" + (end - start) + "ms");
} catch (Exception e) {
e.printStackTrace();
} finally {
// run调用lame解码器最后释放内存
run.freeMemory();
}

}

方法二:使用jar包(jave-1.0.2.jar)。

1.下载完jar包以后导入maven项目下。在pom文件添加:

<dependency>
<groupId>org.jave</groupId>
<artifactId>jave</artifactId>
<version>1.0.2</version>
</dependency>

2.添加完后更新maven,一定要更新maven。。。

3.方法代码:

/**

* @Title: changeToMp3
* @Description: amr转mp3
* @author yjs
* @date 2018年4月19日 下午4:18:38
* @param sourcePath 被转文件地址
* @param targetPath 转换后保存地址
*/
public static void changeToMp3(String sourcePath, String targetPath) {
File source = new File(sourcePath);
int i = targetPath.lastIndexOf("/") + 1;
String substring = targetPath.substring(0, i);
// 如果文件存放路径不存在,则mkdir一个
File path = new File(substring);
if (!path.exists()) {
path.mkdirs();
}
File target = new File(targetPath);
AudioAttributes audio = new AudioAttributes();
Encoder encoder = new Encoder();


audio.setCodec("libmp3lame");
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mp3");
attrs.setAudioAttributes(audio);


try {
encoder.encode(source, target, attrs);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (EncoderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


猜你喜欢

转载自blog.csdn.net/qq_32734273/article/details/80030845
今日推荐