Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get Youtube video urls
// Run from the dev tools console of any Youtube video
// Accurate as of January 6, 2020.
// Removed ES5 version.
// ES6 version
(async () => {
// At some point from the last update of this script, Youtube no longer exposes ytplayer as a window level variable.
// The script element containing the ytplayer and config code is removed from the DOM.
// Here we re-fetch the raw html from the video url and then eval it, playing ytplayer into the global context.
// The old code to extract the video urls continues to work as before then.
// Running this script messes with the video already loaded on the page. Be sure to refresh the video page on each attempt to get the videoUrls.
const html = await fetch(window.location.href).then((resp) => resp.text()).then((text) => text);
const start = html.indexOf('var ytplayer = ytplayer');
const end = html.indexOf('</script', start);
eval(html.slice(start, end));
const videoUrls = ytplayer.config.args.adaptive_fmts
.split(',')
.map(item => item
.split('&')
.reduce((prev, curr) => (curr = curr.split('='),
Object.assign(prev, { [curr[0]]: decodeURIComponent(curr[1]) })
), {})
)
.reduce((prev, curr) => Object.assign(prev, {
[curr.quality_label || curr.type]: curr
}), {});
console.log(videoUrls);
})();
@narcotx

This comment has been minimized.

Copy link

narcotx commented Jun 14, 2016

i use (JAVA) Vget Library for Youtube Video download.But it's downloads two separete files(mp4 & webm)sometimes.Only video and only sound.How can i use this script with JSP ? Thanks...

@narcotx

This comment has been minimized.

Copy link

narcotx commented Dec 21, 2016

doesn't work;
example url;
https://www.youtube.com/watch?v=JprU54xX2aM
i can't download with url; if videourls tag contains "s:" ...

@guilhermemarconi

This comment has been minimized.

Copy link

guilhermemarconi commented Mar 16, 2017

Can I test this without the ytplayer?

I'm using React to create my own component (slider with video) and I don't think ytplayer is the best tool now, because it's too much just to get YouTube video URL.

You know some way to do that with only the video id?

@abbddo

This comment has been minimized.

Copy link

abbddo commented Jul 1, 2017

@gtkpr

This comment has been minimized.

Copy link

gtkpr commented Jul 22, 2017

this technique only provides you with separate video and audio files

@gtkpr

This comment has been minimized.

Copy link

gtkpr commented Jul 24, 2017

this script only works when the user loads a video url directly, instead of using YT internal navigation. in other words, you have to refresh the page on every video you wish to download in order to gain access to adaptive_fmts

@chay22

This comment has been minimized.

Copy link

chay22 commented Sep 14, 2017

I can't find adaptive_fmts but url_encoded_fmt_stream_map under args object and everything is working as pointed.
ytplayer.config.args.url_encoded_fmt_stream_map

@towfiqi

This comment has been minimized.

Copy link

towfiqi commented Feb 27, 2018

Doest work anymore in 2018

@Viktor19931

This comment has been minimized.

Copy link

Viktor19931 commented Mar 28, 2018

ytplayer.config.args.url_encoded_fmt_stream_map doesnt work at mobile version of youtube.

@Viktor19931

This comment has been minimized.

Copy link

Viktor19931 commented Mar 28, 2018

Does someone know how to get direct url on mobile version of youtube ?

@ViralShastri

This comment has been minimized.

Copy link

ViralShastri commented Dec 20, 2019

Uncaught TypeError: Cannot read property 'split' of undefined
at youtubedl.js:15

@geuis

This comment has been minimized.

Copy link
Owner Author

geuis commented Jan 6, 2020

@ViralShastri review the updated script.

@nysander

This comment has been minimized.

Copy link

nysander commented Jan 20, 2020

in what language is it written in? I need to rewrite it to swift

@geuis

This comment has been minimized.

Copy link
Owner Author

geuis commented Jan 20, 2020

@nysander its javascript.

@roffe

This comment has been minimized.

Copy link

roffe commented Feb 15, 2020

does not work on for example https://www.youtube.com/watch?v=IcrbM1l_BoI

VM780:13 Uncaught (in promise) TypeError: Cannot read property 'split' of undefined
at :13:6

@yair-man

This comment has been minimized.

Copy link

yair-man commented Mar 12, 2020

Where do I put this code?

@geuis

This comment has been minimized.

Copy link
Owner Author

geuis commented Mar 14, 2020

@yair-man its in the very first line

@Joshuajrodrigues

This comment has been minimized.

Copy link

Joshuajrodrigues commented Mar 30, 2020

Uncaught TypeError: Cannot read property 'split' of undefined
at :2:4

got this when i tired it out in console

@jarvis1234d

This comment has been minimized.

Copy link

jarvis1234d commented May 23, 2020

Uncaught (in promise) TypeError: Cannot read property 'split' of undefined
It shows this error everytime I run it.
I get the url of the page when by using window.location.href but I need the url which contains other properties such as screen-resolution, frame-rate, bit-rate , etc.

I also tried using ifram api by using src="https://www.youtube.com/get_video_info?video_id={videoId}" but the file which it returns gives this error status=fail&errorcode=2&reason=Invalid+parameters..

Please help if you can.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.