欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

linux java转amr为mp3

程序员文章站 2024-01-05 09:26:04
...
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 将amr文件转为mp3格式文件
 * 【在windows、linux、mac通用可行的解决方案】
 * 在web项目中只需将ffmpeg命令放到和执行命令的程序同一个目录,调用命令:ffmpeg -i src.amr dst.mp3
 * @author bruse
 *
 */
public class FFmpeg {

	public static boolean amrToMp3(String localPath, String targetFilePath) {

		try {

			System.out.println("**************  ffmpeg ****************");

			java.lang.Runtime rt = Runtime.getRuntime();
			String command = "/Users/bruse/Desktop/ffmpeg -i " + localPath + " " + targetFilePath;

			System.out.println("/Users/bruse/Desktop/ffmpeg exec command = " + command);

			Process proc = rt.exec(command);
			InputStream stderr = proc.getErrorStream();
			InputStreamReader isr = new InputStreamReader(stderr);
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			StringBuffer sb = new StringBuffer();
			while ((line = br.readLine()) != null)
				sb.append(line);

			System.out.println("ffmpeg Process errorInfo: " + sb.toString());

			int exitVal = proc.waitFor();
			System.out.println("ffmpeg Process exitValue: " + exitVal);

			return true;
		} catch (Exception e) {
			System.out.println("ffmpeg exec cmd Exception " + e.toString());
		}
		return false;
	}
	
	public static void main(String[] args) {
		String path1 = "/Users/bruse/Desktop/527732_RA8NRoCNXQcrAKOju7zXXTgXHbHJnt4qXaeSSftbzBaXGnsxT49br2e-H4HMlTp4.amr";
		String path2 = "/Users/bruse/Desktop/bb.mp3";
		amrToMp3(path1, path2);
	}

}  

其中ffmpeg命令文件,从官网下载:
http://ffmpeg.org/download.html

上一篇:

下一篇: