youtube-downloader

Simple Next.js website and interface to download YouTube videos to mp3 or mp4 files with URLs
git clone https://codeberg.org/night0721/youtube-downloader
Log | Files | Refs | README | LICENSE

download.ts (701B)


      1 import { Router } from "express";
      2 const ytdl = require("ytdl-core");
      3 
      4 const router = Router();
      5 
      6 router.post("/download", async (req, res) => {
      7   try {
      8     const url = req.body.url;
      9     const type = req.body.type;
     10     if (!ytdl.validateURL(url))
     11       return res.status(400).json({ message: "Invalid URL", code: 400 });
     12     if (type !== "mp3" && type !== "mp4")
     13       return res.status(400).json({ message: "Invalid type", code: 400 });
     14     res.setHeader("Content-Type", type === "mp3" ? "audio/mpeg" : "video/mp4");
     15     ytdl(url, {
     16       format: type,
     17       filter: type == "mp4" ? "videoandaudio" : "audioonly",
     18     }).pipe(res);
     19   } catch (e) {
     20     console.log(e);
     21   }
     22 });
     23 
     24 module.exports = router;