Skip to content

Instantly share code, notes, and snippets.

@plugnburn

plugnburn/ytdl.js

Last active Mar 17, 2021
Embed
What would you like to do?
Handmade YouT00be downloader because I don't trust any other
/**
* Yet another YouT00be downloader (requiring no third-party NodeJS modules)
* Usage: node ytdl.js [video id]
*
* @license Unlicense
*/
const vID = process.argv[2]
const qs = require('querystring')
const https = require('https')
const fs = require('fs')
function dlStr(url, cb) {
https.get(url, res => {
res.setEncoding('utf8')
let chunks = []
res.on('data', (data) => chunks.push(data))
res.on('end', () => cb(chunks.join('')))
}).end()
}
function dlFile(url, fileName, cb) {
console.log('Downloading to ' + fileName)
const fStream = fs.createWriteStream(fileName)
https.get(url, res => {
res.pipe(fStream)
fStream.on('finish', () => {
fStream.close(() => {
cb(fileName + ' downloaded successfully')
})
})
}).on('error', e => {
fs.unlink(fileName)
console.error(e.message)
if(cb) cb(e.message)
})
}
const vUrl = 'utube.com/get_video_info?html5=1&video_id=' + vID
console.log('Getting the video info')
dlStr('https://www.yo' + vUrl, str => {
let entire = qs.parse(str)
let playerResponse = JSON.parse(entire.player_response)
let vidUrl = playerResponse.streamingData.formats[0].url
let vidType = playerResponse.streamingData.formats[0].mimeType
let fExt = '.avi'
if(vidType.startsWith('video/mp4'))
fExt = '.mp4'
else if(vidType.startsWith('video/webm'))
fExt = '.webm'
else if(vidType.startsWith('video/ogg'))
fExt = '.ogv'
else if(vidType.startsWith('video/x-flv'))
fExt = '.flv'
else if(vidType.startsWith('video/3gpp'))
fExt = '.3gp'
let outName = vID + fExt
dlFile(vidUrl, outName, msg => {console.log(msg)})
})
@guilhem-bichot

This comment has been minimized.

Copy link

@guilhem-bichot guilhem-bichot commented Dec 7, 2019

Hi plugnburn! I didn't find another way to notify you, other than putting a comment here, I'm sorry. In http://831337.xyz/ there is a link for SAL-PRO, but it links back to the same page (broken link?). In Google's cache I found SAL-PRO, but that cache will expire one day. Would it be possible, please, to restore the link to SAL-PRO? This program has been very useful to me today! Thank you!

@plugnburn

This comment has been minimized.

Copy link
Owner Author

@plugnburn plugnburn commented Feb 13, 2020

Hi plugnburn! I didn't find another way to notify you, other than putting a comment here, I'm sorry. In http://831337.xyz/ there is a link for SAL-PRO, but it links back to the same page (broken link?). In Google's cache I found SAL-PRO, but that cache will expire one day. Would it be possible, please, to restore the link to SAL-PRO? This program has been very useful to me today! Thank you!

Seems to work fine right now.

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