Skip to content

Instantly share code, notes, and snippets.

@max-te
Created September 17, 2012 15:56
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save max-te/3738162 to your computer and use it in GitHub Desktop.
Save max-te/3738162 to your computer and use it in GitHub Desktop.
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("http://www.youtube.com/get_video_info?video_id="+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)
downloadVideo(videoInfo)
})
}).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"
http.get(url,
function(res) {
var stream = fs.createWriteStream(filename)
res.pipe(stream)
})
console.log("Downloading to "+filename);
}
function map (a,f) {
for(i=0;i<a.length;i++){
a[i]= f(a[i])
}
return a
}
@lucasmrdt
Copy link

hi guy !

Great job ! I would like to know where did you found the "http://www.youtube.com/get_video_info?video_id=" link ?

Thanks again !

@AsP3X
Copy link

AsP3X commented Mar 15, 2018

@lucasmrdt
You can find links like that in the youtube api documentation

@Shrekie
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;
    res.json(output[0]);
});

@vuelove1226
Copy link

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