FarelDeveloper commited on
Commit
3944a6b
·
verified ·
1 Parent(s): 25d6424

Create server.js

Browse files
Files changed (1) hide show
  1. server.js +66 -0
server.js ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import express from "express";
2
+ import cors from "cors";
3
+ import ytdl from "ytdl-core";
4
+
5
+ const app = express();
6
+ app.use(cors());
7
+
8
+ // Homepage
9
+ app.get("/", (req, res) => {
10
+ res.send("YT Downloader API (MP3 & MP4) — by Muhammad Farel");
11
+ });
12
+
13
+ // MP4 Downloader
14
+ app.get("/mp4", async (req, res) => {
15
+ try {
16
+ const url = req.query.url;
17
+ if (!url) return res.json({ error: "Masukkan parameter ?url=" });
18
+
19
+ if (!ytdl.validateURL(url)) return res.json({ error: "URL YouTube tidak valid" });
20
+
21
+ const info = await ytdl.getInfo(url);
22
+ const format = ytdl.chooseFormat(info.formats, {
23
+ quality: "highestvideo"
24
+ });
25
+
26
+ res.json({
27
+ type: "mp4",
28
+ title: info.videoDetails.title,
29
+ thumbnail: info.videoDetails.thumbnails.pop().url,
30
+ download: format.url
31
+ });
32
+
33
+ } catch (err) {
34
+ res.json({ error: "Gagal mengambil link", detail: err.message });
35
+ }
36
+ });
37
+
38
+ // MP3 Downloader (audio-only)
39
+ app.get("/mp3", async (req, res) => {
40
+ try {
41
+ const url = req.query.url;
42
+ if (!url) return res.json({ error: "Masukkan parameter ?url=" });
43
+
44
+ if (!ytdl.validateURL(url)) return res.json({ error: "URL YouTube tidak valid" });
45
+
46
+ const info = await ytdl.getInfo(url);
47
+
48
+ const audio = ytdl.chooseFormat(info.formats, {
49
+ filter: "audioonly",
50
+ quality: "highestaudio"
51
+ });
52
+
53
+ res.json({
54
+ type: "mp3",
55
+ title: info.videoDetails.title,
56
+ thumbnail: info.videoDetails.thumbnails.pop().url,
57
+ download: audio.url
58
+ });
59
+
60
+ } catch (err) {
61
+ res.json({ error: "Gagal mengambil link", detail: err.message });
62
+ }
63
+ });
64
+
65
+ const PORT = 7860;
66
+ app.listen(PORT, () => console.log("Server berjalan di port " + PORT));