Repository: Moorad/youtubeDownloader Branch: master Commit: d66318bd6eec Files: 8 Total size: 5.3 KB Directory structure: gitextract_zxsqpt5b/ ├── .gitignore ├── Contributors.md ├── README.md ├── Server/ │ ├── index.js │ └── package.json ├── index.html ├── script.js └── style.css ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ node_modules ================================================ FILE: Contributors.md ================================================ Balaji S github URL: https://github.com/balajinikhil ================================================ FILE: README.md ================================================ # Youtube Downloader This is a repository that has sample code for my [Medium Article](https://blog.usejournal.com/how-i-made-my-own-youtube-downloader-using-javascript-and-node-js-160b172f6e10) ## Getting Started If there is any issues please open a new issue. 1. You need to clone this repository ``` git clone https://github.com/mooradal/youtubeDownloader ``` 2. After you clone the repo you will have to navigate to the Server folder ``` cd Server ``` 3. Then you will have to install all the packages and dependencies ``` npm install ``` 4. Finally you need to run it ``` node index.js ``` 5. If you want to use nodemon (nodemon is a package that will auto restart the server when files are changed) you can run **(Optional)** ``` npm run dev ``` or ``` nodemon index.js ``` ## Info If there is any issues please open a new issue. You are welcome to add pull requests at anytime Thank you so much for supporting me and thank you for almost 2,000 claps. I really appreciate that. I will try to post more articles and I'm thinking of turning this project from a sample code to an actual functional public website for everyone to use! ================================================ FILE: Server/index.js ================================================ const express = require('express'); const cors = require('cors'); const ytdl = require('ytdl-core'); const app = express(); const PORT = 4000; app.use(cors()); app.listen(PORT, () => { console.log(`Server Works !!! At port ${PORT}`); }); app.get('/downloadmp3', async (req, res, next) => { try { var url = req.query.url; if(!ytdl.validateURL(url)) { return res.sendStatus(400); } let title = 'audio'; await ytdl.getBasicInfo(url, { format: 'mp4' }, (err, info) => { if (err) throw err; title = info.player_response.videoDetails.title.replace(/[^\x00-\x7F]/g, ""); }); res.header('Content-Disposition', `attachment; filename="${title}.mp3"`); ytdl(url, { format: 'mp3', filter: 'audioonly', }).pipe(res); } catch (err) { console.error(err); } }); app.get('/downloadmp4', async (req, res, next) => { try { let url = req.query.url; if(!ytdl.validateURL(url)) { return res.sendStatus(400); } let title = 'video'; await ytdl.getBasicInfo(url, { format: 'mp4' }, (err, info) => { title = info.player_response.videoDetails.title.replace(/[^\x00-\x7F]/g, ""); }); res.header('Content-Disposition', `attachment; filename="${title}.mp4"`); ytdl(url, { format: 'mp4', }).pipe(res); } catch (err) { console.error(err); } }); ================================================ FILE: Server/package.json ================================================ { "name": "server", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "dev": "nodemon index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "cors": "^2.8.5", "express": "^4.16.4", "ytdl-core": "^3.1.2" }, "devDependencies": { "nodemon": "^1.18.10" } } ================================================ FILE: index.html ================================================