GithubHelp home page GithubHelp logo

ytdl-core's Introduction

@distube/ytdl-core

DisTube fork of ytdl-core. This fork is dedicated to fixing bugs and adding features that are not merged into the original repo as soon as possible.

Buy Me a Coffee at ko-fi.com

Installation

npm install @distube/ytdl-core@latest

Make sure you're installing the latest version of @distube/ytdl-core to keep up with the latest fixes.

Usage

const ytdl = require("@distube/ytdl-core");
// TypeScript: import ytdl from '@distube/ytdl-core'; with --esModuleInterop
// TypeScript: import * as ytdl from '@distube/ytdl-core'; with --allowSyntheticDefaultImports
// TypeScript: import ytdl = require('@distube/ytdl-core'); with neither of the above

// Download a video
ytdl("http://www.youtube.com/watch?v=aqz-KE-bpKQ").pipe(require("fs").createWriteStream("video.mp4"));

// Get video info
ytdl.getBasicInfo("http://www.youtube.com/watch?v=aqz-KE-bpKQ").then(info => {
  console.log(info.title);
});

// Get video info with download formats
ytdl.getInfo("http://www.youtube.com/watch?v=aqz-KE-bpKQ").then(info => {
  console.log(info.formats);
});

Cookies Support

const ytdl = require("@distube/ytdl-core");

// (Optional) Below are examples, NOT the recommended options
const cookies = [
  { name: "cookie1", value: "COOKIE1_HERE" },
  { name: "cookie2", value: "COOKIE2_HERE" },
];

// (Optional) http-cookie-agent / undici agent options
// Below are examples, NOT the recommended options
const agentOptions = {
  pipelining: 5,
  maxRedirections: 0,
  localAddress: "127.0.0.1",
};

// agent should be created once if you don't want to change your cookie
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 });

How to get cookies

  • Install EditThisCookie extension for your browser.
  • Go to YouTube.
  • Log in to your account. (You should use a new account for this purpose)
  • Click on the extension icon and click "Export" icon.
  • Your cookie will be added to your clipboard and paste it into your code.
const ytdl = require("@distube/ytdl-core");
const agent = ytdl.createAgent([
  {
    domain: ".youtube.com",
    expirationDate: 1234567890,
    hostOnly: false,
    httpOnly: true,
    name: "LOGIN_INFO",
    path: "/",
    sameSite: "no_restriction",
    secure: true,
    session: false,
    value: "---xxx---",
  },
  "...",
]);
  • Or you can paste it into a file and use fs.readFileSync to read it.
const ytdl = require("@distube/ytdl-core");
const fs = require("fs");
const agent = ytdl.createAgent(JSON.parse(fs.readFileSync("cookies.json")));

Proxy Support

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 });

Use both proxy and 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 Rotation

Built-in ip rotation (getRandomIPv6) won't be updated and will be removed in the future, create your own ip rotation instead.

To implement IP rotation, you need to assign the desired IP address to the localAddress property within undici.Agent.Options. Therefore, you'll need to use a different ytdl.Agent for each IP address you want to use.

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

You can find the API documentation in the original repo. Except a few changes:

ytdl.getInfoOptions

ytdl.createAgent([cookies]): ytdl.Agent

cookies: an array of json cookies exported with EditThisCookie.

ytdl.createProxyAgent(proxy[, cookies]): ytdl.Agent

proxy: ProxyAgentOptions contains your proxy server information.

How to implement ytdl.Agent with your own Dispatcher

You can find the example here

Limitations

ytdl cannot download videos that fall into the following

  • Regionally restricted (requires a proxy)
  • Private (if you have access, requires cookies)
  • Rentals (if you have access, requires cookies)
  • YouTube Premium content (if you have access, requires cookies)
  • Only HLS Livestreams are currently supported. Other formats will get filtered out in ytdl.chooseFormats

Generated download links are valid for 6 hours, and may only be downloadable from the same IP address.

Rate Limiting

When doing too many requests YouTube might block. This will result in your requests getting denied with HTTP-StatusCode 429. The following steps might help you:

  • Update @distube/ytdl-core to the latest version
  • Use proxies (you can find an example here)
  • Extend the Proxy Idea by rotating (IPv6-)Addresses
    • read this for more information about this
  • Use cookies (you can find an example here)
    • for this to take effect you have to FIRST wait for the current rate limit to expire
  • Wait it out (it usually goes away within a few days)

Update Checks

The issue of using an outdated version of ytdl-core became so prevalent, that ytdl-core now checks for updates at run time, and every 12 hours. If it finds an update, it will print a warning to the console advising you to update. Due to the nature of this library, it is important to always use the latest version as YouTube continues to update.

If you'd like to disable this update check, you can do so by providing the YTDL_NO_UPDATE env variable.

env YTDL_NO_UPDATE=1 node myapp.js

Related Projects

  • DisTube - A Discord.js module to simplify your music commands and play songs with audio filters on Discord without any API key.
  • @distube/ytsr - DisTube fork of ytsr.
  • @distube/ytpl - DisTube fork of ytpl.

ytdl-core's People

Contributors

aasim-a avatar andrewrk avatar brunomoreira99 avatar coderaiser avatar dependabot-preview[bot] avatar depfu[bot] avatar favna avatar fent avatar firecontroller1847 avatar gatecrasher777 avatar greenkeeper[bot] avatar guichaguri avatar hcgrandon avatar mattez02 avatar moisout avatar rafer45 avatar raltamirano avatar roki100 avatar rubs019 avatar sank6 avatar sesam avatar shanethmoore avatar skick1234 avatar skybldev avatar thepieterdc avatar timeforaninja avatar tinyman avatar vladdrozd avatar waqasibrahim avatar yinglunq avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.