Java uses ffmpeg to convert AMR, CAF to MP3 format

Source: Internet
Author: User

Recently, do a project to upload the mobile app files to the server, including pictures, sound, video files. At first only Android version, you know, Android recording format AMR, on the computer does not play out, must transcode. Because the previous only Android version, so with Jave to the convenience of the solution. But later the iphone version was added, and the recording format was the CAF. Although the Jave supports dozens of audio and video formats, it does not support the CAF format. Later also tried the lame, also can not. Most of the information on the Internet is on the iphone, but I'm going to go on the server. For one weeks, no progress was made. Later, I consulted colleagues, they recommended me to use FFmpeg, said it is a very powerful tool, almost can be transferred to any format of audio and video, format factory is the use of his this kernel. And I tried the format factory, and I can actually go to the CAF format. Finally steady. This also made me realize the strength of the team, Three Stooges also set a Zhuge Liang, have problems and have to communicate with others.

Anyway In order to let everyone no longer detours, introduce me how to do, in fact, very simple. I am using the socket upload, when there is a file upload finished, if it is audio files, I will turn him into MP3 format. Both AMR format and CAF format, as well as any audio format, are available.

First of all, download Ffmpeg.exe, it is easy to search, can be from the official website. Because we're just transcoding, we don't introduce him to the principle of work or anything. After downloading, it is possible to execute the Ffmpeg.exe directly inside the program. The method code for the conversion is as follows:

/**      *  convert uploaded recordings to MP3 format       *  @param  webroot  project's root directory       *  @param  sourcePath  relative address of file        */      public static void tomp3 ( String webroot, string sourcepath) {           File file = new file (SourcePath);           string targetpath = sourcepath+ ". mp3";//The storage address of the converted file, directly after the original file name plus MP3 suffix name            Runtime run = null;             try {                 run = runtime.getruntime ();          &nbsP;      long start=system.currenttimemillis ();                 process p=run.exec (webroot+ "files /ffmpeg -i  "+webroot+sourcepath+"  -acodec libmp3lame  "+webroot+targetpath";// Execute Ffmpeg.exe, preceded by the address of Ffmpeg.exe, in the middle is the file address that needs to be converted, followed by the converted file address. -I is the mode of conversion, meaning can encode and decode, MP3 encoding method is used libmp3lame               //Release Process                  p.getoutputstream (). Close ();                 p.getinputstream (). Close ();                 p.geterrorstream (). Close ();                 p.waitfor ();    &nbsP;            long end= System.currenttimemillis ();                 system.out.println (sourcepath+ " convert success, costs:" + (End-start) + "MS");                 //Delete the original file                 //if ( File.exists ()) {                     //file.delete ();                 //}             } catch  (exception e)  {                 e.printstacktrace ();            }finally{                 //run Call lame decoder last release memory                  run.freememory ();             }      }

There is a need to convert the file, call this method directly, pass in two parameters, you can. Note that the webroot here is the absolute address, which is the address with the drive letter, such as D:/tomcat/webroot. Relative address estimation is also possible. The specific code for MP3 when those parameters I did not set, like the rate, channel what, because as long as you can hear the sound on the line, not music, so let him automatically go. Of course, you can set these parameters if you have a request.

public static void Main (string[] args) {ToMp3 ("e:/workspace/reportweb/webroot/", "Audio/rec_20150126_175835.amr")      ; }


This article is from the "through The Jungle" blog, please be sure to keep this source http://luqyu.blog.51cto.com/1663183/1615259

Java uses ffmpeg to convert AMR, CAF to MP3 format

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.