Giter Club home page Giter Club 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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ytdl-core's Issues

Bug: WARNING: Could not parse n transform function.

Describe the bug

Dear users, hoping you are well, I come to the aid of this error that I have in my Discord Bot V14 project with Distube, which when I want to run Distube, the following error appears.

image

Debug File

1720559378400-base.txt

Environment

  • @distube/ytdl-core version: ^4.13.4
  • Node.js version: v18.20.3
  • Operating system: Windows 11

Discord invite is invalid

Describe the bug

The discord link, when clicked, says the invite is invalid.

Debug File

Environment

  • @distube/ytdl-core version:
  • Node.js version:
  • Operating system:

an error occurs when fetching a stream url

Describe the bug

I get an error when trying to fetch a googlevideo.com stream url, I can give u my code if u want.

Debug File

Environment

  • @distube/ytdl-core version: latest
  • Node.js version: 20.13.1
  • Operating system: Windows 11 Home

1721349981644-base.js

Describe the bug

Please report this issue with the "1721349981644-base.js" file on https://github.com/distubejs/ytdl-core/issues.
Stream URL may be slow but still works.

Debug File

const { EmbedPlayer } = require("../util/embed/embedPlayer");
const embed = new EmbedPlayer();
const { EmbedClass } = require("../util/embed/embedBase");
const embedClass = new EmbedClass();
const { ValidationServer } = require("../util/serverValidation");
const vali = new ValidationServer();
module.exports = {
name: "play",
aliases: ["p"],
description: "Enter your song link or song name to play",
usage: "play <URL/song name>",
voiceChannel: true,
options: [
{
name: "search",
description: "The song link or song name",
type: 3,
required: true,
},
],

async execute(client, message, args) {
if (vali.vali(client, message) == true) {
const { member, guildId, options, guild } = message;
const voiceChannel = member.voice.channel;
const string = args.join("");
try {
if (!args[0]) {
return embed.noSong(message);
}
if (
string.includes("playlist") === true &&
string.includes("spotify") === true
) {
return embed.noPlaylist(message);
} else if (
(string.includes("playlist") === true &&
string.includes("youtube") === true) ||
(string.includes("playlist") === true &&
string.includes("youtube") === true)
) {
return embed.noPlaylist(message);
} else if (
string.includes("youtube") === true ||
string.includes("youtu.be") === true
) {
console.log("Player")
await client.distube.play(message.member.voice.channel, string, {
message,
member: message.member,
});
} else if (string.includes("spotify") === true) {
await client.distube.play(message.member.voice.channel, string, {
message,
member: message.member,
});
} else {
return embed.notYoutubeLink(message);
}
} catch (err) {
console.log(err);
return embed.genericError(message);
}
} else {
return embedClass.noPermission(message);
}
},
};

Environment

  • @distube/ytdl-core version: "^4.11.10"
  • Node.js version: v20.11.1
  • Operating system:

Version 4.13.4 introduces major breaking changes

Describe the bug

Specifically, the upgrade of some dependencies here: efa92e9

One upgrade that caused uncompilable code for me was undici 5->6, as detailed by somebody else here: nodejs/undici#3010

In general, as this commit upgrades major versions of the dependencies, it should cause a major version bump in the next release.

Debug File

N/A

Environment

  • @distube/ytdl-core version: 4.13.4
  • Node.js version: 20
  • Operating system: All

Module not found: Can't resolve 'fs'

Describe the bug

Attempting to use ytdl.getInfo results in module not found error in Next.js 14.2.3

Debug File

image

Environment

  • @distube/ytdl-core version: 4.13.3
  • Node.js version: 20.13.1
  • Operating system: Linux (Windows WSL)

Download is generating 0 byte file.

Describe the bug

I used the sample code for download without changing anything but it seems to be downloading 0 byte video.mp4 file.

Debug File

The mime urls returned from console log for info.formats are also not playable in browser as well.

Environment

  • @distube/ytdl-core version: 4.13.5
  • Node.js version:v18.19.0
  • Operating system:Debian 12

1720622064544-base.js

Describe the bug

Debug File

Environment

  • @distube/ytdl-core version:
  • Node.js version:
  • Operating system:

How to use proxy with `@distube/ytdl-core`

Thanks for the fork and for help!

Here is the error:
InvalidArgumentError: unsupported opts.agent. Did you mean opts.client? 25|test | at Object.request (/home/ubuntu/ponyapp_debug/node_modules/undici/index.js:84:13) 25|test | at exports.request (/home/ubuntu/ponyapp_debug/node_modules/@distube/ytdl-core/lib/utils.js:178:28) 25|test | at /home/ubuntu/ponyapp_debug/node_modules/@distube/ytdl-core/lib/info.js:127:59 25|test | at Cache.getOrSet (/home/ubuntu/ponyapp_debug/node_modules/@distube/ytdl-core/lib/cache.js:29:19) 25|test | at getWatchHTMLPageBody (/home/ubuntu/ponyapp_debug/node_modules/@distube/ytdl-core/lib/info.js:127:33) 25|test | at getWatchHTMLPage (/home/ubuntu/ponyapp_debug/node_modules/@distube/ytdl-core/lib/info.js:262:20) 25|test | at retryFunc (/home/ubuntu/ponyapp_debug/node_modules/@distube/ytdl-core/lib/info.js:218:22) 25|test | at async pipeline (/home/ubuntu/ponyapp_debug/node_modules/@distube/ytdl-core/lib/info.js:159:23) 25|test | at async exports.getBasicInfo (/home/ubuntu/ponyapp_debug/node_modules/@distube/ytdl-core/lib/info.js:66:14) { 25|test | code: 'UND_ERR_INVALID_ARG' 25|test | }

and here is my function call:
getOxylabsDataCenterProxyAgent() { const proxy =http://user:[email protected]:10000`;
const agent = new HttpsProxyAgent.HttpsProxyAgent(proxy);
return agent
}

info = await ytdl.getBasicInfo(videoUrl, {
requestOptions: {
agent: this.getOxylabsDataCenterProxyAgent(),
},
});
`

here is undici source code:
` const { agent, dispatcher = getGlobalDispatcher() } = opts

if (agent) {
  throw new InvalidArgumentError('unsupported opts.agent. Did you mean opts.client?')
}

`

the error didnt appear in native ytdl lib though

MinigetError: Status code: 403

It seems like there was another change at YouTube - it worked fine until yesterday for us and now we get 403 errors again.

proxy error

Describe the bug

My logs show an error when trying to use the proxy agent suggesting that the example code (I basically used this) is wrong.

Debug File

Error in audio stream for jxXxT72tuU0: InvalidArgumentError: unsupported opts.agent. Did you mean opts.client? 
2023-12-15 22:36:27.025 PST
videoIdToMP4aiu4uirmzozb     at /workspace/node_modules/undici/index.js:86:13 
2023-12-15 22:36:27.025 PST
videoIdToMP4aiu4uirmzozb     at exports.request (/workspace/node_modules/@distube/ytdl-core/lib/utils.js:172:21) 
2023-12-15 22:36:27.025 PST
videoIdToMP4aiu4uirmzozb     at /workspace/node_modules/@distube/ytdl-core/lib/info.js:107:59 
2023-12-15 22:36:27.025 PST
videoIdToMP4aiu4uirmzozb     at Cache.getOrSet (/workspace/node_modules/@distube/ytdl-core/lib/cache.js:29:19) 
2023-12-15 22:36:27.025 PST
videoIdToMP4aiu4uirmzozb     at getWatchHTMLPageBody (/workspace/node_modules/@distube/ytdl-core/lib/info.js:107:33) 
2023-12-15 22:36:27.025 PST
videoIdToMP4aiu4uirmzozb     at getWatchHTMLPage (/workspace/node_modules/@distube/ytdl-core/lib/info.js:188:20) 
2023-12-15 22:36:27.025 PST
videoIdToMP4aiu4uirmzozb     at retryFunc (/workspace/node_modules/@distube/ytdl-core/lib/info.js:144:22) 
2023-12-15 22:36:27.025 PST
videoIdToMP4aiu4uirmzozb     at async exports.getBasicInfo (/workspace/node_modules/@distube/ytdl-core/lib/info.js:50:16) { 
2023-12-15 22:36:27.025 PST
videoIdToMP4aiu4uirmzozb   code: 'UND_ERR_INVALID_ARG' 
2023-12-15 22:36:27.025 PST
videoIdToMP4aiu4uirmzozb } 

This is my code:

const agent = ytdl.createProxyAgent({ uri: "https://xxxx:7000" });
....

const audioStream = ytdl(videoUrl, { quality: lowestQualityFormat.itag, requestOptions: { agent: agent } });

Environment

  • @distube/ytdl-core version: "@distube/ytdl-core": "^4.13.3",
  • Node.js version: "node": "18"
  • Operating system: Linux on Firebase Functions

what i should do?

when I play music from youtube, I got an issue like this.

Could not parse n transform function, please report it on @distube/ytdl-core GitHub.

what i should do?

How can I set quality for dowloading a private video with cookie?

Describe the bug

  • I can download a private video with cookie but I cannot set quality for this video. When I set quality so download an zero by video.

Environment

  • @distube/ytdl-core version: 4.13.5
  • Node.js version: 22.1.0
  • Operating system: MacOS

My code

const cookies = [ ];
const ytdl = require("@distube/ytdl-core");
const agent = ytdl.createAgent(cookies);

const videoID = '';

ytdl(`http://www.youtube.com/watch?v=${videoID}`, { agent, quality: 'highestvideo' })
.pipe(require("fs")
.createWriteStream("video3.mp4"));

