您的位置:首页 > 运维架构 > Linux

linux下ffmpeg转换amr为mp3大小为0的解决方案

2016-09-23 17:08 453 查看
直接上代码:

public class AudioUtils {
/**
* 将一个amr文件转换成mp3文件
*
* @param amrFile
* @param mp3File
* @throws IOException
*/
public static void amr2mp3(String amrFile, String mp3File) throws IOException {
File source = new File(amrFile);
File target = new File(mp3File);
AudioAttributes audio = new AudioAttributes();
Encoder encoder = new Encoder();

audio.setCodec("libmp3lame");
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mp3");
attrs.setAudioAttributes(audio);
try {
encoder.encode(source, target, attrs);
} catch (Exception e) {
}
}
}

以上代码是利用jave.jar实现的

结果:windows下可以正常转MP3,可是在linux下转的MP3都是0字节!

其实jave只是封装了ffmpeg(强大的音频编码工具))工具,用解压工具打开可以看到里面是包含window下可以执行的ffmpeg.exe 和相应的dll,

所以如果需要在linux上面也能够顺利解码,那么至少需要在linux服务器上安装ffmpeg,至于怎么安装可以去ffmpeg官网。



现在,我们不用再linux上安装ffmpeg,而是利用绿色版的ffmpeg进行解决!



其中:Amr2Mp3Filter.java的代码为

package com.bill99.amr.filter;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bill99.amr.util.AudioUtils;

/**
* 此过滤器用来拦截所有以amr后缀结尾的请求,并转换成mp3流输出,输出MIME类型为audio/mpeg.
* @author zhangkenan
*
*/
public class Amr2Mp3Filter implements Filter{

/**
* mp3扩展名对应的MIME类型,值为"audio/mpeg"
*/
public final static String MP3_MIME_TYPE = "audio/mpeg";

public void init(FilterConfig filterConfig) throws ServletException {}
public void destroy() {}

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest request;
HttpServletResponse response;
try {
request = (HttpServletRequest) req;
response = (HttpServletResponse) resp;
} catch (ClassCastException e) {
throw new ServletException("non-HTTP request or response");
}

String requstURI = request.getRequestURI();
String contextPath = request.getContextPath();
String resPath = requstURI;
//去掉requstURI中contextPath部分和参数部分
if(contextPath.length() > 0) {
resPath = resPath.substring(contextPath.length());
}
int index = 0;
if((index = resPath.lastIndexOf("?")) != -1) {
resPath = resPath.substring(0, index);
}

String resRealPath = req.getServletContext().getRealPath(resPath);
String mp3ResRealPath = resRealPath.replaceFirst(".amr$", ".mp3");
File mp3File = new File(mp3ResRealPath);
if(!mp3File.exists()) {
File amrFile = new File(resRealPath);
if(!amrFile.exists()) {
filterChain.doFilter(request, response);
return;
}
AudioUtils.amr2mp3(amrFile.getAbsolutePath(), mp3File.getAbsolutePath());
}
response.setContentLength((int)mp3File.length());
response.setContentType(MP3_MIME_TYPE);
InputStream in = new FileInputStream(mp3File);
OutputStream out = response.getOutputStream();
try {
byte[] buf = new byte[1024];
int len = -1;
while((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
} finally {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
out.flush();
}
}

}



AudioUtils.java的内容为:

package com.bill99.amr.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class AudioUtils {
/**
* ffmpeg.exe文件所在的路径
*/
private final static String FFMPEG_PATH;
static {
FFMPEG_PATH = AudioUtils.class.getResource("ffmpeg").getFile();
}
/**
* 将一个amr文件转换成mp3文件
* @param amrFile
* @param mp3File
* @throws IOException
*/
public static void amr2mp3(String amrFileName, String mp3FileName) throws IOException {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(FFMPEG_PATH + " -i "+amrFileName+" -ar 8000 -ac 1 -y -ab 12.4k " + mp3FileName);
InputStream in = process.getErrorStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = br.readLine())!=null) {
System.out.println(line);
}
if(process.exitValue() != 0 ) {
throw new RuntimeException("转换失败!");
}
}
}



完整版本可到我的gihub进行下载:

https://github.com/iamzken/amr-to-mp3/tree/master/ffmpeg-linux
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: