Skip to content

Instantly share code, notes, and snippets.

@iamtakagi
Last active February 16, 2022 13:15
Show Gist options
  • Save iamtakagi/80719c307bcbe4505d8a2b1628ec72ed to your computer and use it in GitHub Desktop.
Save iamtakagi/80719c307bcbe4505d8a2b1628ec72ed to your computer and use it in GitHub Desktop.
node-ytdl-core-mp4-pipe-streaming-server
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