linux下ffmpeg轉換amr為mp3大小為0的解決方案

linux下ffmpeg轉換amr為mp3大小為0的解決方案

直接上程式碼:

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