TypeScript compilation fails

Describe the bug

tsc --project . fails because of the types of a third party lib.

node_modules/@distube/ytdl-core/typings/index.d.ts:3:43 - error TS7016: Could not find a declaration file for module 'tough-cookie'. '<project-root>/node_modules/tough-cookie/lib/cookie.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/tough-cookie` if it exists or add a new declaration (.d.ts) file containing `declare module 'tough-cookie';`

3   import { Cookie as CK, CookieJar } from 'tough-cookie';

Environment

  • @distube/ytdl-core version: 4.12.1
  • Node.js version: v16.17.1
  • Operating system: macOs Monterey (12.5)

Could not parse n transform function.

i got message in "@distube/ytdl-core": "^4.11.13", :

[WARN]: Could not parse n transform function.
Please report this issue with the contents of "1690046714282-base.js" file on https://github.com/distubejs/ytdl-core/issues.
Stream URL may be slow but still works.

so im gonna send zip file right here:
1690046714282-base.zip

MinigetError: Status code: 403 Appeared again

Describe the bug

The library was working fine until suddenly it started throwing the following error:

MinigetError: Status code: 403
    at ClientRequest.<anonymous> (/root/project/node_modules/miniget/dist/index.js:206:27)
    at Object.onceWrapper (node:events:626:26)
    at ClientRequest.emit (node:events:511:28)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (node:_http_client:693:27)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:119:17)
    at TLSSocket.socketOnData (node:_http_client:535:22)
    at TLSSocket.emit (node:events:511:28)
    at addChunk (node:internal/streams/readable:332:12)
    at readableAddChunk (node:internal/streams/readable:305:9)
    at Readable.push (node:internal/streams/readable:242:10) {
  statusCode: 403
}

It does not happen consistently, it happens a few times, then its gone, then it happens again. All of these with a video that is for sure download-able.

Debug File

const stream = ytdl(ytdl.getVideoID(video), {
          filter: "audioonly",
          format: "webm",
        }).on("error", (error) => console.log(error));

        return stream.pipe(res, { end: true });

Environment

  • @distube/ytdl-core version:
  • Node.js version: v20.3.1
  • Operating system: Ubuntu 24.04

1720600101710-base.js

Describe the bug

1720600101710-base.js

Debug File

Environment

  • @distube/ytdl-core version:
  • Node.js version:
  • Operating system:

error in version 4.13.5

