Java uses ffmpeg to turn AMR, CAF to MP3 format

Source: Internet
Author: User

Recently to do a project, to the mobile phone app on the file upload to the server, including pictures, sounds, video files. At first only the Android version, as you know, Android's recording format Amr, can not play out on the computer, must be transcoding. Because previously only the Android version, so the use of Jave to facilitate the solution. But then the iphone version was added and the recording format was CAF. Jave supports dozens of audio and video formats, but it does not support the CAF format. Later also tried lame, also not. Most of the information on the Internet is on the iphone, but I'm going to turn it on the server. For one weeks, there was no progress. Later to consult colleagues, they recommend me to use FFmpeg, said this is a very powerful tool, almost any format of audio and video, format factory is used in his core. And I tried the format factory, and I can actually turn to the CAF format. Finally the end of the. This also makes me realize the strength of the team, heads also set a Zhuge Liang, there are problems and others to communicate more.

Anyway In order to let everyone no longer detours, introduce how I do, in fact, very simple. I am using socket upload, when there is a file upload completed, if it is an audio file, I will turn him into a mp3 format. Whether it's AMR format or CAF format, and any audio format, it's OK.

First of all, download Ffmpeg.exe, very easy to search, can be from the official web. Because we are only used for transcoding, we do not introduce his working principles or anything. After downloading, it is possible to execute this ffmpeg.exe directly in the program. The method code for the conversion is as follows:


/** * Convert uploaded recordings to MP3 format * @param webroot The root of the project * @param sourcepath file relative address/public static void ToMp3 (String Webroo
		T, String sourcepath) {//file file = new file (SourcePath);  
		String TargetPath = sourcepath+ ". mp3";//The storage address of the converted file, directly after the original filename plus the mp3 suffix Runtime run = null;  
			try {run = Runtime.getruntime ();  
			Long Start=system.currenttimemillis (); Process p=run.exec (webroot+ "files/ffmpeg-i" +webroot+sourcepath+ "-acodec libmp3lame" +webroot+targetpath); Execution Ffmpeg.exe, preceded by the address of the Ffmpeg.exe, the middle is the file address that needs to be converted, followed by the converted file address.  
			-I is the mode of conversion, which means encoded decoding, and the MP3 encoding uses the Libmp3lame//release process P.getoutputstream (). Close ();  
			P.getinputstream (). Close ();  
			P.geterrorstream (). Close ();  
			P.waitfor ();  
			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 calls the lame decoder to finally free the memory run.fReememory (); }
	}


There are files that need to be converted, call this method directly, and pass in two parameters. To illustrate, the webroot here is the absolute address, that is, the address of the letter, such as D:/tomcat/webroot. Relative address estimation is also possible. The specific encoding for the MP3 when those parameters I have not set, such as the rate of the channel, and so on, because as long as the sound can be heard on the line, and not music, so let him automatically turn it. Of course, if you have a request, you can also set these parameters.

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




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.