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

commit a507d16843ab39a08adf3ae18348a6f1db464e40
parent 42d41bb5c44b37592caaaef202eda4fa2907047e
Author: night0721 <[email protected]>
Date:   Sat,  3 Feb 2024 14:14:20 +0000

add server code

Diffstat:
Aserver/.gitignore | 4++++
Aserver/package.json | 20++++++++++++++++++++
Aserver/src/download.ts | 24++++++++++++++++++++++++
Aserver/src/index.ts | 17+++++++++++++++++
Aserver/tsconfig.json | 11+++++++++++
5 files changed, 76 insertions(+), 0 deletions(-)

diff --git a/server/.gitignore b/server/.gitignore @@ -0,0 +1,3 @@ +package-lock.json +node_modules +dist +\ No newline at end of file diff --git a/server/package.json b/server/package.json @@ -0,0 +1,20 @@ +{ + "name": "service-api", + "version": "1.0.0", + "main": "index.js", + "license": "MIT", + "devDependencies": { + "@types/cors": "^2.8.12", + "@types/express": "^4.17.17", + "typescript": "^4.5.5" + }, + "dependencies": { + "cors": "^2.8.5", + "express": "^4.18.2", + "ytdl-core": "npm:@distube/ytdl-core@^4.11.9" + }, + "scripts": { + "build": "tsc", + "start": "node ./dist/index.js" + } +} diff --git a/server/src/download.ts b/server/src/download.ts @@ -0,0 +1,24 @@ +import { Router } from "express"; +const ytdl = require("ytdl-core"); + +const router = Router(); + +router.post("/download", async (req, res) => { + try { + const url = req.body.url; + const type = req.body.type; + if (!ytdl.validateURL(url)) + return res.status(400).json({ message: "Invalid URL", code: 400 }); + if (type !== "mp3" && type !== "mp4") + return res.status(400).json({ message: "Invalid type", code: 400 }); + res.setHeader("Content-Type", type === "mp3" ? "audio/mpeg" : "video/mp4"); + ytdl(url, { + format: type, + filter: type == "mp4" ? "videoandaudio" : "audioonly", + }).pipe(res); + } catch (e) { + console.log(e); + } +}); + +module.exports = router; diff --git a/server/src/index.ts b/server/src/index.ts @@ -0,0 +1,17 @@ +import express from "express"; +import cors from "cors"; + +const app = express(); +app.use(express.json()); +app.use(express.urlencoded({ extended: true })); +app.use(cors()); + +app.use("/api", require("./download")); + +app.listen(852, () => { + console.log("Server running on port 852"); +}); + +process.on("uncaughtException", err => { + console.log(err); +}); diff --git a/server/tsconfig.json b/server/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "target": "es5", + "module": "commonjs", + "outDir": "./dist", + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true + } +}