安装ffmpeg
ffmpeg -codecs
—可以看出ffmpeg默认的MP3格式无法编码为其它格式【D.A.L mp3】D=解码 E=编码 A=AudioCodec L=有损压缩
需要安装lame服务使得MP3具有编码为其他格式的功能
参考:/zhaofengdeng/article/details/79413330
微信/qq所需要的音频文件为Silk v3格式,Silk v3格式有占用空间小的特点,如amr、aud、slk、silk都是Silk v3的格式
MP3->PCM->SILK,这个过程叫做silk v3的解码(也是mp3的编码)
SILK->PCM->MP3,这个过程叫做Silk v3的编码
中间格式PCM是一个类似WAV的文件格式。
详解:MP3->PCM->SILK
1、 MP3->PCM 使用ffmpeg转换
ffmpeg -y -i {源文件.mp3} -f s16le -ar 24000 -ac 1 {目标文件.pcm}
参数:-y 强制覆盖
-i 输入文件
-f s16le 转换格式 s16le=(PCM signed 16-bit little-endian), s16be=(PCM signed 16-bit big-endian)
-ar 24000 频率Hz,默认的微信音频频率是24000(set audio sampling rate (in Hz))
-ac 1 频道数,(set number of audio channels)
2、 PCM->SILK 使用kn007的开源项目Silk2MP3转换(需要编译encoder)
/usr/webser/silk-v3-decoder/silk/encoder {源文件.pcm} {目标文件.silk} -tencent
参考:/kn007/silk-v3-decoder
同理 SILK->PCM->MP3
1、 SILK->PCM 使用Silk2MP3转换
/usr/webser/silk-v3-decoder/silk/decoder {源文件.silk} {目标文件.pcm} -tencent
2、 PCM->MP3 使用ffmpeg转换
ffmpeg -y -ac 1 -ar 16000 -f s16le -i {源文件.pcm} -c:a libmp3lame -q:a 2 {目标文件.mp3}
获取音频时长
使用jave,不赘诉
参考:/xxbai1123/p/9718528.html
附:
Silk2MP3编译encoder,(由于该开源项目主要是用于silkv3_decoder,但是encoder有源码,只是需要自己手动编译)
cd /usr/webser/silk-v3/decoder/silk
make encoder
编译完成后encoder即可使用
代码
public static String mp3ToSilk(String source, String path, String audioType) {File sourceFile = new File(source);if (sourceFile.exists()) {String name = sourceFile.getName();logger.debug("filename={}", name);String pcmPath = path + name + ".pcm";logger.debug("pcmPath={}", pcmPath);processPcm(path, source, pcmPath);// 先转成pcm格式String target = path + name + "." + audioType;logger.debug("target={}", target);File pcmFile = new File(pcmPath);if (pcmFile.exists()) {processMp3(path, pcmPath, target);// 再由pcm转成mp3} else {processFfmpegMp3(path, source, target);// 直接转成mp3}return target;} else {logger.error("文件不存在 {}", source);}return null;}// 调用sile_v3_encoder,pcm->silkprivate static void processMp3(String path, String source, String target) {String command = StringUtils.replaceEach("/usr/webser/silk-v3-decoder/silk/encoder {源文件} {目标文件} -tencent",new String[] {"{源文件}", "{目标文件}" }, new String[] {source, target });try {p = Runtime.getRuntime().exec(command);p.waitFor();} catch (Exception e) {logger.error("执行失败{}", command, e);}}// 调用ffmpeg,mp3->pcmprivate static void processPcm(String path, String source, String target) {// ffmpeg -y -i 源文件 -f s16le -ar 24000 -ac 1 目标文件String command = StringUtils.replaceEach("ffmpeg -y -i {源文件} -f s16le -ar 24000 -ac 1 {目标文件}",new String[] {"{源文件}", "{目标文件}" }, new String[] {source, target });try {p = Runtime.getRuntime().exec(command);p.waitFor();} catch (Exception e) {logger.error("执行失败{}", command, e);}}// 调用ffmpeg,mp3->silkprivate static void processFfmpegMp3(String path, String source, String target) {String command = StringUtils.replaceEach("ffmpeg -i {源文件} {目标文件}", new String[] {"{源文件}", "{目标文件}" },new String[] {source, target });try {p = Runtime.getRuntime().exec(command);p.waitFor();} catch (Exception e) {logger.error("执行失败{}", command, e);}}public static Long getMp3VoiceLen(String source) {File sourceFile = new File(source);if (sourceFile.exists()) {Encoder encoder = new Encoder();long ls = 0;MultimediaInfo m;try {m = encoder.getInfo(sourceFile);ls = m.getDuration();} catch (Exception e) {logger.error("获取音频时长有误:{}", source, e);}return ls;} else {logger.error("文件不存在 {}", source);}return 0L;}
在此再次感谢kn007提供的转换包:/kn007/silk-v3-decoder
linux下微信/qq的aud silk音频文件格式与mp3格式互转 获取音频时长的方法 附问题分析和java代码。