如何使用FFmpeg修改MP3等音频文件的音调?
如何用FFmpeg修改MP3音频的音调
我尝试用FFmpeg修改MP3音频文件的音调,但一直无法成功。请问正确的FFmpeg命令是什么?
我的Java代码如下:
String outPutPath = new File("/storage/emulated/0/Share it Application/Over_the_HorizonTemp.wav").getPath(); String[] strFfmpeg = {"ffmpeg","-i" ,strInputPath,"-af", "rubberband=tempo=1.0:pitch=1.5:pitchq=quality" ,outPutPath}; execffmpegBinary(strFfmpeg);
execffmpegBinary函数实现:
public void execffmpegBinary(String[] command) { Config.enableLogCallback(new LogCallback() { @Override public void apply(LogMessage message) { Log.e(Config.TAG, message.getText()); Log.e("TAG", "apply: " +message.getText()); } }); Config.enableStatisticsCallback(new StatisticsCallback() { @Override public void apply(Statistics statistics) { } }); long executionId = FFmpeg.executeAsync(command, new ExecuteCallback() { @Override public void apply(long executionId, int returnCode) { if (returnCode == RETURN_CODE_SUCCESS) { Log.e("1TAG", "apply:return code "+returnCode ); Log.e("1TAG", "apply:execution Id "+executionId ); Log.e("1TAG", "apply:execution Id "+ new FFmpegExecution(executionId,command)); } else if (returnCode == RETURN_CODE_CANCEL) { Log.e("2TAG", "apply:return code "+returnCode ); Log.e("2TAG", "apply:execution Id "+executionId ); Log.e("2TAG", "apply:execution Id "+ new FFmpegExecution(executionId,command)); } else { Log.e("3TAG", "apply: returnCode"+ returnCode); Log.e("3TAG", "apply:return code "+returnCode ); Log.e("3TAG", "apply:execution Id "+executionId ); Log.e("3TAG", "apply:execution Id "+ new FFmpegExecution(executionId,command)); } } }); }
解决方案
1. 修正rubberband滤镜参数
你当前的rubberband参数存在错误:pitchq=quality是无效写法,pitchq需要指定具体质量等级,比如high/medium/low。同时要确保你的FFmpeg版本编译时包含了rubberband滤镜支持。
修正后的Java命令数组:
String[] strFfmpeg = {"ffmpeg", "-i", strInputPath, "-af", "rubberband=tempo=1.0:pitch=1.5:pitchq=high", outPutPath};
2. 内置滤镜替代方案:asetrate+atempo组合
如果rubberband滤镜不可用,可使用FFmpeg原生滤镜组合实现音调调整,无需额外依赖:
// 升调1.5倍:通过修改采样率+调整速度还原时长 // 通用写法(自动适配原采样率) String[] strFfmpeg = {"ffmpeg", "-i", strInputPath, "-af", "asetrate=sr*1.5,atempo=1/1.5", outPutPath};
3. 直观替代方案:pitchshift滤镜
部分FFmpeg版本支持pitchshift滤镜,直接用半音数调整音调更直观:
// pitch=6代表升6个半音,约等于音调提升1.5倍 String[] strFfmpeg = {"ffmpeg", "-i", strInputPath, "-af", "pitchshift=pitch=6", outPutPath};
排查要点
- 查看
LogCallback打印的FFmpeg日志,确认是否有滤镜不支持、路径错误、权限不足等提示。 - 确保
strInputPath正确指向目标MP3文件,输出路径有写入权限。
内容的提问来源于stack exchange,提问作者syed kashifullah
相关产品推荐
相关产品推荐

