Jekyll2000 commited on
Commit
d9e5a18
1 Parent(s): 68239f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -1,15 +1,13 @@
1
- import os
 
2
  import gradio as gr
3
  import yt_dlp
4
 
5
  def download_media(url, download_video):
6
- download_dir = 'downloads'
7
- os.makedirs(download_dir, exist_ok=True)
8
-
9
  if download_video:
10
  ydl_opts = {
11
  'format': 'bestvideo+bestaudio/best',
12
- 'outtmpl': f'{download_dir}/%(title)s.%(ext)s',
13
  }
14
  else:
15
  ydl_opts = {
@@ -19,13 +17,13 @@ def download_media(url, download_video):
19
  'preferredcodec': 'mp3',
20
  'preferredquality': '192',
21
  }],
22
- 'outtmpl': f'{download_dir}/%(title)s.%(ext)s',
23
  }
24
 
25
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
26
  info_dict = ydl.extract_info(url, download=True)
27
  file_title = ydl.prepare_filename(info_dict)
28
-
29
  if download_video:
30
  output_file = file_title
31
  else:
@@ -33,16 +31,16 @@ def download_media(url, download_video):
33
 
34
  return output_file
35
 
36
- def handle_download(url, download_video):
37
- output_file = download_media(url, download_video)
38
  if download_video:
39
- return gr.update(value=output_file, visible=True), gr.update(visible=False)
40
  else:
41
- return gr.update(visible=False), gr.update(value=output_file, visible=True)
42
 
 
43
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", secondary_hue="orange")) as demo:
44
- gr.Markdown(f"# <div style='text-align: center;'>YOUTUBE Downloader</div>")
45
- gr.Markdown(f"## <div style='text-align: center;'>Download MP3/MP4 from YouTube URL</div>")
46
 
47
  with gr.Row():
48
  url_input = gr.Textbox(label="YouTube URL")
@@ -54,12 +52,18 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", secondary_hue="orange"
54
  output_audio = gr.Audio(label="Downloaded Media", visible=False)
55
  output_file = gr.File(label="Downloaded Media", visible=False)
56
 
 
 
 
 
 
 
 
 
57
  download_button.click(
58
  handle_download,
59
  inputs=[url_input, download_video_checkbox],
60
  outputs=[output_file, output_audio]
61
  )
62
 
63
- gr.Markdown(f"### <div style='text-align: center;'>Made with ❤ by <a href='https://huggingface.co/Gradio-Blocks'>Gradio-Blocks-Party-Member</a></div>")
64
-
65
  demo.launch()
 
1
+ print("please wait...")
2
+
3
  import gradio as gr
4
  import yt_dlp
5
 
6
  def download_media(url, download_video):
 
 
 
7
  if download_video:
8
  ydl_opts = {
9
  'format': 'bestvideo+bestaudio/best',
10
+ 'outtmpl': 'downloads/%(title)s.%(ext)s',
11
  }
12
  else:
13
  ydl_opts = {
 
17
  'preferredcodec': 'mp3',
18
  'preferredquality': '192',
19
  }],
20
+ 'outtmpl': 'downloads/%(title)s.%(ext)s',
21
  }
22
 
23
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
24
  info_dict = ydl.extract_info(url, download=True)
25
  file_title = ydl.prepare_filename(info_dict)
26
+
27
  if download_video:
28
  output_file = file_title
29
  else:
 
31
 
32
  return output_file
33
 
34
+ def get_output_component(download_video):
 
35
  if download_video:
36
+ return gr.File(label="Downloaded Media")
37
  else:
38
+ return gr.Audio(label="Downloaded Media")
39
 
40
+ # Create the Gradio interface
41
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", secondary_hue="orange")) as demo:
42
+ gr.Markdown(f"# <div style='text-align: center;'> YOUTUBE Downloader</div>")
43
+ gr.Markdown(f"## <div style='text-align: center;'> download mp3/mp4 form youtube url </div>")
44
 
45
  with gr.Row():
46
  url_input = gr.Textbox(label="YouTube URL")
 
52
  output_audio = gr.Audio(label="Downloaded Media", visible=False)
53
  output_file = gr.File(label="Downloaded Media", visible=False)
54
 
55
+ def handle_download(url, download_video):
56
+ output_file = download_media(url, download_video)
57
+ if download_video:
58
+ return gr.update(value=output_file, visible=True), gr.update(visible=False)
59
+ else:
60
+ return gr.update(visible=False), gr.update(value=output_file, visible=True)
61
+ gr.Markdown(f"### <div style='text-align: center;'>made with ❤ by <a href='https://www.linkedin.com/in/muhammad-haseeb-ahmed-1954b5230/'>Muhammad Haseeb Ahmed</a></div>")
62
+
63
  download_button.click(
64
  handle_download,
65
  inputs=[url_input, download_video_checkbox],
66
  outputs=[output_file, output_audio]
67
  )
68
 
 
 
69
  demo.launch()