taslim19 commited on
Commit
280e0cf
·
1 Parent(s): 8d58bbb

fix: download direct video URLs with httpx if not YouTube, avoid yt-dlp format errors

Browse files
Files changed (1) hide show
  1. DragMusic/platforms/Youtube.py +21 -3
DragMusic/platforms/Youtube.py CHANGED
@@ -261,6 +261,15 @@ class YouTubeAPI:
261
  def audio_dl(bitflow_info):
262
  temp_dir = tempfile.gettempdir()
263
  xyz = os.path.join(temp_dir, f"{bitflow_info['videoid']}.{bitflow_info['ext']}")
 
 
 
 
 
 
 
 
 
264
  ydl_optssx = {
265
  "format": "bestaudio/best",
266
  "outtmpl": xyz,
@@ -269,14 +278,23 @@ class YouTubeAPI:
269
  "quiet": True,
270
  "no_warnings": True,
271
  }
272
- x = yt_dlp.YoutubeDL(ydl_optssx)
273
  if os.path.exists(xyz):
274
  return xyz
275
- x.download([bitflow_info['url']])
276
  return xyz
277
  def video_dl(bitflow_info):
278
  temp_dir = tempfile.gettempdir()
279
  xyz = os.path.join(temp_dir, f"{bitflow_info['videoid']}.{bitflow_info['ext']}")
 
 
 
 
 
 
 
 
 
280
  ydl_optssx = {
281
  "format": "(bestvideo[height<=?720][width<=?1280][ext=mp4])+(bestaudio[ext=m4a])",
282
  "outtmpl": xyz,
@@ -288,7 +306,7 @@ class YouTubeAPI:
288
  x = yt_dlp.YoutubeDL(ydl_optssx)
289
  if os.path.exists(xyz):
290
  return xyz
291
- x.download([bitflow_info['url']])
292
  return xyz
293
  def song_video_dl():
294
  temp_dir = tempfile.gettempdir()
 
261
  def audio_dl(bitflow_info):
262
  temp_dir = tempfile.gettempdir()
263
  xyz = os.path.join(temp_dir, f"{bitflow_info['videoid']}.{bitflow_info['ext']}")
264
+ url = bitflow_info['url']
265
+ # If the url is not a YouTube link, download directly
266
+ if not (url.startswith('http') and ('youtube.com' in url or 'youtu.be' in url)):
267
+ import httpx
268
+ with httpx.Client() as client:
269
+ r = client.get(url)
270
+ with open(xyz, "wb") as f:
271
+ f.write(r.content)
272
+ return xyz
273
  ydl_optssx = {
274
  "format": "bestaudio/best",
275
  "outtmpl": xyz,
 
278
  "quiet": True,
279
  "no_warnings": True,
280
  }
281
+ x = yt_dlp.YoutubeDL(ydl_optssx)
282
  if os.path.exists(xyz):
283
  return xyz
284
+ x.download([url])
285
  return xyz
286
  def video_dl(bitflow_info):
287
  temp_dir = tempfile.gettempdir()
288
  xyz = os.path.join(temp_dir, f"{bitflow_info['videoid']}.{bitflow_info['ext']}")
289
+ url = bitflow_info['url']
290
+ # If the url is not a YouTube link, download directly
291
+ if not (url.startswith('http') and ('youtube.com' in url or 'youtu.be' in url)):
292
+ import httpx
293
+ with httpx.Client() as client:
294
+ r = client.get(url)
295
+ with open(xyz, "wb") as f:
296
+ f.write(r.content)
297
+ return xyz
298
  ydl_optssx = {
299
  "format": "(bestvideo[height<=?720][width<=?1280][ext=mp4])+(bestaudio[ext=m4a])",
300
  "outtmpl": xyz,
 
306
  x = yt_dlp.YoutubeDL(ydl_optssx)
307
  if os.path.exists(xyz):
308
  return xyz
309
+ x.download([url])
310
  return xyz
311
  def song_video_dl():
312
  temp_dir = tempfile.gettempdir()