developer28 commited on
Commit
d7688de
Β·
verified Β·
1 Parent(s): 6cf3494

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -7
app.py CHANGED
@@ -18,7 +18,7 @@ class YouTubeDownloader:
18
  )
19
  return youtube_regex.match(url) is not None
20
 
21
- def get_video_info(self, url, progress=gr.Progress()):
22
  """Get detailed video information without downloading"""
23
  if not url or not url.strip():
24
  return None, "❌ Please enter a YouTube URL"
@@ -34,6 +34,8 @@ class YouTubeDownloader:
34
  'noplaylist': True,
35
  'extract_flat': False,
36
  }
 
 
37
 
38
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
39
  try:
@@ -73,7 +75,7 @@ class YouTubeDownloader:
73
  except Exception as e:
74
  return None, f"❌ An unexpected error occurred: {str(e)}"
75
 
76
- def download_video(self, url, progress=gr.Progress()):
77
  """Download YouTube video using yt-dlp"""
78
  if not url or not url.strip():
79
  return None, "❌ Please enter a YouTube URL"
@@ -90,7 +92,9 @@ class YouTubeDownloader:
90
  'outtmpl': os.path.join(self.download_dir, '%(title)s.%(ext)s'),
91
  'noplaylist': True, # Download only single video, not playlist
92
  }
93
-
 
 
94
  progress(0.3, desc="Fetching video information...")
95
 
96
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
@@ -269,6 +273,13 @@ def create_interface():
269
  lines=1,
270
  max_lines=1
271
  )
 
 
 
 
 
 
 
272
 
273
  download_btn = gr.Button(
274
  "Download Video",
@@ -307,12 +318,15 @@ def create_interface():
307
  def handle_download(url):
308
  if not url or not url.strip():
309
  return "Please enter a YouTube URL", gr.File(visible=False), gr.Button(visible=False), None
 
 
 
310
 
311
  # First get video info and store it
312
- video_info, info_message = downloader.get_video_info(url)
313
 
314
  # Then download the video
315
- file_path, download_message = downloader.download_video(url)
316
 
317
  if file_path and video_info:
318
  success_message = f"{download_message}\n\nVideo downloaded successfully! Click 'Show Analysis Results' to see detailed information."
@@ -347,7 +361,7 @@ def create_interface():
347
  # Button click events
348
  download_btn.click(
349
  fn=handle_download,
350
- inputs=[url_input],
351
  outputs=[status_output, file_output, analysis_btn, video_info_state],
352
  show_progress="full"
353
  )
@@ -361,7 +375,7 @@ def create_interface():
361
  # Allow Enter key to trigger download
362
  url_input.submit(
363
  fn=handle_download,
364
- inputs=[url_input],
365
  outputs=[status_output, file_output, analysis_btn, video_info_state],
366
  show_progress="full"
367
  )
 
18
  )
19
  return youtube_regex.match(url) is not None
20
 
21
+ def get_video_info(self, url, progress=gr.Progress(),cookiefile=None)):
22
  """Get detailed video information without downloading"""
23
  if not url or not url.strip():
24
  return None, "❌ Please enter a YouTube URL"
 
34
  'noplaylist': True,
35
  'extract_flat': False,
36
  }
37
+ if cookiefile:
38
+ ydl_opts['cookiefile'] = cookiefile
39
 
40
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
41
  try:
 
75
  except Exception as e:
76
  return None, f"❌ An unexpected error occurred: {str(e)}"
77
 
78
+ def download_video(self, url, progress=gr.Progress(),cookiefile=None):
79
  """Download YouTube video using yt-dlp"""
80
  if not url or not url.strip():
81
  return None, "❌ Please enter a YouTube URL"
 
92
  'outtmpl': os.path.join(self.download_dir, '%(title)s.%(ext)s'),
93
  'noplaylist': True, # Download only single video, not playlist
94
  }
95
+ if cookiefile:
96
+ ydl_opts['cookiefile'] = cookiefile
97
+
98
  progress(0.3, desc="Fetching video information...")
99
 
100
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
 
273
  lines=1,
274
  max_lines=1
275
  )
276
+
277
+ cookies_input = gr.File(
278
+ label="Upload cookies.txt (optional for age-restricted/protected videos)",
279
+ type="filepath",
280
+ file_types=[".txt"],
281
+ visible=True
282
+ )
283
 
284
  download_btn = gr.Button(
285
  "Download Video",
 
318
  def handle_download(url):
319
  if not url or not url.strip():
320
  return "Please enter a YouTube URL", gr.File(visible=False), gr.Button(visible=False), None
321
+
322
+ # Set cookies path if provided
323
+ cookiefile = cookies_path if cookies_path and os.path.exists(cookies_path) else None
324
 
325
  # First get video info and store it
326
+ video_info, info_message = downloader.get_video_info(url,cookiefile=cookiefile)
327
 
328
  # Then download the video
329
+ file_path, download_message = downloader.download_video(url,cookiefile=cookiefile)
330
 
331
  if file_path and video_info:
332
  success_message = f"{download_message}\n\nVideo downloaded successfully! Click 'Show Analysis Results' to see detailed information."
 
361
  # Button click events
362
  download_btn.click(
363
  fn=handle_download,
364
+ inputs=[url_input,cookies_input],
365
  outputs=[status_output, file_output, analysis_btn, video_info_state],
366
  show_progress="full"
367
  )
 
375
  # Allow Enter key to trigger download
376
  url_input.submit(
377
  fn=handle_download,
378
+ inputs=[url_input,cookies_input],
379
  outputs=[status_output, file_output, analysis_btn, video_info_state],
380
  show_progress="full"
381
  )