ilhamdev commited on
Commit
d861f36
1 Parent(s): 10c5e6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -25
app.py CHANGED
@@ -4,32 +4,33 @@ 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 = {
14
- 'format': 'bestaudio/best',
15
- 'postprocessors': [{
16
- 'key': 'FFmpegExtractAudio',
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:
30
- output_file = file_title.rsplit('.', 1)[0] + '.mp3'
31
 
32
- return output_file
 
 
 
 
33
 
34
  def get_output_component(download_video):
35
  if download_video:
@@ -39,11 +40,11 @@ def get_output_component(download_video):
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")
47
  with gr.Row():
48
  download_video_checkbox = gr.Checkbox(label="Download Video", value=False)
49
  with gr.Row():
@@ -58,12 +59,14 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", secondary_hue="orange"
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://huggingface.co/Gradio-Blocks'>Gradio-Blocks-Party-Member</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()
 
 
4
  import yt_dlp
5
 
6
  def download_media(url, download_video):
7
+ ydl_opts = {
8
+ 'format': 'bestvideo+bestaudio/best' if download_video else 'bestaudio/best',
9
+ 'outtmpl': 'downloads/%(title)s.%(ext)s',
10
+ 'postprocessors': [{
11
+ 'key': 'FFmpegExtractAudio',
12
+ 'preferredcodec': 'mp3',
13
+ 'preferredquality': '192',
14
+ }] if not download_video else [],
15
+ 'noplaylist': True,
16
+ 'quiet': True,
17
+ 'progress_hooks': [progress_hook],
18
+ }
 
 
 
19
 
20
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
21
  info_dict = ydl.extract_info(url, download=True)
22
  file_title = ydl.prepare_filename(info_dict)
23
 
24
+ if not download_video:
25
+ file_title = file_title.rsplit('.', 1)[0] + '.mp3'
26
+
27
+ return file_title
28
 
29
+ def progress_hook(d):
30
+ if d['status'] == 'downloading':
31
+ print(f"Downloading: {d['_percent_str']} of {d['_total_bytes_str']}")
32
+ elif d['status'] == 'finished':
33
+ print("Download complete, now converting...")
34
 
35
  def get_output_component(download_video):
36
  if download_video:
 
40
 
41
  # Create the Gradio interface
42
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", secondary_hue="orange")) as demo:
43
+ gr.Markdown(f"# <div style='text-align: center;'>YOUTUBE Downloader</div>")
44
+ gr.Markdown(f"## <div style='text-align: center;'>Download MP3/MP4 from YouTube URL</div>")
45
 
46
  with gr.Row():
47
+ url_input = gr.Textbox(label="YouTube URL", placeholder="Enter YouTube video URL here")
48
  with gr.Row():
49
  download_video_checkbox = gr.Checkbox(label="Download Video", value=False)
50
  with gr.Row():
 
59
  return gr.update(value=output_file, visible=True), gr.update(visible=False)
60
  else:
61
  return gr.update(visible=False), gr.update(value=output_file, visible=True)
62
+
 
63
  download_button.click(
64
  handle_download,
65
  inputs=[url_input, download_video_checkbox],
66
  outputs=[output_file, output_audio]
67
  )
68
 
69
+ gr.Markdown(f"### <div style='text-align: center;'>Made with ❤ by <a href='https://huggingface.co/Gradio-Blocks'>Gradio-Blocks-Party-Member</a></div>")
70
+
71
  demo.launch()
72
+