将amr文件转成mp3

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);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值