Hev832 commited on
Commit
24811bb
1 Parent(s): 0130bd6

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +25 -10
run.py CHANGED
@@ -1,15 +1,16 @@
1
  import gradio as gr
2
- import yt_dlp
3
  import os
4
- import spaces
5
 
6
- @spaces.GPU
7
- def downloader(video_url, audio_format, audio_name):
8
  # Ensure the directory exists
9
  os.makedirs('audios', exist_ok=True)
10
 
11
  # Use a temporary placeholder for the output file
12
- temp_output_path = f"audios/{audio_name}.%(ext)s"
 
 
 
13
 
14
  ydl_opts = {
15
  'format': 'bestaudio/best',
@@ -23,13 +24,26 @@ def downloader(video_url, audio_format, audio_name):
23
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
24
  ydl.download([video_url])
25
 
26
- # Find the downloaded file and rename it
27
- temp_file = temp_output_path.replace('%(ext)s', audio_format)
28
- final_output_path = f"audios/{audio_name}.{audio_format}"
29
- os.rename(temp_file, final_output_path)
 
 
 
 
 
 
 
 
30
 
31
  return final_output_path
32
 
 
 
 
 
 
33
 
34
 
35
 
@@ -71,7 +85,8 @@ with gr.Blocks() as demo:
71
  gr.Markdown("# YouTube Downloader 2.0")
72
  with gr.Row():
73
  video_url = gr.Textbox(placeholder="https://youtu.be/yQBGdXGCUbA?si=7avvqH-6OOkGWqFm", label="YouTube video link")
74
- audio_name = gr.Textbox(placeholder="killshot", label="Audio name of YouTube audio")
 
75
  audio_format = gr.Radio(["wav", "flac", "mp3"], label="Select the output format", value=" wav")
76
  with gr.Row():
77
  output = gr.Audio(label="Output")
 
1
  import gradio as gr
 
2
  import os
3
+ import yt_dlp
4
 
5
+ def downloader(video_url, audio_format, audio_name=None):
 
6
  # Ensure the directory exists
7
  os.makedirs('audios', exist_ok=True)
8
 
9
  # Use a temporary placeholder for the output file
10
+ if audio_name:
11
+ temp_output_path = f"audios/{audio_name}.%(ext)s"
12
+ else:
13
+ temp_output_path = f"audios/%(title)s.%(ext)s"
14
 
15
  ydl_opts = {
16
  'format': 'bestaudio/best',
 
24
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
25
  ydl.download([video_url])
26
 
27
+ # Find the downloaded file and rename it if audio_name is provided
28
+ if audio_name:
29
+ temp_file = temp_output_path.replace('%(ext)s', audio_format)
30
+ final_output_path = f"audios/{audio_name}.{audio_format}"
31
+ os.rename(temp_file, final_output_path)
32
+ else:
33
+ final_output_path = None
34
+ # Since the file is already named by title, find it by the pattern
35
+ for file in os.listdir('audios'):
36
+ if file.endswith(f".{audio_format}"):
37
+ final_output_path = os.path.join('audios', file)
38
+ break
39
 
40
  return final_output_path
41
 
42
+ # Example usage:
43
+ # downloader('https://www.youtube.com/watch?v=dQw4w9WgXcQ', 'mp3', 'my_audio')
44
+ # downloader('https://www.youtube.com/watch?v=dQw4w9WgXcQ', 'mp3')
45
+
46
+
47
 
48
 
49
 
 
85
  gr.Markdown("# YouTube Downloader 2.0")
86
  with gr.Row():
87
  video_url = gr.Textbox(placeholder="https://youtu.be/yQBGdXGCUbA?si=7avvqH-6OOkGWqFm", label="YouTube video link")
88
+ with gr.Row():
89
+ audio_name = gr.Textbox(placeholder="killshot", label="Audio name of YouTube audio", info="this is optional")
90
  audio_format = gr.Radio(["wav", "flac", "mp3"], label="Select the output format", value=" wav")
91
  with gr.Row():
92
  output = gr.Audio(label="Output")