参考文章:
备注:使用博主提供的jar包,在linux下试验不成功。最后使用shell命令调用ffmpeg实现。
使用ffmpeg:
1. 从这里下载 https://ffmpeg.org/download.html
2. 下载linux平台的Linux Static Builds
3. 将ffmpeg拷贝到Linux环境下
4. 准备一个amr文件,将两个文件都拷贝到同一个目录下,进行测试。如果准备好了,如图:
5. 接着先给ffmpeg加个执行权限
chmod +x ffmpeg
6. 接着调用 ffmpeg 执行转换下
./ffmpeg -i test.amr test.mp3
然后你会看到一堆的日志,并且可能还有提示,“test.amr: Input/output error”,但不要担心,先不管,你查看下这个目录下是否多了一个test.mp3 文件。如果有了,恭喜你,转换成功了,尝试播放下MP3
你可能得到的日志如下:
----------------------------------
shell调用
public class ShellUtil {
/**
* 运行shell脚本
*
* @param shell
* 需要运行的shell脚本
*/
public static void execShell(String shell) {
try {
Runtime rt = Runtime.getRuntime();
rt.exec(shell);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 运行shell
*
* @param shStr
* 需要执行的shell
* @return
* @throws IOException
* @throws InterruptedException
*/
public static List runShell(String shStr) throws IOException, InterruptedException {
List strList = new ArrayList();
Process process;
process = Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", shStr }, null, null);
InputStreamReader ir = new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
process.waitFor();
while ((line = input.readLine()) != null) {
strList.add(line);
}
return strList;
}
}