这是ytdl-core
的DisTube分支。该分支致力于尽快修复错误并添加未合并到原始仓库的功能。
<a href='https://ko-fi.com/skick' target='_blank'><img height='48' src='https://storage.ko-fi.com/cdn/kofi3.png' alt='在ko-fi.com为我买杯咖啡' /></a>
npm install @distube/ytdl-core@latest
请确保安装最新版本的@distube/ytdl-core
以获取最新的修复。
const ytdl = require("@distube/ytdl-core"); // TypeScript: import ytdl from '@distube/ytdl-core'; 使用 --esModuleInterop // TypeScript: import * as ytdl from '@distube/ytdl-core'; 使用 --allowSyntheticDefaultImports // TypeScript: import ytdl = require('@distube/ytdl-core'); 不使用上述任何选项 // 下载视频 ytdl("http://www.youtube.com/watch?v=aqz-KE-bpKQ").pipe(require("fs").createWriteStream("video.mp4")); // 获取视频信息 ytdl.getBasicInfo("http://www.youtube.com/watch?v=aqz-KE-bpKQ").then(info => { console.log(info.title); }); // 获取包含下载格式的视频信息 ytdl.getInfo("http://www.youtube.com/watch?v=aqz-KE-bpKQ").then(info => { console.log(info.formats); });
const ytdl = require("@distube/ytdl-core"); // (可选)以下是 示例,非推荐选项 const cookies = [ { name: "cookie1", value: "COOKIE1_HERE" }, { name: "cookie2", value: "COOKIE2_HERE" }, ]; // (可选)http-cookie-agent / undici agent 选项 // 以下是示例,非推荐选项 const agentOptions = { pipelining: 5, maxRedirections: 0, localAddress: "127.0.0.1", }; // 如果不想更改cookie,应该只创建一次agent const agent = ytdl.createAgent(cookies, agentOptions); ytdl.getBasicInfo("http://www.youtube.com/watch?v=aqz-KE-bpKQ", { agent }); ytdl.getInfo("http://www.youtube.com/watch?v=aqz-KE-bpKQ", { agent });
[!警告] 不要通过点击YouTube/Google账户管理器上的登出按钮来退出,这会使您的cookies失效。 您可以删除浏览器的cookies来在浏览器上登出。 或者使用隐身模式获取cookies,然后关闭它。
[!警告] 将剪贴板中的整个cookies数组粘贴到
createAgent
函数中。如果您不知道自己在做什么,请不要删除或编辑任何cookie。
[!警告] 确保您获取cookies时登录的账户同一时间只使用一个IP。这将使您的cookies保持更长时间的有效性。
const ytdl = require("@distube/ytdl-core"); const agent = ytdl.createAgent([ { domain: ".youtube.com", expirationDate: 1234567890, hostOnly: false, httpOnly: true, name: "---xxx---", path: "/", sameSite: "no_restriction", secure: true, session: false, value: "---xxx---", }, { "...": "...", }, ]);
fs.readFileSync
读取它。const ytdl = require("@distube/ytdl-core"); const fs = require("fs"); const agent = ytdl.createAgent(JSON.parse(fs.readFileSync("cookies.json")));
const ytdl = require("@distube/ytdl-core"); const agent = ytdl.createProxyAgent({ uri: "my.proxy.server" }); ytdl.getBasicInfo("http://www.youtube.com/watch?v=aqz-KE-bpKQ", { agent }); ytdl.getInfo("http://www.youtube.com/watch?v=aqz-KE-bpKQ", { agent });
同时使用代理和cookies:
const ytdl = require("@distube/ytdl-core"); const agent = ytdl.createProxyAgent({ uri: "my.proxy.server" }, [{ name: "cookie", value: "COOKIE_HERE" }]); ytdl.getBasicInfo("http://www.youtube.com/watch?v=aqz-KE-bpKQ", { agent }); ytdl.getInfo("http://www.youtube.com/watch?v=aqz-KE-bpKQ", { agent });
内置的IP轮换(getRandomIPv6
)将不再更新,并将在未来被移除,请创建您自己的IP轮换方案。
要实现 IP 轮换,你需要将所需的 IP 地址分配给 undici.Agent.Options
中的 localAddress
属性。
因此,你需要为每个要使用的 IP 地址使用不同的 ytdl.Agent
。
const ytdl = require("@distube/ytdl-core"); const { getRandomIPv6 } = require("@distube/ytdl-core/lib/utils"); const agentForARandomIP = ytdl.createAgent(undefined, { localAddress: getRandomIPv6("2001:2::/48"), }); ytdl.getBasicInfo("http://www.youtube.com/watch?v=aqz-KE-bpKQ", { agent: agentForARandomIP }); const agentForAnotherRandomIP = ytdl.createAgent(undefined, { localAddress: getRandomIPv6("2001:2::/48"), }); ytdl.getInfo("http://www.youtube.com/watch?v=aqz-KE-bpKQ", { agent: agentForAnotherRandomIP });
你可以在原始仓库中找到 API 文档。除了一些变更:
ytdl.getInfoOptions
requestOptions
现在是 undici
的 RequestOptions
。agent
:ytdl.Agent
ytdl.createAgent([cookies]): ytdl.Agent
cookies
:使用 EditThisCookie 导出的 JSON cookie 数组。
ytdl.createProxyAgent(proxy[, cookies]): ytdl.Agent
proxy
:ProxyAgentOptions
包含你的代理服务器信息。
ytdl.Agent
你可以在这里找到示例
ytdl 无法下载以下类型的视频:
生成的下载链接有效期为 6 小时,并且可能只能从相同的 IP 地址下载。
当发送过多请求时,YouTube 可能会进行封锁。这将导致你的请求被拒绝,并返回 HTTP 状态码 429。以下步骤可能会有所帮助: