如何在Node.js项目中调用GitHub上的Python项目whisperX?
在Node.js项目中调用whisperX的两种可行方案
不需要将whisperX打包成单个.py文件,以下是两种直接可用的实现方式:
方案1:通过child_process调用whisperX命令行工具(推荐)
whisperX本身支持命令行调用,只需先将其安装为系统/虚拟环境内的命令工具:
- 克隆whisperX仓库后,进入仓库根目录,执行:
这会将pip install -e .whisperx命令注册到当前环境,后续直接通过命令调用即可。 - 在Node.js中使用
child_process模块执行命令:
命令参数可根据需求调整,参考whisperX的命令行参数说明即可。const { execSync } = require('child_process'); try { // 示例:处理音频文件并生成srt字幕 const result = execSync('whisperx /path/to/target/audio.mp3 --output_format srt', { encoding: 'utf8' }); console.log('处理结果:', result); } catch (err) { console.error('执行失败:', err.stderr); }
方案2:通过python-shell直接调用项目脚本
若不想安装为命令行工具,可直接调用仓库内的核心脚本:
whisperX的入口逻辑在whisperx/cli.py文件中,直接调用该脚本即可:
const { PythonShell } = require('python-shell'); const options = { scriptPath: '/path/to/whisperX/repo/whisperx', // 指向仓库内的whisperx子目录 args: ['/path/to/target/audio.mp3', '--output_format', 'vtt'] // 传入处理参数 }; PythonShell.run('cli.py', options, (err, results) => { if (err) throw err; console.log('脚本输出:', results); });
也可通过python -m whisperx的方式调用,本质是触发whisperx/__main__.py的执行,效果与命令行一致。
内容的提问来源于stack exchange,提问作者I am L
相关产品推荐
相关产品推荐

