Hev832 commited on
Commit
6c7ed02
1 Parent(s): 4b1708b

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +13 -3
run.py CHANGED
@@ -3,20 +3,30 @@ import yt_dlp
3
  import os
4
 
5
  def downloader(video_url, audio_format, audio_name):
 
6
  os.makedirs('audios', exist_ok=True)
 
 
 
 
7
  ydl_opts = {
8
  'format': 'bestaudio/best',
9
  'postprocessors': [{
10
  'key': 'FFmpegExtractAudio',
11
  'preferredcodec': audio_format,
12
  }],
13
- 'outtmpl': f"audios/{audio_name}",
14
  }
15
- output_path = f"audios/{audio_name}.%(ext)s"
16
 
17
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
18
  ydl.download([video_url])
19
- return output_path
 
 
 
 
 
 
20
 
21
  with gr.Blocks() as demo:
22
  gr.Markdown("# YouTube Downloader 2.0")
 
3
  import os
4
 
5
  def downloader(video_url, audio_format, audio_name):
6
+ # Ensure the directory exists
7
  os.makedirs('audios', exist_ok=True)
8
+
9
+ # Use a temporary placeholder for the output file
10
+ temp_output_path = f"audios/{audio_name}.%(ext)s"
11
+
12
  ydl_opts = {
13
  'format': 'bestaudio/best',
14
  'postprocessors': [{
15
  'key': 'FFmpegExtractAudio',
16
  'preferredcodec': audio_format,
17
  }],
18
+ 'outtmpl': temp_output_path,
19
  }
 
20
 
21
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
22
  ydl.download([video_url])
23
+
24
+ # Find the downloaded file and rename it
25
+ temp_file = temp_output_path.replace('%(ext)s', audio_format)
26
+ final_output_path = f"audios/{audio_name}.{audio_format}"
27
+ os.rename(temp_file, final_output_path)
28
+
29
+ return final_output_path
30
 
31
  with gr.Blocks() as demo:
32
  gr.Markdown("# YouTube Downloader 2.0")