Last active
July 5, 2018 12:15
-
-
Save rigwild/ec4ab35af99122590200025bc0739230 to your computer and use it in GitHub Desktop.
Get MP4/OGG download links from Youtube videos. A little ytdl-core server implementation (https://github.com/fent/node-ytdl-core).
This file contains 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
const http = require('http'); | |
const ytdl = require('ytdl-core'); | |
const getInfo = async youtubeUrl => { | |
const data = await ytdl.getInfo(youtubeUrl); | |
let result = { | |
id: data.video_id, | |
url: data.video_url, | |
title: data.title, | |
description: data.description, | |
author: data.author, | |
thumbnail: data.thumbnail_url, | |
formats: data.formats | |
} | |
return JSON.stringify(result); | |
} | |
http.createServer((req, res) => { | |
try { | |
res.writeHead(200, { | |
'Content-Type': 'application/json', | |
'Access-Control-Allow-Origin': '*' | |
}); | |
const sentData = req.url.substring(1); | |
console.log("Received : ", sentData) | |
if (sentData.match(/(?:youtube\.com\/(?:watch\?v=|embed\/)|youtu.be\/).+/)) { | |
getInfo(sentData) | |
.then(result => { | |
console.log("Valid youtube video ID :", sentData); | |
res.write(result); | |
res.end(); | |
}) | |
.catch(err => { | |
console.error("Invalid youtube video ID :", sentData); | |
res.write("{error: 'Invalid youtube video ID'}"); | |
res.end(); | |
}); | |
} | |
else { | |
res.write("{error: 'The provided URL is invalid'}"); | |
res.end(); | |
} | |
} | |
catch (e) { | |
console.error(e); | |
res.writeHead(500, { | |
'Access-Control-Allow-Origin': '*' | |
}); | |
res.end(); | |
} | |
}).listen(8080); | |
console.log("Server is listening on 127.0.0.1:8080"); | |
/* | |
# ytdl-core-server | |
Get MP4/OGG download links from Youtube videos. A little ytdl-core server implementation (https://github.com/fent/node-ytdl-core). | |
## Install | |
$ mkdir ytdl-core-server && cd ytdl-core-server && npm init -y && npm i ytdl-core | |
Download the ytdl-core-server.js file in this directory. | |
## Start | |
$ node ytdl-core-server.js | |
## Usage | |
The server will start on 127.0.0.1:8080. You simply need to to add your Youtube URL at the end. | |
http://127.0.0.1:8080/https://www.youtube.com/watch?v=KMU0tzLwhbE | |
http://127.0.0.1:8080/https://www.youtube.com/embed/KMU0tzLwhbE | |
http://127.0.0.1:8080/https://youtu.be/KMU0tzLwhbE | |
## License | |
The MIT License | |
Copyright (c) 2018 < rigwild >. https://asauvage.fr/ | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment