在java中使用ffmpeg将amr格式的语音转为mp3格式
ffmpeg是一个非常强大的音视频处理工具,官网是:http://ffmpeg.org/。
由于ffmpeg在windows上和linux系统上的执行文件不一样(Windows上不需要安装ffmpeg,只需要下载Windows版本的ffmpeg就行。linux上需要用户自己安装ffmpeg---> 参考链接:http://linux.it.net.cn/e/Linuxit/2014/0828/3980.html)
最近最项目是遇到一个需求,就是将安卓端amr格式的录音文件转为mp3格式,然后在网页上播放。
一、 Windows系统和linux系统的处理方式
1、首先在Windows系统上倒好解决。方案有2个,一个是使用jave.jar工具包,另一种是直接将下载好的Windows版本的ffmpeg解压,然后将其中bin目录下ffmpeg.exe文件导入到项目中(或者直接使用代码读取本地的ffmpeg.exe执行文件)。
1.1、 使用jave.jar工具包
http://mfan.iteye.com/blog/2032454
1.2、使用ffmpeg.exe执行文件
1.2.1、使用本地的ffmpeg.exe执行文件,直接通过File获取

1.2.2、将ffmpeg.exe执行文件导入到项目中,通过 URL url = Thread.currentThread().getContextClassLoader().getResource("ffmpeg/windows/"); 来获取

1.3、linux服务器上使用ffmpeg将amr转为mp3
1.3.1、首先要在linux服务器上安装ffmpeg工具,安装方式见上方
二、utils工具类(代码具体实现)
/**
* Create By yxl on 2018/6/5
*/
public class AmrToMP3Utils { private static Logger logger =Logger.getLogger(AmrToMP3Utils.class); /**
* 将amr文件输入转为mp3格式
* @param file
* @return
*/
public static InputStream 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 = new File(mp3FilePath);
InputStream fileInputStream = new FileInputStream(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);
}*/ return fileInputStream;
} catch (Exception e) {
e.printStackTrace();
} finally {
runtime.freeMemory();
}
return null;
} /**
* 将amr文件输入流转为mp3格式
* @param inputStream amr文件的输入流(也可以是其它的文件流)
* @param fileName 文件名(包含后缀)
* @return
*/
public static InputStream 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 = new File(mp3FilePath);
InputStream fileInputStream = new FileInputStream(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);
}*/
return fileInputStream; } catch (Exception e) {
e.printStackTrace();
} finally {
runtime.freeMemory();
}
return null;
} /**
* 将用户输入的amr音频文件流转为音频文件并存入临时文件夹中
* @param inputStream 输入流
* @param fileName 文件姓名
* @return amr临时文件存放地址
* @throws IOException
*/
private static String copyFile(InputStream inputStream, String fileName) throws IOException {
Properties props = System.getProperties();
String filePath = props.getProperty("user.home") + File.separator + "MP3TempFile"; //创建临时目录
File dir = new File(filePath);
if (!dir.exists()) {
dir.mkdir();
} String outPutFile = dir + File.separator + fileName; OutputStream outputStream = new FileOutputStream(outPutFile);
int bytesRead;
byte[] buffer = new byte[8192];
while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
outputStream.close();
inputStream.close(); return outPutFile;
} /**
* 判断系统是Windows还是linux并且拼接ffmpegPath
* @return
*/
private static String 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();
}
}
return ffmpegPath;
}
}
在java中使用ffmpeg将amr格式的语音转为mp3格式的更多相关文章
- 在java中使用FFmpeg处理视频与音频
FFmpeg是一个非常好用的视频处理工具,下面讲讲如何在java中使用该工具类. 一.首先,让我们来认识一下FFmpeg在Dos界面的常见操作 1.拷贝视频,并指定新的视频的名字以及格式 ffmpeg ...
- Java中如何判断一个日期字符串是否是指定的格式
判断日期格式是否满足要求 import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date ...
- .net amr格式文件转换成mp3格式文件的方法
前言:winform端对于音频文件的格式多有限制,大多数不支持amr格式的文件的播放.但是,手机端传过来的音频文件大多数是amr格式的文件,所以,要想在winform客户端支持音频文件的播放,可以通过 ...
- jquery中定义数组并给数组赋值后转为json格式为[]问题的解决
一.问题描述:jquery定义一个空数组,并赋值,结果转为json格式后打印值为空 我原本是这样写的,但是show_data值一直为[] var export_data = [];export_dat ...
- 【转】如何将FLAC格式转为MP3格式
原文网址:http://jingyan.baidu.com/ae/3aed632e65708470108091ca.html FLAC全称为无损音频压缩编码,FLAC格式又称无损格式 不会破坏原有的音 ...
- Java 中的 SimpleDateFormat 【 parse 和 format 】【转换时间格式】
在 Java 里面有很多特别方便的函数(尽管术语可能不这么说)可以供我们使用,让一些本来要写好长好多的代码的事情变得仅仅几行就解决了. 在 SimpleDateFormat 中,有以下特定的规则: G ...
- java中字符串"1999-10-01T00:00:00+08: 00" 转化为Date格式
String oldStr = "1999-10-01T00:00:00+08: 00": SimpleDateFORMAT sdf = new SimpleDateFORMAT ...
- C#中怎样将List<自己定义>转为Json格式 及相关函数-DataContractJsonSerializer
对C#和.net使用List<自己定义>和Json格式相互转化的方法进行总结 关于JSON的入门介绍见http://www.json.org/ ,或者百度,这里不赘述,只是通过以下的样例会 ...
- openssl生成RSA格式,并转为pkcs8格式
原文地址:http://www.thinkingquest.net/articles/391.html?utm_source=tuicool 支付宝接口开发相关:openssl 加密工具 支付宝“手机 ...
随机推荐
- CA双向认证的时候,如果一开始下载的证书就有问题的,怎么保证以后的交易没有问题?
研究HTTPS协议的时候,发现网站的CA认证,比如建行,比如支付宝,需要首先下载数字证书, 当然有些其他的双向认证,比如之前做过的港航和JP MORGAN进行交互的时候,证书是私下发送的,不需要去公网 ...
- 2018ICPC网络赛(徐州站)A题题解
一.题目链接 https://nanti.jisuanke.com/t/31453 二.题意 给定$N$个位置,$2^k$种颜色,让你去涂色,条件是相邻的两种颜色类型异或值的二进制表示不全为$1$(以 ...
- matlab批量修改图片大小
转自:http://blog.csdn.net/cike0cop/article/details/53087995 %author:coplin %time:2016-10-10 %function: ...
- WCF服务部署
一.将WCF服务部署到IIS上 1.首先检测电脑上是否安装了IIS,一般来说Win7以上系统自带IIS 2.下面进行IIS服务的开启设置 控制面板=>打开或关闭Windos功能 3.勾选该窗口中 ...
- Web API源码剖析之HttpServer
Web API源码剖析之HttpServer 上一节我们讲述全局配置.本节将讲述全局配置的DefaultServer,它是一个HttpServer类型. 主要作用就是接受每一次请求,然后分发给消息处理 ...
- windows hbase installation
In the previous post, I have introduced how to install hadoop on windows based system. Now, I will ...
- express有中间件的增删改查
var express = require('express');引入express框架 var router = express.Router();引入router路由级中间件 var data = ...
- MYSQL体系结构-来自期刊
MySQL三层体系结构 |-----------------------------------------------------------------------------------| | ...
- Gson进行json字符串和对象之间的转化
Gson可以实现对象与json字符串之间的转化,以下是在Android中的示例代码. Gson主页:https://code.google.com/p/google-gson/ public clas ...
- sqlserver主从复制
参考网站: http://www.178linux.com/9079 https://www.cnblogs.com/tatsuya/p/5025583.html windows系统环境进行主从复制操 ...