You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ytdl配合fluent-ffmpeg流存视频时报ERR_INVALID_ARG_TYPE错误

错误根因
  • 你使用的@ffmpeg-installer/ffmpeg模块返回值为结构化对象,并非ffmpeg可执行文件的路径字符串,直接将整个对象传入setFfmpegPath方法,触发了类型校验报错。ffmpeg可执行文件的正确路径存放在该对象的path属性中。
  • 额外优化点:ytdl()调用后直接返回可读流,无需使用await包裹;转存为MP3格式时建议明确指定编码参数,避免ffmpeg自动识别流格式出现异常。
修正后可运行代码
const ffmpeg = require('fluent-ffmpeg')
const ffmpegPath = require('@ffmpeg-installer/ffmpeg')
const fs = require('fs')
const ytdl = require('ytdl-core')

// 传入正确的ffmpeg可执行路径
ffmpeg.setFfmpegPath(ffmpegPath.path)

function get() {
    // 直接传入ytdl返回的流,明确指定音频编码和输出格式
    ffmpeg(ytdl('LuchnQtK86U'))
      .audioCodec('libmp3lame')
      .format('mp3')
      .pipe(fs.createWriteStream('track.mp3'))
}

get()
可选:补充异常监听逻辑

如果需要处理网络异常、转码失败等情况,可添加对应事件监听避免未捕获报错:

function get() {
    const videoStream = ytdl('LuchnQtK86U')
    videoStream.on('error', err => console.error('视频流读取失败:', err))
    
    ffmpeg(videoStream)
      .audioCodec('libmp3lame')
      .format('mp3')
      .on('error', err => console.error('音频转码失败:', err))
      .pipe(fs.createWriteStream('track.mp3'))
      .on('finish', () => console.log('MP3文件保存完成'))
}

内容的提问来源于stack exchange,提问作者Shahar Brandman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 05:15:04