700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java amr转mp3_在java中使用ffmpeg将amr格式的语音转为mp3格式

java amr转mp3_在java中使用ffmpeg将amr格式的语音转为mp3格式

时间:2021-04-05 16:40:33

相关推荐

java amr转mp3_在java中使用ffmpeg将amr格式的语音转为mp3格式

/*** 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;

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。