lakshminarayana commited on
Commit
25ab9a8
·
1 Parent(s): 977b57d

added cookies

Browse files
Files changed (1) hide show
  1. app.py +51 -41
app.py CHANGED
@@ -1,54 +1,64 @@
1
  import gradio as gr
2
  import yt_dlp
3
  import os
4
- import platform
5
- from pathlib import Path
6
-
7
- # Determine the default download directory based on the user's OS
8
- def get_default_download_path():
9
- if platform.system() == 'Windows':
10
- return str(Path.home() / "Downloads") # Windows default Downloads folder
11
- elif platform.system() == 'Darwin':
12
- return str(Path.home() / "Downloads") # macOS default Downloads folder
13
- else:
14
- return str(Path.home() / "Downloads") # Linux default Downloads folder
15
-
16
- # Define the progress hook function that updates the progress bar
17
- def progress_hook(d, progress):
18
- if d['status'] == 'downloading':
19
- p = d['downloaded_bytes'] / d['total_bytes'] * 100
20
- progress.update(p) # Update Gradio progress bar with percentage
21
 
22
- # Define the download function
23
- def download_video(url, progress=gr.Progress()):
24
  try:
25
- download_dir = get_default_download_path()
26
-
27
- # Set options for yt-dlp to get the best video/audio quality and save as .mp4
28
- ydl_opts = {
29
- 'format': 'bestvideo+bestaudio/best', # Best video and audio stream combination
30
- 'outtmpl': os.path.join(download_dir, '%(title)s.%(ext)s'), # Save video in the Downloads folder
31
- 'merge_output_format': 'mp4', # Force merge output to .mp4
32
- 'progress_hooks': [lambda d: progress_hook(d, progress)], # Hook for progress updates
33
- }
34
-
35
- # Create the downloader with options
36
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
37
- info_dict = ydl.extract_info(url, download=True)
38
- video_path = os.path.join(download_dir, f"{info_dict['title']}.mp4")
39
- return video_path # Return the file path for download
 
 
 
 
 
 
 
40
 
 
 
 
 
 
 
 
 
41
  except Exception as e:
42
- return f"Error: {str(e)}"
 
 
 
 
 
43
 
44
- # Create the Gradio interface with a progress bar that updates during download
45
  iface = gr.Interface(
46
  fn=download_video,
47
- inputs=gr.Textbox(label="Enter the YouTube URL"),
48
- outputs=gr.File(label="Download Video"),
 
 
 
49
  title="YouTube Video Downloader",
50
- description="Enter the URL of the YouTube video you want to download."
 
 
 
51
  )
52
 
53
- # Launch the interface with allowed_paths parameter to include Downloads directory
54
- iface.launch(allowed_paths=["/home/dq/Downloads"]) # Allow the Downloads directory
 
 
1
  import gradio as gr
2
  import yt_dlp
3
  import os
4
+ import tempfile
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ def download_video(url, cookies, progress=gr.Progress()):
 
7
  try:
8
+ # Get the path to the cookies file if provided
9
+ cookies_path = cookies.get('name') if cookies else None
10
+
11
+ with tempfile.TemporaryDirectory() as temp_dir:
12
+ # Configure yt-dlp options
13
+ ydl_opts = {
14
+ 'format': 'bestvideo+bestaudio/best',
15
+ 'outtmpl': os.path.join(temp_dir, '%(title)s.%(ext)s'),
16
+ 'merge_output_format': 'mp4',
17
+ 'progress_hooks': [lambda d: progress_hook(d, progress)],
18
+ 'cookies': cookies_path,
19
+ 'verbose': True,
20
+ }
21
+
22
+ # Download video using yt-dlp
23
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
24
+ info_dict = ydl.extract_info(url, download=True)
25
+
26
+ # Read downloaded video file into memory
27
+ video_file = info_dict.get('filename')
28
+ if not video_file:
29
+ return "Download Failed: Could not retrieve video file"
30
 
31
+ with open(video_file, 'rb') as f:
32
+ video_bytes = f.read()
33
+
34
+ # Return the video bytes for download
35
+ return {'name': os.path.basename(video_file), 'data': video_bytes}
36
+
37
+ except yt_dlp.utils.DownloadError as e:
38
+ return f"Download Failed: {str(e)}"
39
  except Exception as e:
40
+ return f"Unexpected Error: {str(e)}"
41
+
42
+ def progress_hook(d, progress):
43
+ if d['status'] == 'downloading':
44
+ p = (d['downloaded_bytes'] / d['total_bytes']) * 100 if d['total_bytes'] else 0
45
+ progress.update(p, desc=f"Downloading ({d['speed'] or '...'} at {d['eta'] or '...'})")
46
 
47
+ # Gradio interface configuration
48
  iface = gr.Interface(
49
  fn=download_video,
50
+ inputs=[
51
+ gr.Textbox(label="YouTube Video URL"),
52
+ gr.File(label="Upload your cookies.txt file (optional)", file_types=[".txt"], optional=True),
53
+ ],
54
+ outputs=gr.File(label="Downloaded Video", type="binary", file_count="single"),
55
  title="YouTube Video Downloader",
56
+ description="Enter a YouTube video URL and upload your cookies.txt (if required) to download the video.",
57
+ examples=[
58
+ ["https://www.youtube.com/watch?v=dQw4w9WgXcQ", None],
59
+ ],
60
  )
61
 
62
+ # Launch the Gradio interface
63
+ iface.queue()
64
+ iface.launch(share=True)