UnrecoverableError: Sign in to confirm you're not a bot
at privateVideoError (/home/container /nod e_modules /ytdl-core/lib/info.js:91:12)
at exports . getBasicInfo (/home/container/ node_modules /ytdl - core/lib/info.js :54:22
at process . processTicksAndRejections (nod e:internal/process/task_queues : 95: 5) unhandle
dRejection UnrecoverableError: Sign in to confirm you' re not a bot
at privateVideoError (/home /container / nod e_modules /ytdl-core/lib/info. js:91 :12)
at exports.getBasicInfo (/home/container/ node_modules /ytdl - core/lib/info. js :54:22
at process . processTicksAndRejections (nod e:internal/process/task_queues : 95:5) unhandle
dRejection

Audio plays for a few seconds, then stops

Describe the bug

After the last update, the audio plays for a few seconds, then stops. Using with Discord and the new CookieManager.

Environment

  • @distube/ytdl-core version: 4.12.1
  • Node.js version: v18
  • Operating system: Ubuntu

This video is unavailable

I am simply starting node in ssh and I type these commands one by one:

  1. const ytdl = require("@distube/ytdl-core");
  2. await ytdl.getBasicInfo("http://www.youtube.com/watch?v=aqz-KE-bpKQ")

Returns:

Uncaught UnrecoverableError: This video is unavailable
at exports.playError (/root/project/node_modules/@distube/ytdl-core/lib/utils.js:164:12)
at exports.getBasicInfo (/root/project/node_modules/@distube/ytdl-core/lib/info.js:52:25)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Happens for every video.

Environment

  • @distube/ytdl-core version: 4.13.5
  • Node.js version: v20.3.1
  • Operating system: Ubuntu 24.04

Cannot play age restricted videos after adding cookies

Describe the bug

I was wondering how am I supposed to fix this error when youtube requires me to sign in I did use the agent and provided the cookie that I think I need

This is where I download the song

videoStream = ytdl(song.url,{
                agent: agent,
                quality: "highestaudio"
            });

I'm using the cookie with LOGIN_INFO as it's name

Debug File

S:\MyDev\_Projects\Discord\TheDirector\node_modules\@distube\ytdl-core\lib\info.js:92
    return new UnrecoverableError(playability.reason || (playability.messages && playability.messages[0]));
           ^
Error: Sign in to confirm your age
    at privateVideoError (S:\MyDev\_Projects\Discord\TheDirector\node_modules\@distube\ytdl-core\lib\info.js:92:12)
    at exports.getBasicInfo (S:\MyDev\_Projects\Discord\TheDirector\node_modules\@distube\ytdl-core\lib\info.js:54:22)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

Environment

  • @distube/ytdl-core version: ^4.13.3
  • Node.js version: v18.17.0
  • Operating system: Windows 11

Error: Sign in to confirm youโ€™re not a bot

Describe the bug

Any playback requests result in an error, playback doesnt happen.

Environment

Docker

  • @distube/ytdl-core version: 4.13.4
  • Node.js version: 18.7.0
  • Operating system: Ubuntu/Docker

Full error stack:

(Usenode --trace-warnings ...to show where the warning was created) /usr/app/node_modules/@distube/ytdl-core/lib/info.js:91 return new UnrecoverableError(playability.reason || (playability.messages && playability.messages[0])); ^ Error: Sign in to confirm youโ€™re not a bot at privateVideoError (/usr/app/node_modules/@distube/ytdl-core/lib/info.js:91:12) at exports.getBasicInfo (/usr/app/node_modules/@distube/ytdl-core/lib/info.js:54:22) at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Seems this is likely something youtube have done, not sure if it can be circumvented or not.

Generated IP address not being used in outgoing undici request

Describe the bug

IP address from ipv6 rotation is not actually used in outgoing network request.

undici does not have localAddress on request() but instead on Client. You need to create a new Client and set localAddress there and then send request through Client.request(). Something like this:

exports.request = async(url, options = {}) => {
  const { requestOptions } = options;

  const client = new Client('https://www.youtube.com', {
    localAddress: requestOptions.localAddress,
    autoSelectFamily: true
  });

  const req = await client.request({
    path: '/watch?v=rUxyKA_-grg', // example path
    method: 'GET',
  });

  //const req = await request(url, requestOptions);
  const code = req.statusCode.toString();
  if (code.startsWith('2')) {
    if (req.headers['content-type'].includes('application/json')) return req.body.json();
    return req.body.text();
  }
  if (code.startsWith('3')) return exports.request(req.headers.location, options);
  const e = new Error(`Status code: ${code}`);
  e.statusCode = req.statusCode;
  throw e;
};

The above snippet does not work, it throws some errors, so treat as pseudo code. But maybe it helps to understand that you need to initialize a new Client where localAddress is specified before sending the request. I unfortunately don't have any experience with @distube/ytdl-core codebase or undici, so don't know how to implement the fix.

Currently, @distube/ytdl-core versions using undici is not actually using the generated IP address, and thus the "IP rotation" has no effect.

Environment

  • @distube/ytdl-core version: 4.12.1
  • Node.js version: v18.17.1
  • Operating system: Win 11 Pro 22621.2215

720 video has no sound

I've noticed that @distube/ytdl-core no longer provides 720p videos with audio. Previously, 320p and 720p videos included both video and audio streams, but now 720p videos seem to come without audio.

Additionally, I attempted to use FFmpeg to merge audio and video streams, but I encountered performance issues. When starting the 6th download (while there was already 5 in progress), it failed to initiate because the any of the 5 requests had not completed yet.

  const info = await ytdl.getInfo("https://www.youtube.com/watch?v=rTDaZoDDW5g", { agent });
  console.log(
    info.formats.filter((format) => format.hasAudio && format.hasVideo)
  );

Output:

[
   {
      "mimeType":"video/mp4; codecs=\"avc1.42001E, mp4a.40.2\"",
      "qualityLabel":"360p",
      "bitrate":603544,
      "audioBitrate":96,
      "itag":18,
      "url":"https://rr5---sn-qxa7sn7r.googlevideo.com/videoplayback?expire=1720904337&ei=MZaSZo3GDJi4z7sPg-G24Ak&ip=122.177.100.176&id=o-ALAl6BwtFMvg5rVPCRYRO13rHUeiYLs7IUV63nZn_oSl&itag=18&source=youtube&requiressl=yes&xpc=EgVo2aDSNQ%3D%3D&mh=zE&mm=31%2C29&mn=sn-qxa7sn7r%2Csn-ci5gup-qxaes&ms=au%2Crdu&mv=m&mvi=5&pl=21&initcwndbps=1080000&bui=AXc671Lk-cf6upJKtLhhva_ByfaR2OHrqmPvzDxApIr7vPMGceEEtJyIm19o3xL5AjGb-dOWrQDQ37p0&spc=NO7bAZ9Gls22W0RI0G0NFjdRqrL2CxQlAX-j-A5HAXiSTKrGCj0DuSCcL0wj&vprv=1&svpuc=1&mime=video%2Fmp4&ns=UNNVKTipxVl4xHL_DAm2SX0Q&rqh=1&gir=yes&clen=64420880&ratebypass=yes&dur=853.960&lmt=1715503839003671&mt=1720882404&fvip=6&c=WEB&sefc=1&txp=5538434&n=YoMpAJabQmX4dw&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cxpc%2Cbui%2Cspc%2Cvprv%2Csvpuc%2Cmime%2Cns%2Crqh%2Cgir%2Cclen%2Cratebypass%2Cdur%2Clmt&sig=AJfQdSswRgIhAPKjCdYs1pcMQsEmIrSVGFiH_Vvq58H9n89DrjsdyFM7AiEAotUKVJvvJsCUzK8B_M1jMASP4cAqafKVr3K8b0kXaIA%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AHlkHjAwRAIgOLRkKICglQb4EXbEzcT9PV_dnXCUdKGFLRBwJjzN-uUCIB3hfjFvaXCwpLWDKZjML7gVsGxnsa3loWPlWB6XNexr",
      "width":640,
      "height":360,
      "lastModified":"1715503839003671",
      "contentLength":"64420880",
      "quality":"medium",
      "fps":30,
      "projectionType":"RECTANGULAR",
      "averageBitrate":603502,
      "audioQuality":"AUDIO_QUALITY_LOW",
      "approxDurationMs":"853960",
      "audioSampleRate":"44100",
      "audioChannels":2,
      "hasVideo":true,
      "hasAudio":true,
      "container":"mp4",
      "codecs":"avc1.42001E, mp4a.40.2",
      "videoCodec":"avc1.42001E",
      "audioCodec":"mp4a.40.2",
      "isLive":false,
      "isHLS":false,
      "isDashMPD":false
   }
]

FFmpeg Solution:
Looking for optimization help to handle more requests.

const express = require("express");
const ffmpegPath = require("ffmpeg-static");
const cp = require("child_process");
const stream = require("stream");
const ytdl = require("@distube/ytdl-core");

const validQualities = [
  "144p",
  "240p",
  "360p",
  "480p",
  "720p",
  "1080p",
  "1440p",
  "2160p",
];

const getVideoFormat = (formats, quality) => {
  // First, try to find HDR format if quality is 4K or above

  console.log(formats);

  if (["2160p"].includes(quality)) {
    const hdrFormat = formats.find(
      (format) =>
        format.qualityLabel === quality &&
        format.hasVideo &&
        (format.colorInfo?.primaries === "bt2020" ||
          format.colorInfo?.transferCharacteristics === "smpte2084")
    );
    if (hdrFormat) return hdrFormat;
  }

  // If HDR not found or not applicable, look for standard format
  let format = formats.find(
    (format) =>
      format.qualityLabel === quality && format.hasVideo && format.hasAudio
  );

  if (!format) {
    format = formats.find(
      (format) => format.qualityLabel === quality && format.hasVideo
    );
  }

  return format;
};

const ytmixer = (info, quality, options = {}) => {
  const result = new stream.PassThrough({
    highWaterMark: options.highWaterMark || 1024 * 512,
  });

  const videoFormat = getVideoFormat(info.formats, quality);

  if (!videoFormat) {
    result.emit(
      "error",
      new Error(`Requested quality ${quality} is not available for this video`)
    );
    return result;
  }

  if (videoFormat.hasAudio) {
    ytdl.downloadFromInfo(info, { format: videoFormat }).pipe(result);
  } else {
    let audioStream = ytdl.downloadFromInfo(info, {
      ...options,
      quality: "highestaudio",
    });
    let videoStream = ytdl.downloadFromInfo(info, { format: videoFormat });

    let ffmpegArgs = [
      "-loglevel",
      "8",
      "-hide_banner",
      "-i",
      "pipe:3",
      "-i",
      "pipe:4",
      "-map",
      "0:a",
      "-map",
      "1:v",
      "-c",
      "copy",
    ];

    if (
      videoFormat.colorInfo?.primaries === "bt2020" ||
      videoFormat.colorInfo?.transferCharacteristics === "smpte2084"
    ) {
      ffmpegArgs.push(
        "-color_primaries",
        "bt2020",
        "-color_trc",
        "smpte2084",
        "-colorspace",
        "bt2020nc"
      );
    }

    ffmpegArgs.push("-f", "matroska", "pipe:5");

    let ffmpegProcess = cp.spawn(ffmpegPath, ffmpegArgs, {
      windowsHide: true,
      stdio: ["inherit", "inherit", "inherit", "pipe", "pipe", "pipe"],
    });
    audioStream.pipe(ffmpegProcess.stdio[3]);
    videoStream.pipe(ffmpegProcess.stdio[4]);
    ffmpegProcess.stdio[5].pipe(result);
  }

  return result;
};

const app = express();

app.get("/video", async (req, res) => {
  const link = req.query.url;
  const quality = req.query.quality || "720p";

  if (!link) {
    return res
      .status(400)
      .send("Please provide a video URL as a query parameter");
  }

  if (!validQualities.includes(quality)) {
    return res
      .status(400)
      .send(`Invalid quality. Valid options are: ${validQualities.join(", ")}`);
  }

  try {
    const info = await ytdl.getInfo(link);
    const videoTitle = info.videoDetails.title.replace(/[^\w\s]/gi, "");
    const videoFormat = getVideoFormat(info.formats, quality);

    if (!videoFormat) {
      return res
        .status(400)
        .send(`Requested quality ${quality} is not available for this video`);
    }

    const isHDR =
      videoFormat.colorInfo?.primaries === "bt2020" ||
      videoFormat.colorInfo?.transferCharacteristics === "smpte2084";
    const fileName = `${videoTitle}_${quality}${isHDR ? "_HDR" : ""}.mp4`;

    res.setHeader("Content-Disposition", `attachment; filename="${fileName}"`);
    res.setHeader("Content-Type", "video/mp4");

    const mixer = ytmixer(info, quality);
    mixer.on("error", (err) => {
      console.error("Mixer error:", err);
      if (!res.headersSent) {
        res.status(400).send(err.message);
      }
    });
    mixer.pipe(res);
  } catch (error) {
    console.error("Error:", error);
    if (!res.headersSent) {
      res.status(500).send("An error occurred while processing the video");
    }
  }
});

app.listen(3000, () => {
  console.log("Server is running on http://localhost:3000");
});

Environment

  • @distube/ytdl-core version: 4.13.5
  • Node.js version: v18.17.0
  • Operating system: MacOs

Aborted error

Describe the bug

Crashes during playback when using with Discord.js

Debug File

Error: aborted
    at TLSSocket.socketCloseListener (node:_http_client:460:19)
    at TLSSocket.emit (node:events:530:35)
    at node:net:337:12
    at TCP.done (node:_tls_wrap:657:7) {
  code: 'ECONNRESET'
}
AudioPlayerError: aborted
    at TLSSocket.socketCloseListener (node:_http_client:460:19)
    at TLSSocket.emit (node:events:530:35)
    at node:net:337:12
    at TCP.done (node:_tls_wrap:657:7) {
  resource: AudioResource {
    playStream: Encoder {
      _events: [Object],
      _readableState: [ReadableState],
      _writableState: [WritableState],
      allowHalfOpen: true,
      _maxListeners: undefined,
      _eventsCount: 5,
      encoder: null,
      _options: [Object],
      _required: 3840,
      _buffer: null,
      [Symbol(shapeMode)]: true,
      [Symbol(kCapture)]: false,
      [Symbol(kCallback)]: [Function: bound onwrite]
    },
    edges: [ [Object], [Object], [Object] ],
    metadata: null,
    volume: VolumeTransformer {
      _events: [Object],
      _readableState: [ReadableState],
      _writableState: [WritableState],
      allowHalfOpen: true,
      _maxListeners: undefined,
      _eventsCount: 5,
      _readInt: [Function (anonymous)],
      _writeInt: [Function (anonymous)],
      _bits: 16,
      _bytes: 2,
      _extremum: 32768,
      volume: 0.2182910708939091,
      _chunk: null,
      [Symbol(shapeMode)]: true,
      [Symbol(kCapture)]: false,
      [Symbol(kCallback)]: [Function: bound onwrite]
    },
    encoder: Encoder {
      _events: [Object],
      _readableState: [ReadableState],
      _writableState: [WritableState],
      allowHalfOpen: true,
      _maxListeners: undefined,
      _eventsCount: 5,
      encoder: null,
      _options: [Object],
      _required: 3840,
      _buffer: null,
      [Symbol(shapeMode)]: true,
      [Symbol(kCapture)]: false,
      [Symbol(kCallback)]: [Function: bound onwrite]
    },
    audioPlayer: AudioPlayer {
      _events: [Object: null prototype],
      _eventsCount: 1,
      _maxListeners: undefined,
      _state: [Object],
      subscribers: [Array],
      behaviors: [Object],
      debug: [Function (anonymous)],
      [Symbol(shapeMode)]: false,
      [Symbol(kCapture)]: false
    },
    playbackDuration: 116980,
    started: true,
    silencePaddingFrames: 5,
    silenceRemaining: -1
  }
}

Btw on original ytdl-core the fix was passing this as options:

{
	filter: "audioonly" as Filter,
	highWaterMark: 1 << 62,
	liveBuffer: 1 << 62,
	dlChunkSize: 0,
	quality: "highestaudio",
}

Environment

  • @distube/ytdl-core version: 4.13.5
  • Node.js version: v20.12.1
  • Operating system: Ubuntu 22.04 Linux aarch64-8gb 5.15.0-1051-oracle #57-Ubuntu SMP Wed Jan 24 18:31:24 UTC 2024 aarch64 aarch64 aarch64 GNU/Linux

Issue with your ydtl-core fork package

[READ MY WHOLE MESSAGE BEFORE ANSWERING PLS]

Using this code with your package

(async function(){
    var ytdl = require("ytdl-core");
    var ended = false;
    ytdl("videoidplaceholder", {
        "quality": "highestvideo"
    })
    .pipe(require("fs").createWriteStream("video.mp4"))
    .on("finish", function () {
        ended = true;
    });
    while (ended === false) {
        await new Promise(resolve => setTimeout(resolve, 100));
    }
})()

First i installed and used ytdl-core base package then one day it stoped working so i read this: fent#1261 which led me to a comment with as solution installing your package with

npm i ytdl-core@npm:@distube/ytdl-core

This was supposed to overwrite the base package so i don't have to change my code and it did but now when i use my code it just downloads a 0 byte empty file.

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.