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);
}
}
將amr文件轉成mp3
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.