lakshminarayana commited on
Commit
977b57d
·
1 Parent(s): 54c1b7b

ADDED PROGRESS

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +29 -13
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ venv
app.py CHANGED
@@ -1,31 +1,47 @@
1
  import gradio as gr
2
  import yt_dlp
3
  import os
 
 
4
 
5
- def download_video(url):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  try:
 
 
7
  # Set options for yt-dlp to get the best video/audio quality and save as .mp4
8
  ydl_opts = {
9
  'format': 'bestvideo+bestaudio/best', # Best video and audio stream combination
10
- 'outtmpl': 'downloads/%(title)s.%(ext)s', # Save the video in the 'downloads' folder
11
  'merge_output_format': 'mp4', # Force merge output to .mp4
 
12
  }
13
 
14
- # Create the downloads directory if it doesn't exist
15
- if not os.path.exists('downloads'):
16
- os.makedirs('downloads')
17
-
18
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
19
  info_dict = ydl.extract_info(url, download=True)
20
- video_path = os.path.join('downloads', f"{info_dict['title']}.mp4")
21
-
22
- # Return the path of the downloaded video file for the user to download
23
- return video_path
24
 
25
  except Exception as e:
26
  return f"Error: {str(e)}"
27
 
28
- # Create the Gradio interface
29
  iface = gr.Interface(
30
  fn=download_video,
31
  inputs=gr.Textbox(label="Enter the YouTube URL"),
@@ -34,5 +50,5 @@ iface = gr.Interface(
34
  description="Enter the URL of the YouTube video you want to download."
35
  )
36
 
37
- # Launch the interface
38
- iface.launch()
 
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"),
 
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