Linux under FFmpeg convert AMR to MP3 size 0 solution __linux

Source: Internet
Author: User

directly on the code:

public class Audioutils {
/**
* Converts an AMR file into a mp3 file
* 
* @param amrfile
* @param mp3file
* @throws IOException
*
/public static void Amr2mp3 (String amrfile, String mp3file) throws IOException {
File Source = n EW 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) {
}
}
}


The above code is implemented using Jave.jar

results: Windows can be normal to MP3, but in the Linux turn MP3 are 0 bytes.

in fact, Jave only encapsulates the FFmpeg (powerful Audio Coding tool)) tool, which is opened with the decompression tool to see that the inside is a ffmpeg.exe and corresponding DLL that contains windows that can be executed,
So if you need to be able to decode on Linux, then at least you need to install FFmpeg on the Linux server, as for how to install the FFmpeg official website.

now, instead of installing FFmpeg on Linux, we use the green version of FFmpeg to solve it.



Where: Amr2mp3filter.java's code is

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;
 /** * This filter is used to intercept all requests ending with the AMR suffix and to convert to MP3 stream output, and the output MIME type is audio/mpeg. * @author Zhangkenan * * */public class Amr2mp3filter implements filter{/** * MP3 extension corresponding to the MIME type, the value is "audio/mpeg" */Public

Final static String Mp3_mime_type = "Audio/mpeg"; public void init (Filterconfig filterconfig) throws servletexception {} public void Destroy () {} public void Dofilter (Ser
Vletrequest 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;
Remove the ContextPath part and parameter portion of Requsturi (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 (); }
}


}



The contents of the Audioutils.java are:

Package com.bill99.amr.util;


Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
the path to the public class Audioutils {/**
* ffmpeg.exe file is * *
private final static String Ffmpeg_path;
static {
Ffmpeg_path = AudioUtils.class.getResource ("FFMPEG"). GetFile ();
}
/**
* Convert an AMR file to mp3 file
* @param amrfile 
* @param mp3file * 
@throws ioexception
* * public static void Amr2mp3 (String amrfilename, String mp3filename) throws IOException {Runtime
Runtime = Runtime.get Runtime ();
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 ("conversion failed.") ");
}
}
}



The full version can be downloaded to my gihub:

Https://github.com/iamzken/amr-to-mp3/tree/master/ffmpeg-linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.