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. [转]html 下拉框级联

    <html> <head> <title>html 下拉框级联</title> <meta charset="UTF-8"/& ...

  2. Ubuntu16.04或18.04上安装QQ微信迅雷

    0. 写在前面 没办法,公司的电脑是Windows的,windows下面开发实在太恶心人,并且开发中需要编译golang和C++的程序,于是开始了Linux的折腾之路. 如果你只是想用Linux环境开 ...

  3. C++ 无锁队列实现

    上源码 #ifndef __GLOBAL_LOCK_FREE_QUEUE_H__ #define __GLOBAL_LOCK_FREE_QUEUE_H__ #include <atomic> ...

  4. guess_age

    age_shanshan = 18count = 3num = 0while num < count: age = int(input("age:")) if age == ...

  5. HTML5 新特性(一)

    1. 新的Doctype 尽管使用<!DOCTYPE html>,即使浏览器不懂这句话也会按照标准模式去渲染 2. Figure元素 用<figure>和<figcapt ...

  6. concurrent.futures模块

    1.concurrent.futures模块介绍 2.ThreadPoolExecutor线程池使用 3.ProcessPoolExecutor进程池使用 4.其他方法使用 1.concurrent. ...

  7. PyQt5系列教程(七)控件

    软硬件环境 Windows 10 Python 3.4.2 PyQt 5.5.1 PyCharm 5.0.4 前言 控件是PyQt应用程序的基石.PyQt5自带很多不同的控件,包括像button.ch ...

  8. Ingress.yaml

    apiVersion: extensions/v1beta1 kind: Ingress metadata: name: test-ingress namespace: default annotat ...

  9. 0_Simple__simpleTexture + 0_Simple__simpleTextureDrv

    使用纹理引用来旋转图片,并在使用了静态编译和运行时编译两种环境. ▶ 源代码:静态编译 #include <stdio.h> #include <windows.h> #inc ...

  10. PHP把excel导入mysql数据库最常用的方法

    Posted on 2011-03-25 09:16 PHP博客 阅读(1316) 评论(0)  编辑 收藏 引用 网摘 PHP把excel(xls)文件导入mysql数据库最常用的方法就是先把xls ...