Skip to content

Instantly share code, notes, and snippets.

@Sollace
Created May 9, 2019 13:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sollace/140c2774b312a01e67f192bd33115a86 to your computer and use it in GitHub Desktop.
Save Sollace/140c2774b312a01e67f192bd33115a86 to your computer and use it in GitHub Desktop.
Script to extract youtube video sources
// https://www.youtube.com/watch?v=ZkZdMA5Ia7I
var stags = [];
stags.push.apply(stags, document.querySelectorAll('#player script'));
stags = stags
.map(s => s.innerText)
.filter(s => s.indexOf('url_encoded_fmt_stream_map') > -1)
.map(s => s.split('url_encoded_fmt_stream_map')[1].split('"')[2].split(','))
.map(s => s.map(decodeURIComponent).map(u => u.split("\\u0026")))
.map(s => s.map(u => {
var obj = {};
u.forEach(entry => {
const key = entry.split('=')[0];
obj[key] = entry.replace(`${key}=`, '');
});
return obj;
}));
JSON.stringify(stags, null, ' ')
/*
[
[
{
"type": "video/mp4;+codecs=\"avc1.64001F,+mp4a.40.2\"",
"url": "https://r1---sn-hxugv4pcgx-woce.googlevideo.com/videoplayback?expire=1557430278&ei=pSvUXI3RPKyNxgKbrZ3gDA&ip=196.192.186.146&id=o-AO9YiavFGknz_TUZDn8LzHMERipzRSQQF5bdU2QkUxGK&itag=22&source=youtube&requiressl=yes&mm=31%2C29&mn=sn-hxugv4pcgx-woce%2Csn-hfxc-wocl&ms=au%2Crdu&mv=m&pl=19&initcwndbps=546250&mime=video%2Fmp4&ratebypass=yes&dur=1550.883&lmt=1532910186376864&mt=1557408580&fvip=4&c=WEB&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cmime%2Cratebypass%2Cdur%2Clmt&sig=ALgxI2wwRQIgA854SE4Smkui465ttFYnmXMZOLY3Xf_cRbrkhXqjoMMCIQC1V94ZxH3kP8OEudf_0s4t_QDo0jAasZAK6RTNitF9vw%3D%3D&lsparams=mm%2Cmn%2Cms%2Cmv%2Cpl%2Cinitcwndbps&lsig=AHylml4wRAIgArD-W3iQ1u9CLG16De5A7p1XF_H_FRQWtfkmKmASrsUCID6rJ6ghbENmQQ9PzKHch_vEHVCkufawfXPekMoZTFRh",
"quality": "hd720",
"itag": "22"
},
{
"type": "video/webm;+codecs=\"vp8.0,+vorbis\"",
"url": "https://r1---sn-hxugv4pcgx-woce.googlevideo.com/videoplayback?expire=1557430278&ei=pSvUXI3RPKyNxgKbrZ3gDA&ip=196.192.186.146&id=o-AO9YiavFGknz_TUZDn8LzHMERipzRSQQF5bdU2QkUxGK&itag=43&source=youtube&requiressl=yes&mm=31%2C29&mn=sn-hxugv4pcgx-woce%2Csn-hfxc-wocl&ms=au%2Crdu&mv=m&pl=19&initcwndbps=546250&mime=video%2Fwebm&gir=yes&clen=143238550&ratebypass=yes&dur=0.000&lmt=1524321709993319&mt=1557408580&fvip=4&c=WEB&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cmime%2Cgir%2Cclen%2Cratebypass%2Cdur%2Clmt&sig=ALgxI2wwRQIgBc5QptTE6lEi30qPFOHP4VyOVVCdJXaB-KzUTGXgzIsCIQCVsJ05tLgq6Rt9adYyCtZtubzeVCFgKaVbEKSnGn1IAw%3D%3D&lsparams=mm%2Cmn%2Cms%2Cmv%2Cpl%2Cinitcwndbps&lsig=AHylml4wRAIgArD-W3iQ1u9CLG16De5A7p1XF_H_FRQWtfkmKmASrsUCID6rJ6ghbENmQQ9PzKHch_vEHVCkufawfXPekMoZTFRh",
"quality": "medium",
"itag": "43"
},
{
"type": "video/mp4;+codecs=\"avc1.42001E,+mp4a.40.2\"",
"url": "https://r1---sn-hxugv4pcgx-woce.googlevideo.com/videoplayback?expire=1557430278&ei=pSvUXI3RPKyNxgKbrZ3gDA&ip=196.192.186.146&id=o-AO9YiavFGknz_TUZDn8LzHMERipzRSQQF5bdU2QkUxGK&itag=18&source=youtube&requiressl=yes&mm=31%2C29&mn=sn-hxugv4pcgx-woce%2Csn-hfxc-wocl&ms=au%2Crdu&mv=m&pl=19&initcwndbps=546250&mime=video%2Fmp4&gir=yes&clen=96964919&ratebypass=yes&dur=1550.883&lmt=1524319451237085&mt=1557408580&fvip=4&c=WEB&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cmime%2Cgir%2Cclen%2Cratebypass%2Cdur%2Clmt&sig=ALgxI2wwRQIhAJXwnKClGK1_6CJWrLUTzg2B6Z3RnK2MTMLgV445fhxuAiB3-xaU4g3eJSYk5igJYnoHmueESlVmOhhEZ_ttM7Y7cA%3D%3D&lsparams=mm%2Cmn%2Cms%2Cmv%2Cpl%2Cinitcwndbps&lsig=AHylml4wRAIgArD-W3iQ1u9CLG16De5A7p1XF_H_FRQWtfkmKmASrsUCID6rJ6ghbENmQQ9PzKHch_vEHVCkufawfXPekMoZTFRh",
"quality": "medium",
"itag": "18"
}
]
]
*/
@putout
Copy link

putout commented Feb 22, 2023

cool script but how do i call it or use it from html ?

@Sollace
Copy link
Author

Sollace commented Feb 22, 2023

@putout This is javascript that only serves to demonstrate how you'd unpack the information from the page. You can run it directly in your browser's console from the dev tools, but to include it in any sort of project you will have to make some changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment