Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Download Youtube-Video in node.js
var http = require('http')
var fs = require('fs')
var argv = require('optimist').argv
var rxVideoID = /v=([\]\[!"#$%'()*+,.\/:;<=>?@\^_`{|}~-\w]*)/
var link = argv._.toString()
var videoID = link.match(rxVideoID)[1]
http.get(""+videoID, function(res) {
var chunks = []
res.on('data', function(chunk){chunks.push(chunk)
}).on('end', function(){
var data = Buffer.concat(chunks).toString()
var videoInfo = parseVideoInfo(data)
}).on('error', function(e) {
console.log("Got error: " + e.message)
function parseVideoInfo(videoInfo) {
var rxUrlMap = /url_encoded_fmt_stream_map=([\]\[!"#$%'()*+,.\/:;<=>?@\^_`{|}~-\w]*)/
urlmap = unescape(videoInfo.match(rxUrlMap)[1])
var rxUrlG = /url=([\]\[!"#$%'()*+,.\/:;<=>?@\^_`{|}~-\w]*)/g
var rxUrl = /url=([\]\[!"#$%'()*+,.\/:;<=>?@\^_`{|}~-\w]*)/
var urls = urlmap.match(rxUrlG)
urls = map(urls, function(s) {return s.match(rxUrl)[1]} )
urls = map(urls, unescape)
var rxTitle = /title=([\]\[!"#$%'()*+,.\/:;<=>?@\^_`{|}~-\w]*)/
var title = argv.o ? argv.o : videoInfo.match(rxTitle)[1]
return { title: title, urls: urls }
function downloadVideo(videoInfo) {
var url = videoInfo.urls[0];
var filename = videoInfo.title + ".flv"
function(res) {
var stream = fs.createWriteStream(filename)
console.log("Downloading to "+filename);
function map (a,f) {
a[i]= f(a[i])
return a

This comment has been minimized.

Copy link

lucasmrdt commented Jan 5, 2018

hi guy !

Great job ! I would like to know where did you found the "" link ?

Thanks again !


This comment has been minimized.

Copy link

ZombyMedia commented Mar 15, 2018

You can find links like that in the youtube api documentation


This comment has been minimized.

Copy link

Shrekie commented Apr 19, 2018

youtube-dl has some overhead but i cant get this to work consistently without it.
Please if someone has code for node to grab the stream URL please help out, the problem is mostly the fact that some youtube videos require you to update a signature as well which is another request and the documentation on this is scarce. You can try to dig through source code of downloaders but I'm having a hard time figuring it out.

const ytdl = require('youtube-dl');

var link = req.body.YTURL;

ytdl.exec(link, ['-f best', '-s', '-g'], {}, function(err, output) {
    if (err) throw err;

This comment has been minimized.

Copy link

vuelove1226 commented Apr 5, 2019

i want school management system.
teachers have to login and download video from youtube capturing url link.
student learn via video.
could you help me?

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.