Last active
September 12, 2020 20:41
-
-
Save emwadde/e9b5499da2efbf8d0dcd73833dd070eb to your computer and use it in GitHub Desktop.
Get YouTube vide direct links in Nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Call this in console and pass YT video url as first argument | |
* e.g.: node yt-links.js https://www.youtube.com/watch?v=axBuiB55CfA | |
* Direct links can be extracted at line 24 | |
*/ | |
const https = require('https'); | |
var url = process.argv.slice(2)[0]; | |
var url = new URL(url); | |
var searchParams = new URLSearchParams(url.search); | |
if(searchParams.has('v')){ | |
let vid = searchParams.get('v'); | |
let vid_info_url = `https://www.youtube.com/get_video_info?video_id=${vid}`; | |
https.get(vid_info_url, (resp) => { | |
let data; | |
resp.on('data', (chunk) => { | |
data += chunk; | |
}); | |
resp.on('end', () => { | |
let decoded = decodeURIComponent(data); | |
let json = decoded.match(/({.+})/)[1] | |
let links = JSON.parse(json).streamingData; | |
console.log(links) | |
}); | |
}).on("error", (err) => { | |
console.log("Error: " + err.message); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Same thing, but using
node-fetch
instead ofhttps
. This seems much faster.