ffmpeg是一个对音频、视频文件进行格式转换便捷的软件,使用起来也十分简易。
在使用ffmpeg对微信小程序语音进行格式转换时也遇到了一些小问题,在此作出记录。
如果已经安转了ffmpeg,则可直接忽略安装的步骤,我是参考(https://www.cnblogs.com/cheng5x/p/5646920.html)安装成功的。
以下写了一个将mp3转换成pcm的方法。
/**
* MP3音频格式转换为pcm格式
* @param ffmpegPath ffmpeg文件路径
* @param source 源文件
* @return
*/
public String VoiceFormatMP3ToPCM(String ffmpegPath, String source) {
File file = new File(source);
//转换后目标文件
String target = "F:/" + file.getName().replace("mp3", "pcm");
//String target = source.replace("mp3", "pcm");
if (!file.exists()) {
System.err.println("路径[" + source + "]对应的视频文件不存在!");
return null;
}
List commands = new ArrayList();
commands.add(ffmpegPath);
commands.add("-y");
commands.add("-i");
commands.add(source);
commands.add("-acodec");
commands.add("pcm_s16le");
commands.add("-f");
commands.add("s16le");
commands.add("-ac");
commands.add("1");
commands.add("-ar");
commands.add("16000");
commands.add(target);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commands);
builder.start();
return target;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
需要注意: