Created
May 29, 2020 03:18
-
-
Save ReeganExE/91cf3fc75e8d290224c579c6436ac3a4 to your computer and use it in GitHub Desktop.
Simple youtube mp3
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
require('express-async-errors') | |
const http = require('http') | |
const bodyParser = require('body-parser') | |
const errorhandler = require('errorhandler') | |
const app = require('express')() | |
app.use(bodyParser.urlencoded({ extended: false })).use(bodyParser.json()) | |
app.set('trust proxy', true) | |
app.get('/watch', (req, res) => { | |
const ytid = req.query.v | |
const link = `https://www.youtube.com/watch?v=${ytid}` | |
const update = require('child_process').spawn('youtube-dl', [ | |
'-g', | |
'-f', | |
'bestaudio', | |
'--extract-audio', | |
'--audio-format', | |
'mp3', | |
'--audio-quality', | |
'0', | |
link | |
]) | |
const errorArr = [] | |
const rs = [] | |
update.stderr.on('data', (data) => errorArr.push(data)) | |
update.stdout.on('data', (data) => rs.push(data)) | |
update.on('exit', (code) => { | |
const stderr = errorArr.join('') | |
if (code) { | |
console.error(stderr) | |
} | |
res.header('content-type', 'text/plain') | |
if (code) { | |
res.status(500).send(stderr) | |
} else { | |
res.redirect(rs.join('')) | |
} | |
}) | |
}) | |
app.use(errorhandler()) | |
const port = process.env.PORT || 3000 | |
const server = http.createServer(app).listen(port, () => { | |
console.log(`Listening on port http://localhost:${server.address().port}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment