Skip to content

Instantly share code, notes, and snippets.

@ReeganExE
Created May 29, 2020 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ReeganExE/91cf3fc75e8d290224c579c6436ac3a4 to your computer and use it in GitHub Desktop.
Save ReeganExE/91cf3fc75e8d290224c579c6436ac3a4 to your computer and use it in GitHub Desktop.
Simple youtube mp3
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