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格式的更多相关文章

  1. 在java中使用FFmpeg处理视频与音频

    FFmpeg是一个非常好用的视频处理工具,下面讲讲如何在java中使用该工具类. 一.首先,让我们来认识一下FFmpeg在Dos界面的常见操作 1.拷贝视频,并指定新的视频的名字以及格式 ffmpeg ...

  2. Java中如何判断一个日期字符串是否是指定的格式

    判断日期格式是否满足要求 import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date ...

  3. .net amr格式文件转换成mp3格式文件的方法

    前言:winform端对于音频文件的格式多有限制,大多数不支持amr格式的文件的播放.但是,手机端传过来的音频文件大多数是amr格式的文件,所以,要想在winform客户端支持音频文件的播放,可以通过 ...

  4. jquery中定义数组并给数组赋值后转为json格式为[]问题的解决

    一.问题描述:jquery定义一个空数组,并赋值,结果转为json格式后打印值为空 我原本是这样写的,但是show_data值一直为[] var export_data = [];export_dat ...

  5. 【转】如何将FLAC格式转为MP3格式

    原文网址:http://jingyan.baidu.com/ae/3aed632e65708470108091ca.html FLAC全称为无损音频压缩编码,FLAC格式又称无损格式 不会破坏原有的音 ...

  6. Java 中的 SimpleDateFormat 【 parse 和 format 】【转换时间格式】

    在 Java 里面有很多特别方便的函数(尽管术语可能不这么说)可以供我们使用,让一些本来要写好长好多的代码的事情变得仅仅几行就解决了. 在 SimpleDateFormat 中,有以下特定的规则: G ...

  7. java中字符串"1999-10-01T00:00:00+08: 00" 转化为Date格式

    String oldStr = "1999-10-01T00:00:00+08: 00": SimpleDateFORMAT sdf = new SimpleDateFORMAT ...

  8. C#中怎样将List<自己定义>转为Json格式 及相关函数-DataContractJsonSerializer

    对C#和.net使用List<自己定义>和Json格式相互转化的方法进行总结 关于JSON的入门介绍见http://www.json.org/ ,或者百度,这里不赘述,只是通过以下的样例会 ...

  9. openssl生成RSA格式,并转为pkcs8格式

    原文地址:http://www.thinkingquest.net/articles/391.html?utm_source=tuicool 支付宝接口开发相关:openssl 加密工具 支付宝“手机 ...

随机推荐

  1. 使用postman模拟appium的http请求

    Appium是Server,接收http请求,使用Postman模拟请求 1.anyproxy 1.1.安装和运行 #安装 npm i -g anyproxy # 运行anyproxy,端口默认800 ...

  2. 解决IE下select标签innerHTML插入option的BUG(兼容

    在ie下面使用innerHTML来插入option选项的话,ie会去掉前面的<option>,并拆分成多个节点,这样会造成select的出错   前言: 这是一个老bug了,现在提供一个完 ...

  3. TSubobjectPtr和C++传统指针的区别

    转自:http://aigo.iteye.com/blog/2282142 主要有以下区别(1和2的前提条件要满足:指针所在的class必须是UObjcct的子类): 1,TSubobjectPtr指 ...

  4. 显示器如何显示一个YUV422格式的图形

    记录在开发过程中对知识点的一些理解: 在开发渲染程序的过程中,需要对视屏文件进行解码解码后特效文件的叠加,使用的技术是(FFmpeg+DirectX) 解码出来的视屏数据格式是YUYV,使用Direc ...

  5. php 数组随机取值

    array_rand()在你想从数组中取出一个或多个随机的单元时相当有用.它接受 input 作为输入数组和一个可选的参数 num_req,指明了你想取出多少个单元 - 如果没有指定,默认为 1. a ...

  6. python 9*9示例

    # 9*9 乘法表# def nine_nine():#     for i in range(1, 10):#         for j in range(1, i+1):#            ...

  7. 我对if(!this.IsPostBack)的理解

    if(!this.IsPostBack) { } 通常用在page_load中,获取一个值,该值指示该页是否正为响应客户端回发而加载,或者它是否正被首次加载和访问,如果是为响应客户端回发而加载该页,则 ...

  8. leetcode235

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...

  9. Spring MVC 确定目标方法POJO 类型参数

    1:确定一个Key 2. 在implicitMode 中存在Key 对应的对象, 若存在则作为参数传入 3. 在implicitMode 中不存在Key 对应的对象, 则检查当前@SessionAtr ...

  10. Nginx Windows 安装启动

    原文连接:http://tengine.taobao.org/book/appendix_c.html#nginxwindows 下载 Nginx是开源软件,用户可以访问 http://nginx.o ...