tebicap commited on
Commit
8d7e89a
·
verified ·
1 Parent(s): 098faff

subo chu fails

Browse files
Files changed (2) hide show
  1. package.json +2 -1
  2. server.js +77 -26
package.json CHANGED
@@ -8,7 +8,8 @@
8
  "dependencies": {
9
  "axios": "1.4.0",
10
  "express": "4.18.2",
11
- "ytdl-core": "4.11.5"
 
12
  }
13
  }
14
 
 
8
  "dependencies": {
9
  "axios": "1.4.0",
10
  "express": "4.18.2",
11
+ "ytdl-core": "4.11.5",
12
+ "ytdl-core-discord": "1.3.1"
13
  }
14
  }
15
 
server.js CHANGED
@@ -1,24 +1,20 @@
1
  const express = require("express")
2
  const path = require("path")
3
  const ytdl = require("ytdl-core")
 
4
  const axios = require("axios")
5
 
6
  const app = express()
7
- const port = process.env.PORT || 7860 // Hugging Face uses port 7860 by default
8
 
9
  app.use(express.static(path.join(__dirname, "public")))
10
 
11
- app.get("/api/info", async (req, res) => {
12
- const { url } = req.query
13
-
14
- if (!url) {
15
- return res.status(400).json({ error: "URL no proporcionada" })
16
- }
17
-
18
  try {
19
- if (url.includes("youtube.com") || url.includes("youtu.be")) {
 
20
  const info = await ytdl.getInfo(url)
21
- res.json({
22
  platform: "YouTube",
23
  title: info.videoDetails.title,
24
  formats: [
@@ -29,7 +25,40 @@ app.get("/api/info", async (req, res) => {
29
  },
30
  { quality: "Audio (MP3)", extension: "mp3", url: `/api/download?url=${encodeURIComponent(url)}&format=mp3` },
31
  ],
32
- })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  } else if (url.includes("facebook.com")) {
34
  res.json({
35
  platform: "Facebook",
@@ -47,12 +76,12 @@ app.get("/api/info", async (req, res) => {
47
  }
48
  } catch (error) {
49
  console.error("Error processing URL:", error)
50
- res.status(500).json({ error: "Error al procesar la URL" })
51
  }
52
  })
53
 
54
  app.get("/api/download", async (req, res) => {
55
- const { url, format } = req.query
56
 
57
  if (!url) {
58
  return res.status(400).json({ error: "URL no proporcionada" })
@@ -60,22 +89,44 @@ app.get("/api/download", async (req, res) => {
60
 
61
  try {
62
  if (url.includes("youtube.com") || url.includes("youtu.be")) {
63
- const info = await ytdl.getInfo(url)
64
- const format_id = format === "mp3" ? "audioonly" : "highest"
65
- const format_info = ytdl.chooseFormat(info.formats, { quality: format_id })
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
- res.header(
68
- "Content-Disposition",
69
- `attachment; filename="${info.videoDetails.title}.${format === "mp3" ? "mp3" : "mp4"}"`,
70
- )
71
- res.header("Content-Type", format === "mp3" ? "audio/mpeg" : "video/mp4")
72
 
73
- ytdl(url, { format: format_info }).pipe(res)
 
 
 
 
 
 
74
  } else {
75
- const response = await axios.get(url, { responseType: "stream" })
76
- res.header("Content-Type", response.headers["content-type"])
77
- res.header("Content-Disposition", response.headers["content-disposition"] || "attachment")
78
- response.data.pipe(res)
 
 
 
 
 
79
  }
80
  } catch (error) {
81
  console.error("Error downloading video:", error)
 
1
  const express = require("express")
2
  const path = require("path")
3
  const ytdl = require("ytdl-core")
4
+ const ytdlDiscord = require("ytdl-core-discord")
5
  const axios = require("axios")
6
 
7
  const app = express()
8
+ const port = process.env.PORT || 7860
9
 
10
  app.use(express.static(path.join(__dirname, "public")))
11
 
12
+ async function getYoutubeInfo(url) {
 
 
 
 
 
 
13
  try {
14
+ // First attempt with regular ytdl-core
15
+ try {
16
  const info = await ytdl.getInfo(url)
17
+ return {
18
  platform: "YouTube",
19
  title: info.videoDetails.title,
20
  formats: [
 
25
  },
26
  { quality: "Audio (MP3)", extension: "mp3", url: `/api/download?url=${encodeURIComponent(url)}&format=mp3` },
27
  ],
28
+ }
29
+ } catch (error) {
30
+ // If regular ytdl fails, try ytdl-discord as fallback
31
+ console.log("Attempting fallback method...")
32
+ await ytdlDiscord(url) // This will throw if the video is completely unavailable
33
+ return {
34
+ platform: "YouTube",
35
+ title: "Video de YouTube",
36
+ formats: [
37
+ {
38
+ quality: "Audio (MP3)",
39
+ extension: "mp3",
40
+ url: `/api/download?url=${encodeURIComponent(url)}&format=mp3&fallback=true`,
41
+ },
42
+ ],
43
+ }
44
+ }
45
+ } catch (error) {
46
+ console.error("YouTube processing error:", error)
47
+ throw new Error("Este video no está disponible o está restringido por YouTube.")
48
+ }
49
+ }
50
+
51
+ app.get("/api/info", async (req, res) => {
52
+ const { url } = req.query
53
+
54
+ if (!url) {
55
+ return res.status(400).json({ error: "URL no proporcionada" })
56
+ }
57
+
58
+ try {
59
+ if (url.includes("youtube.com") || url.includes("youtu.be")) {
60
+ const info = await getYoutubeInfo(url)
61
+ res.json(info)
62
  } else if (url.includes("facebook.com")) {
63
  res.json({
64
  platform: "Facebook",
 
76
  }
77
  } catch (error) {
78
  console.error("Error processing URL:", error)
79
+ res.status(500).json({ error: error.message || "Error al procesar la URL" })
80
  }
81
  })
82
 
83
  app.get("/api/download", async (req, res) => {
84
+ const { url, format, fallback } = req.query
85
 
86
  if (!url) {
87
  return res.status(400).json({ error: "URL no proporcionada" })
 
89
 
90
  try {
91
  if (url.includes("youtube.com") || url.includes("youtu.be")) {
92
+ let stream
93
+ let contentType
94
+ let filename
95
+
96
+ try {
97
+ if (fallback === "true") {
98
+ // Use ytdl-discord for fallback
99
+ stream = await ytdlDiscord(url, { filter: "audioonly" })
100
+ contentType = "audio/mpeg"
101
+ filename = "audio.mp3"
102
+ } else {
103
+ // Use regular ytdl-core
104
+ const info = await ytdl.getInfo(url)
105
+ const format_id = format === "mp3" ? "audioonly" : "highest"
106
+ const format_info = ytdl.chooseFormat(info.formats, { quality: format_id })
107
 
108
+ stream = ytdl(url, { format: format_info })
109
+ contentType = format === "mp3" ? "audio/mpeg" : "video/mp4"
110
+ filename = `${info.videoDetails.title}.${format === "mp3" ? "mp3" : "mp4"}`
111
+ }
 
112
 
113
+ res.header("Content-Disposition", `attachment; filename="${filename}"`)
114
+ res.header("Content-Type", contentType)
115
+ stream.pipe(res)
116
+ } catch (error) {
117
+ console.error("YouTube download error:", error)
118
+ res.status(500).json({ error: "Error al descargar el video de YouTube. El video puede estar restringido." })
119
+ }
120
  } else {
121
+ try {
122
+ const response = await axios.get(url, { responseType: "stream" })
123
+ res.header("Content-Type", response.headers["content-type"])
124
+ res.header("Content-Disposition", response.headers["content-disposition"] || "attachment")
125
+ response.data.pipe(res)
126
+ } catch (error) {
127
+ console.error("Download error:", error)
128
+ res.status(500).json({ error: "Error al descargar el video" })
129
+ }
130
  }
131
  } catch (error) {
132
  console.error("Error downloading video:", error)