您的位置:首页 > 编程语言 > Java开发

将amr文件转成mp3

2016-04-06 09:40 561 查看
package com.ericlin.dongjing.util;

import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncodingAttributes;

import java.io.File;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* 将amr文件转成mp3
* 不同操作系统使用不同方式
* linux服务器需要安装ffmpeg, 将手机语音amr转为mp3格式
*	安装参考:
*		centos yum install ffmpeg ffmpeg-devel
*		Ubuntu apt-get install ffmpeg
*		参考:https://www.ffmpeg.org/
*
* @author Ericlin
*/
public class Mp3Util {

private static Log log = LogFactory.getLog(Mp3Util.class);

/**
* 将amr格式转成mp3格式
* @param amrFilePath amr文件
* @return  mp3文件路径
*/
public static String convertAmr2Mp3(String amrFilePath) {
File source = new File(IConstants.UPLOAD_FILE_PATH + amrFilePath);
String extension = amrFilePath.substring(amrFilePath.lastIndexOf("."));
String targetFilename = amrFilePath.replace(extension, ".mp3");

String os = System.getProperties().getProperty("os.name").toLowerCase();
if (os.startsWith("win")) {
File target = new File(IConstants.UPLOAD_FILE_PATH + targetFilename);
Encoder encoder = new Encoder();
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mp3");
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame");
attrs.setAudioAttributes(audio);
try {
encoder.encode(source, target, attrs);
} catch (Exception e) {
log.error("convert amr to mp3 error", e);
}
} else {
String command = "ffmpeg -i " + IConstants.UPLOAD_FILE_PATH + amrFilePath + " " + IConstants.UPLOAD_FILE_PATH + targetFilename;
try {
Runtime.getRuntime().exec(command);
} catch (IOException e) {
log.error("convert amr to mp3 error", e);
}
}

return targetFilename;
}

public static void main(String[] args) {
String a = Mp3Util.convertAmr2Mp3("/223400017.amr");
System.out.println(a);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java mp3 amr