Windows环境下如何使用google/zx执行ffmpeg shell命令?
问题根因
scoop安装的软件默认会将对应的可执行文件shim放到用户目录下的scoop\shims文件夹,并将该路径添加到当前用户的PowerShell PATH环境变量中。zx默认运行时加载的是系统级PATH,或者默认使用cmd作为子shell,没有加载到用户级的PATH配置,因此找不到ffmpeg命令。
解决方法
你可以任选以下一种方案修复:
方案1:使用ffmpeg绝对路径调用
- 先在PowerShell中执行
Get-Command ffmpeg,获取ffmpeg的完整安装路径,示例输出为C:\Users\你的用户名\scoop\shims\ffmpeg.exe - 将脚本中的ffmpeg替换为获取到的绝对路径即可
代码示例:
#!/usr/bin/env zx const filePath = `./abc.mp4`; const srsServerURL = `192.168.30.100`; // 替换为你实际查询到的ffmpeg绝对路径 const ffmpegPath = `C:/Users/xxx/scoop/shims/ffmpeg.exe`; await $`${ffmpegPath} -re -i ${filePath} -c copy -f mp4 -y rtmp://${srsServerURL}/live/livestream`;- 先在PowerShell中执行
方案2:手动将scoop shims目录加入zx运行时环境变量
直接在脚本开头将scoop的shims路径追加到PATH变量中,后续可直接调用ffmpeg
代码示例:#!/usr/bin/env zx // 替换为你实际的scoop shims目录路径 process.env.PATH = `${process.env.PATH};C:/Users/xxx/scoop/shims`; const filePath = `./abc.mp4`; const srsServerURL = `192.168.30.100`; await $`ffmpeg -re -i ${filePath} -c copy -f mp4 -y rtmp://${srsServerURL}/live/livestream`;方案3:指定zx使用PowerShell作为子shell
zx在Windows上默认使用cmd作为子shell,不会加载PowerShell的用户级PATH配置,指定shell为PowerShell即可正常读取你当前的PATH配置
代码示例:#!/usr/bin/env zx $.shell = 'powershell.exe'; const filePath = `./abc.mp4`; const srsServerURL = `192.168.30.100`; await $`ffmpeg -re -i ${filePath} -c copy -f mp4 -y rtmp://${srsServerURL}/live/livestream`;
内容的提问来源于stack exchange,提问作者radiorz
相关产品推荐
相关产品推荐

