node-ytdl-core icon indicating copy to clipboard operation
node-ytdl-core copied to clipboard

Download a private video youtube

Open kamalkech opened this issue 10 months ago • 4 comments

ytdl-core: ^4.11.5

i try to use this example to download a private video youtube but not working. https://github.com/fent/node-ytdl-core/blob/master/example/cookies.js

on this example should inject cookie and x-youtube-identity-token so how can get cookie value ?

kamalkech avatar Apr 03 '24 10:04 kamalkech

I think you have to go to YouTube.com when you're signed in then put this in the console to see your cookies:

console.log(document.cookie)

But, I'm on a phone right now... so who knows.

twarped avatar Apr 11 '24 17:04 twarped

I got it to work but not using the cookie approach, but using the headers directly I am using "ytdl-core": "^4.11.5" and "node":"20.11.0"

To get the headers using Chrome:

  • Go to the specified private video
  • Open the Developer Tools
  • Go to the Network Tab (Refresh the page if needed)
  • Toggle the Doc Filter
  • You should find a request named watch?v=<VIDEO_ID>

Screenshot 2024-06-26 120405

  • Copy the request headers (Just right-click it > Copy > Copy as fetch (Node.js) > then extract the headers object)
  • Now use this headers object for requestOptions.header in option

Hope this helped 😄, I'll add my code too, if this worked don't forget to close the issue Omar Ezeldin

This is my code:

const ytdl = require('ytdl-core');
const path = require('path');
const fs = require('fs');

const videoID = "YOUR VIDEO ID";
const HEADERS = "YOUR HEADERS OBJECT";

const outputName = 'video.mp4';
const outputPath = path.resolve(__dirname, outputName);

const video = ytdl(videoID, {
	requestOptions: {
		headers: HEADERS
	},
});

video.on('info', (info) => {
	console.log('title:', info.videoDetails.title);
	console.log('rating:', info.player_response.videoDetails.averageRating);
	console.log('uploaded by:', info.videoDetails.author.name);
});

video.on('progress', (chunkLength, downloaded, total) => {
	const percent = downloaded / total;
	console.log('downloading', `${(percent * 100).toFixed(1)}%`);
});

video.on('end', () => {
	console.log('saved to', outputName);
});

video.pipe(fs.createWriteStream(outputPath));

omar-ezeldin avatar Jun 26 '24 09:06 omar-ezeldin

bro can u explain it a bit more to how to find these as i unable to find them

Achin-Aggarwal avatar Jul 09 '24 16:07 Achin-Aggarwal

Oh, that makes sense.

you have to go to the network tab in developer tools if you’re confused

twarped avatar Jul 11 '24 23:07 twarped