/*** Create By yxl on /6/5*/
public classAmrToMP3Utils {private static Logger logger =Logger.getLogger(AmrToMP3Utils.class);/*** 将amr文件输入转为mp3格式
*@paramfile
*@return
*/
public staticInputStream amrToMP3(MultipartFile file) {
String ffmpegPath=getLinuxOrWindowsFfmpegPath();
Runtime runtime=Runtime.getRuntime();try{
String filePath=copyFile(file.getInputStream(), file.getOriginalFilename());
String substring= filePath.substring(0, filePath.lastIndexOf("."));
String mp3FilePath= substring + ".mp3";//执行ffmpeg文件,将amr格式转为mp3//filePath ----> amr文件在临时文件夹中的地址//mp3FilePath ----> 转换后的mp3文件地址
Process p = runtime.exec(ffmpegPath + "ffmpeg -i " + filePath + " " + mp3FilePath);//执行ffmpeg.exe,前面是ffmpeg.exe的地址,中间是需要转换的文件地址,后面是转换后的文件地址。-i是转换方式,意思是可编码解码,mp3编码方式采用的是libmp3lame//释放进程
p.getOutputStream().close();
p.getInputStream().close();
p.getErrorStream().close();
p.waitFor();
File mp3File= newFile(mp3FilePath);
InputStream fileInputStream= newFileInputStream(mp3File);//应该在调用该方法的地方关闭该input流(使用完后),并且要删除掉临时文件夹下的相应文件
/*File amrFile = new File(filePath);
File mp3File = new File(mp3FilePath);
if (amrFile.exists()) {
boolean delete = amrFile.delete();
System.out.println("删除源文件:"+delete);
}
if (mp3File.exists()) {
boolean delete = mp3File.delete();
System.out.println("删除mp3文件:"+delete);
}*/
returnfileInputStream;
}catch(Exception e) {
e.printStackTrace();
}finally{
runtime.freeMemory();
}return null;
}/*** 将amr文件输入流转为mp3格式
*@paraminputStream amr文件的输入流(也可以是其它的文件流)
*@paramfileName 文件名(包含后缀)
*@return
*/
public staticInputStream amrToMP3(InputStream inputStream, String fileName) {
String ffmpegPath=getLinuxOrWindowsFfmpegPath();
Runtime runtime=Runtime.getRuntime();try{
String filePath=copyFile(inputStream, fileName);
String substring= filePath.substring(0, filePath.lastIndexOf("."));
String mp3FilePath= substring + ".mp3";//执行ffmpeg文件,将amr格式转为mp3//filePath ----> amr文件在临时文件夹中的地址//mp3FilePath ----> 转换后的mp3文件地址
Process p = runtime.exec(ffmpegPath + "ffmpeg -i" + " " +filePath + " " + mp3FilePath);//执行ffmpeg.exe,前面是ffmpeg.exe的地址,中间是需要转换的文件地址,后面是转换后的文件地址。-i是转换方式,意思是可编码解码,mp3编码方式采用的是libmp3lame//释放进程
p.getOutputStream().close();
p.getInputStream().close();
p.getErrorStream().close();
p.waitFor();
File file= newFile(mp3FilePath);
InputStream fileInputStream= newFileInputStream(file);//应该在调用该方法的地方关闭该input流(使用完后),并且要删除掉临时文件夹下的相应文件
/*File amrFile = new File(filePath);
File mp3File = new File(mp3FilePath);
if (amrFile.exists()) {
boolean delete = amrFile.delete();
System.out.println("删除源文件:"+delete);
}
if (mp3File.exists()) {
boolean delete = mp3File.delete();
System.out.println("删除mp3文件:"+delete);
}*/
returnfileInputStream;
}catch(Exception e) {
e.printStackTrace();
}finally{
runtime.freeMemory();
}return null;
}/*** 将用户输入的amr音频文件流转为音频文件并存入临时文件夹中
*@paraminputStream 输入流
*@paramfileName 文件姓名
*@returnamr临时文件存放地址
*@throwsIOException*/
private static String copyFile(InputStream inputStream, String fileName) throwsIOException {
Properties props=System.getProperties();
String filePath= props.getProperty("user.home") + File.separator + "MP3TempFile"; //创建临时目录
File dir = newFile(filePath);if (!dir.exists()) {
dir.mkdir();
}
String outPutFile= dir + File.separator +fileName;
OutputStream outputStream= newFileOutputStream(outPutFile);intbytesRead;byte[] buffer = new byte[8192];while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
outputStream.write(buffer,0, bytesRead);
}
outputStream.flush();
outputStream.close();
inputStream.close();returnoutPutFile;
}/*** 判断系统是Windows还是linux并且拼接ffmpegPath
*@return
*/
private staticString getLinuxOrWindowsFfmpegPath() {
String ffmpegPath= "";
String osName= System.getProperties().getProperty("os.name");if (osName.toLowerCase().indexOf("linux") >= 0) {
ffmpegPath= "";
}else{
URL url= Thread.currentThread().getContextClassLoader().getResource("ffmpeg/windows/");if (url != null) {
ffmpegPath=url.getFile();
}
}returnffmpegPath;
}
}