CCockrum commited on
Commit
e81fb27
·
verified ·
1 Parent(s): 52685e3

Update downloader.py

Browse files
Files changed (1) hide show
  1. downloader.py +57 -0
downloader.py CHANGED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # downloader.py
2
+
3
+ import os
4
+ import yt_dlp
5
+ import gradio as gr
6
+
7
+
8
+ def audio_downloader(url_media: str):
9
+ url_media = url_media.strip()
10
+ if not url_media:
11
+ return None
12
+
13
+ dir_output_downloads = "downloads"
14
+ os.makedirs(dir_output_downloads, exist_ok=True)
15
+
16
+ media_info = yt_dlp.YoutubeDL({
17
+ "quiet": True,
18
+ "no_warnings": True,
19
+ "noplaylist": True
20
+ }).extract_info(url_media, download=False)
21
+
22
+ download_path = f"{os.path.join(dir_output_downloads, media_info['title'])}.m4a"
23
+
24
+ ydl_opts = {
25
+ 'format': 'm4a/bestaudio/best',
26
+ 'postprocessors': [{
27
+ 'key': 'FFmpegExtractAudio',
28
+ 'preferredcodec': 'm4a'
29
+ }],
30
+ 'force_overwrites': True,
31
+ 'noplaylist': True,
32
+ 'no_warnings': True,
33
+ 'quiet': True,
34
+ 'ignore_no_formats_error': True,
35
+ 'restrictfilenames': True,
36
+ 'outtmpl': download_path
37
+ }
38
+
39
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl_download:
40
+ ydl_download.download([url_media])
41
+
42
+ return download_path
43
+
44
+
45
+ # Gradio downloader-related UI components
46
+
47
+ def downloader_conf():
48
+ return gr.Checkbox(False, label="URL-to-Audio", container=False)
49
+
50
+ def url_media_conf():
51
+ return gr.Textbox("", label="Enter URL", placeholder="www.youtube.com/watch?v=abc123", visible=False, lines=1)
52
+
53
+ def url_button_conf():
54
+ return gr.Button("Go", variant="secondary", visible=False)
55
+
56
+ def show_components_downloader(value_active):
57
+ return gr.update(visible=value_active), gr.update(visible=value_active)