File size: 1,532 Bytes
e81fb27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# downloader.py

import os
import yt_dlp
import gradio as gr


def audio_downloader(url_media: str):
    url_media = url_media.strip()
    if not url_media:
        return None

    dir_output_downloads = "downloads"
    os.makedirs(dir_output_downloads, exist_ok=True)

    media_info = yt_dlp.YoutubeDL({
        "quiet": True,
        "no_warnings": True,
        "noplaylist": True
    }).extract_info(url_media, download=False)

    download_path = f"{os.path.join(dir_output_downloads, media_info['title'])}.m4a"

    ydl_opts = {
        'format': 'm4a/bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'm4a'
        }],
        'force_overwrites': True,
        'noplaylist': True,
        'no_warnings': True,
        'quiet': True,
        'ignore_no_formats_error': True,
        'restrictfilenames': True,
        'outtmpl': download_path
    }

    with yt_dlp.YoutubeDL(ydl_opts) as ydl_download:
        ydl_download.download([url_media])

    return download_path


# Gradio downloader-related UI components

def downloader_conf():
    return gr.Checkbox(False, label="URL-to-Audio", container=False)

def url_media_conf():
    return gr.Textbox("", label="Enter URL", placeholder="www.youtube.com/watch?v=abc123", visible=False, lines=1)

def url_button_conf():
    return gr.Button("Go", variant="secondary", visible=False)

def show_components_downloader(value_active):
    return gr.update(visible=value_active), gr.update(visible=value_active)