Node.js中gTTS音频语速提至1.25倍求助及say包女声切换问题
解决gTTS语速调整与say包女声切换问题
一、gTTS实现1.25倍语速
gTTS本身没有内置语速调整参数,需先生成原始音频,再通过音频处理工具调整速度。推荐使用跨平台工具ffmpeg,步骤如下:
确保系统已安装ffmpeg:
- macOS:终端执行
brew install ffmpeg - Windows:下载官方安装包完成配置
- Linux:通过对应包管理器(如
apt install ffmpeg)安装
- macOS:终端执行
修改代码实现生成+调速流程:
const gTTS = require('gtts'); const { exec } = require('child_process'); let speech = 'Welcome to GeeksforGeeks'; const gtts = new gTTS(speech, 'en'); gtts.save('Voice.mp3', function (err, result) { if (err) { throw new Error(err); } console.log("原始音频生成完成!"); // 用ffmpeg将语速调整至1.25倍(不改变音调) const targetSpeed = 1.25; const ffmpegCmd = `ffmpeg -i Voice.mp3 -filter:a "atempo=${targetSpeed}" Voice_1.25x.mp3`; exec(ffmpegCmd, (error) => { if (error) { console.error('调速失败:', error); return; } console.log("语速已调整至1.25倍!"); }); });
说明:
atempo滤镜支持0.5-2.0的速度范围,若需超过2倍,可叠加多个atempo滤镜(如atempo=2.0,atempo=1.25实现2.5倍速度)。
二、say包切换女声并设置语速
say包依赖系统自带TTS引擎,不同平台的女声语音名称不同,需先查询可用语音,再指定对应名称:
1. 查询系统可用语音
- macOS/Linux:终端执行
say -v ?,可查看所有语音(如美式女声Samantha) - Windows:终端执行
say --voices,可查看所有语音(如英文女声Microsoft Zira Desktop)
2. 修改代码实现女声+语速设置
const say = require('say'); const text = 'Welcome to GeeksforGeeks'; const targetSpeed = 1.25; // 根据平台选择对应女声名称,示例为macOS的Samantha,Windows替换为Microsoft Zira Desktop const femaleVoice = 'Samantha'; // 直接指定女声和语速,无需额外工具二次处理 say.export(text, femaleVoice, targetSpeed, 'output_female_1.25x.wav', (error) => { if (error) { console.error('生成失败:', error); return; } console.log('女声语音生成完成,语速1.25倍!'); });
说明:say的语速参数范围为0.1-10.0,1.0为正常语速,直接设置1.25即可达到目标,无需额外调用soundstretch。
内容的提问来源于stack exchange,提问作者Kunal Vijan
相关产品推荐
相关产品推荐

