npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

youtube-comment-downloader

v0.1.1

Published

Simple script for downloading Youtube comments without using the Youtube API

Readme

youtube-comment-downloader

一个十分简单的脚本,用于下载 YouTube 评论而无需使用 YouTube API。输出格式为行分隔的 JSON。

egbertbouman/youtube-comment-downloader 的 Node.js/TypeScript 移植版本,并添加了代理支持。

语言版本

English | 中文

安装

通过 pnpm 安装此包(推荐):

pnpm add youtube-comment-downloader

或使用 npm:

npm install youtube-comment-downloader

全局 CLI 使用:

pnpm add -g youtube-comment-downloader
# 或
npm install -g youtube-comment-downloader

命令行界面使用

$ youtube-comment-downloader --help
Usage: youtube-comment-downloader [options]

下载 YouTube 评论而无需使用 YouTube API

Options:
  -y, --youtubeid <id>     要下载评论的 YouTube 视频 ID
  -u, --url <url>          要下载评论的 YouTube URL
  -o, --output <file>      输出文件名(输出格式为行分隔的 JSON)
  -p, --pretty             将输出格式更改为缩进的 JSON
  -l, --limit <number>     限制评论数量
  -a, --language <lang>    YouTube 生成文本的语言(例如 zh-CN)
  --proxy <uri>            代理 URI(例如 http://user:[email protected]:8080)
  -s, --sort <number>      是否下载热门评论(0)或最新评论(1)。默认为 1
  -h, --help               显示帮助信息

示例

使用视频 URL 下载评论:

youtube-comment-downloader --url https://www.youtube.com/watch?v=ScMzIvxBSi4 --output ScMzIvxBSi4.json

使用视频 ID 下载评论:

youtube-comment-downloader --youtubeid ScMzIvxBSi4 --output ScMzIvxBSi4.json

使用美化格式下载:

youtube-comment-downloader --youtubeid ScMzIvxBSi4 --output ScMzIvxBSi4.json --pretty

仅下载前 100 条评论:

youtube-comment-downloader --youtubeid ScMzIvxBSi4 --output ScMzIvxBSi4.json --limit 100

下载热门评论而非最新评论:

youtube-comment-downloader --youtubeid ScMzIvxBSi4 --output ScMzIvxBSi4.json --sort 0

使用代理下载:

youtube-comment-downloader --youtubeid ScMzIvxBSi4 --output ScMzIvxBSi4.json --proxy http://proxy.example.com:8080

使用带身份验证的代理下载:

youtube-comment-downloader --youtubeid ScMzIvxBSi4 --output ScMzIvxBSi4.json --proxy http://username:[email protected]:8080

对于以 -(破折号)开头的 YouTube ID,使用 = 语法:

youtube-comment-downloader --youtubeid=-idwithdash --output output.json

作为库使用

您也可以在 Node.js/TypeScript 项目中以编程方式使用此包:

JavaScript 示例

const { YoutubeCommentDownloader, SORT_BY_POPULAR } = require('youtube-comment-downloader');

async function downloadComments() {
  const downloader = new YoutubeCommentDownloader();
  
  // 从 URL 下载评论
  const comments = downloader.getCommentsFromUrl(
    'https://www.youtube.com/watch?v=ScMzIvxBSi4',
    SORT_BY_POPULAR
  );
  
  // 打印前 10 条评论
  let count = 0;
  for await (const comment of comments) {
    console.log(comment);
    if (++count >= 10) break;
  }
}

downloadComments();

TypeScript 示例

import { YoutubeCommentDownloader, SORT_BY_POPULAR, Comment } from 'youtube-comment-downloader';

async function downloadComments(): Promise<void> {
  const downloader = new YoutubeCommentDownloader();
  
  // 从视频 ID 下载评论
  const comments = downloader.getComments('ScMzIvxBSi4', SORT_BY_POPULAR);
  
  // 收集所有评论
  const allComments: Comment[] = [];
  for await (const comment of comments) {
    allComments.push(comment);
  }
  
  console.log(`下载了 ${allComments.length} 条评论`);
}

downloadComments();

使用代理支持下载

import { YoutubeCommentDownloader, SORT_BY_RECENT, Comment } from 'youtube-comment-downloader';

async function downloadWithProxy(): Promise<void> {
  // 创建带代理配置的下载器
  const downloader = new YoutubeCommentDownloader({
    proxy: { uri: 'http://proxy.example.com:8080' }
  });
  
  // 使用代理下载评论
  const comments = downloader.getComments('ScMzIvxBSi4', SORT_BY_RECENT);
  
  for await (const comment of comments) {
    console.log(`${comment.author}: ${comment.text}`);
  }
}

downloadWithProxy();

使用语言偏好下载

const { YoutubeCommentDownloader, SORT_BY_RECENT } = require('youtube-comment-downloader');

async function downloadWithLanguage() {
  const downloader = new YoutubeCommentDownloader();
  
  // 使用中文语言偏好下载评论
  const comments = downloader.getComments(
    'ScMzIvxBSi4',
    SORT_BY_RECENT,
    'zh-CN'  // 语言代码
  );
  
  for await (const comment of comments) {
    console.log(`${comment.author}: ${comment.text}`);
  }
}

API 参考

YoutubeCommentDownloader

用于下载 YouTube 评论的主类。

构造函数

new YoutubeCommentDownloader(options?)

创建 YouTube 评论下载器的新实例。

  • options (DownloaderOptions, 可选): 配置选项
    • proxy (ProxyConfig, 可选): 代理配置
      • uri (string): 代理 URI(例如 http://proxy.example.com:8080http://user:[email protected]:8080

方法

getComments(youtubeId, sortBy?, language?, sleepTime?)

通过视频 ID 下载 YouTube 视频的评论。

  • youtubeId (string): YouTube 视频 ID
  • sortBy (number, 可选): 排序顺序 - SORT_BY_RECENT (1) 或 SORT_BY_POPULAR (0)。默认:SORT_BY_RECENT
  • language (string, 可选): YouTube 生成文本的语言代码(例如 'zh-CN', 'en', 'ja')
  • sleepTime (number, 可选): 请求之间的休眠时间(秒)。默认:0.1

返回:AsyncGenerator<Comment> - 产生评论对象的异步生成器

getCommentsFromUrl(youtubeUrl, sortBy?, language?, sleepTime?)

通过 URL 下载 YouTube 视频的评论。

  • youtubeUrl (string): 完整的 YouTube 视频 URL
  • sortBy (number, 可选): 排序顺序 - SORT_BY_RECENT (1) 或 SORT_BY_POPULAR (0)。默认:SORT_BY_RECENT
  • language (string, 可选): YouTube 生成文本的语言代码
  • sleepTime (number, 可选): 请求之间的休眠时间(秒)。默认:0.1

返回:AsyncGenerator<Comment> - 产生评论对象的异步生成器

评论对象

生成器产生的每个评论都具有以下结构:

interface Comment {
  cid: string;         // 评论 ID
  text: string;        // 评论文本内容
  time: string;        // 时间字符串(例如 "2小时前")
  author: string;      // 评论作者的显示名称
  channel: string;     // 作者的频道 ID
  votes: string;       // 点赞数
  replies: number;     // 回复数
  photo: string;       // 作者头像 URL
  heart: boolean;      // 评论是否收到视频创作者的爱心
  reply: boolean;      // 这是否是对另一条评论的回复
  time_parsed?: number; // Unix 时间戳(如果可解析)
  paid?: string;       // 付费评论/超级聊天消息(如果适用)
}

常量

  • SORT_BY_POPULAR (0): 按热门程度排序评论
  • SORT_BY_RECENT (1): 按最新程度排序评论(最新优先)

功能特性

  • 无需 YouTube API 密钥
  • 支持视频 URL 和视频 ID
  • 下载所有评论包括回复
  • 按热门或最新排序
  • 语言偏好支持
  • 代理支持 - 使用 HTTP/HTTPS 代理改善访问
  • 失败时自动重试
  • 自动处理同意页面
  • 支持付费评论/超级聊天
  • 行分隔 JSON 或美化格式输出
  • 完整的 TypeScript 支持

开发

从源码构建

# 克隆仓库
git clone https://github.com/p697/node-youtube-comment-downloader.git
cd node-youtube-comment-downloader

# 安装依赖(使用 pnpm)
pnpm install

# 构建项目
pnpm run build

# 运行测试
pnpm test

# 以监视模式运行
pnpm run watch

运行测试

# 运行所有测试
pnpm test

# 以监视模式运行测试
pnpm run test:watch

# 运行带覆盖率的测试
pnpm test -- --coverage

许可证

MIT

贡献

欢迎贡献!请随时提交 Pull Request。

免责声明

此包仅用于教育目的。使用此工具时,请尊重 YouTube 的服务条款以及内容创作者和评论者的权利。