Last active
February 16, 2022 13:15
-
-
Save iamtakagi/80719c307bcbe4505d8a2b1628ec72ed to your computer and use it in GitHub Desktop.
node-ytdl-core-mp4-pipe-streaming-server
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
import ytdl from "ytdl-core"; | |
import express from "express"; | |
const app = express() | |
app.get("/stream", async (req, res, next) => { | |
const url = req.query.url | |
const quality = req.query.quality; | |
if (!url || typeof url != "string") return next(); | |
if (!quality || typeof quality != "string") return next(); | |
const info = await ytdl.getInfo(url); | |
const videoFormat = ytdl.chooseFormat(info.formats, { quality: String(quality) }) | |
res.contentType("video/mp4") | |
res.attachment(`${info.videoDetails.title}.mp4`) | |
ytdl(url, {quality, format: videoFormat}).pipe(res); | |
}); | |
app.listen(process.env.PORT || 3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment