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

使用ytdl-core在AWS Lambda中上传YouTube视频至S3时流无数据问题

问题:AWS Lambda中使用ytdl-core无法生成有效流上传至S3

尝试在AWS Lambda函数中使用ytdl-core将YouTube视频以.mp4格式上传至S3存储桶,但ytdl无法生成包含数据的流,导致没有内容写入S3。

代码片段

if (video_url.includes("youtube")){
    const passthrough = new PassThrough();
    const video_file = ytdl(video_url, { quality: '18' });
    const pipe_response = video_file.pipe(passthrough);
    console.log(pipe_response);
    const key_name = id.concat('.', 'mp4');
    const s3Client = new S3Client({ region: REGION });
    const upload = new Upload({
      client: s3Client,
      params: {
        Bucket: BUCKET_NAME,
        Key: key_name,
        Body: passthrough
      },
      partSize: 1024*1024*64,
      leavePartsOnError: false,
    });
    try {
      const response = await upload.done();
      console.log(response);
      console.log('Upload done');
    } catch (err) {
      console.error('Error uploading video:', err);
    }
  }

现象

流始终返回空缓冲区:

PassThrough {
  _readableState: ReadableState {
    state: 6160,
    highWaterMark: 524288,
    buffer: BufferList { head: null, tail: null, length: 0 },
    length: 0,

调用函数时,没有任何内容被推送到缓冲区或S3存储桶。


解答

ytdl-core的现状

ytdl-core已超过一年未更新,YouTube的视频提取机制和API频繁迭代,导致该库目前基本无法正常工作,多数用户反馈无法获取有效流、返回403/404错误,这是你遇到流为空的核心原因。

临时排查方案(若仍想尝试使用ytdl-core)

  • 更新至最新版本:执行npm install ytdl-core@latest,尽管很久没更新,但旧版本问题可能更多
  • 添加错误监听:给ytdl流绑定错误事件,排查隐藏异常:
    video_file.on('error', (err) => {
      console.error('ytdl stream error:', err);
    });
    
  • 调整画质参数:'18'对应360p MP4,但部分视频可能无该画质,尝试自动筛选可用流:
    const video_file = ytdl(video_url, { filter: 'audioandvideo', format: 'mp4' });
    
  • 优化Lambda配置:提升函数内存配额,延长执行超时时间,确保流读取有足够资源。

推荐的Node.js替代库

  • play-dl:专为YouTube打造的现代库,维护活跃,支持流提取、搜索等功能,无需额外二进制文件,适配Node.js环境。
  • youtube-dl-exec:基于youtube-dl/yt-dlp的Node.js封装,更新频繁,支持多平台视频提取,可在Lambda中通过层部署对应二进制文件。
  • yt-dlp:youtube-dl的活跃分支,功能强大、兼容性强,可通过Node.js调用,适合需要稳定提取能力的场景。

内容的提问来源于stack exchange,提问作者Ray Babich

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 02:22:40