在NestJS项目中使用whisper-node库转写音频时遇TypeError问题
TypeError: Cannot read properties of null (reading 'shift') in whisper-node (NestJS)
我在NestJS项目中使用whisper-node库转写WAV音频文件时遇到了这个错误,代码片段如下:
import whisper from 'whisper-node'; import path from 'path'; const filePath = path.join(__dirname, '..', 'uploads', 'output.wav'); const options = { modelName: 'base.en', whisperOptions: { language: 'auto', gen_file_txt: false, gen_file_subtitle: false, gen_file_vtt: false, word_timestamps: true, }, }; const transcript = await whisper(filePath, options);
已确认文件路径正确且WAV文件有效,但调用whisper函数时触发错误:
TypeError: Cannot read properties of null (reading 'shift') at parseTranscript
解决建议
升级whisper-node版本
这个错误大概率是库内部parseTranscript函数的解析bug导致的——当开启word_timestamps: true时,预期的解析数组为空。直接升级到最新稳定版:npm update whisper-node # 或使用yarn yarn upgrade whisper-node临时关闭word_timestamps选项
如果升级后问题仍存在,先关闭该选项验证核心转写功能是否正常:const options = { modelName: 'base.en', whisperOptions: { language: 'auto', gen_file_txt: false, gen_file_subtitle: false, gen_file_vtt: false, word_timestamps: false, // 暂时关闭 }, };若关闭后正常,说明是该选项的解析逻辑存在问题,可向库仓库提交issue反馈。
修复音频文件编码
Whisper对音频格式有严格要求(16kHz采样率、单声道、PCM编码),即使是WAV文件也可能不符合标准。用ffmpeg转换后再测试:ffmpeg -i output.wav -ar 16000 -ac 1 -c:a pcm_s16le output_fixed.wav替换原文件后重新运行代码。
手动指定模型路径
若模型下载不完整也会导致转录失败,尝试手动指定本地已下载的模型路径:const options = { modelName: 'base.en', modelPath: '/your/local/model/path/base.en/', // 替换为实际路径 whisperOptions: { // 你的配置项 }, };
内容的提问来源于stack exchange,提问作者Vahe Vardanyan
相关产品推荐
相关产品推荐

