File size: 2,876 Bytes
f5799e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""YouTube Downloader"""

import io
import os

import yt_dlp
from PIL import Image

from modules.console_colors import ULTRASINGER_HEAD
from modules.Image.image_helper import crop_image_to_square


def get_youtube_title(url: str) -> tuple[str, str]:
    """Get the title of the YouTube video"""

    ydl_opts = {}
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        result = ydl.extract_info(
            url, download=False  # We just want to extract the info
        )

    if "artist" in result:
        return result["artist"].strip(), result["track"].strip()
    if "-" in result["title"]:
        return result["title"].split("-")[0].strip(), result["title"].split("-")[1].strip()
    return result["channel"].strip(), result["title"].strip()


def download_youtube_audio(url: str, clear_filename: str, output_path: str):
    """Download audio from YouTube"""

    print(f"{ULTRASINGER_HEAD} Downloading Audio")
    ydl_opts = {
        "format": "bestaudio/best",
        "outtmpl": output_path + "/" + clear_filename,
        "postprocessors": [
            {"key": "FFmpegExtractAudio", "preferredcodec": "mp3"}
        ],
    }

    start_download(ydl_opts, url)


def download_youtube_thumbnail(url: str, clear_filename: str, output_path: str):
    """Download thumbnail from YouTube"""

    print(f"{ULTRASINGER_HEAD} Downloading thumbnail")
    ydl_opts = {
        "skip_download": True,
        "writethumbnail": True,
    }

    download_and_convert_thumbnail(ydl_opts, url, clear_filename, output_path)


def download_and_convert_thumbnail(ydl_opts, url: str, clear_filename: str, output_path: str) -> None:
    """Download and convert thumbnail from YouTube"""

    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        info_dict = ydl.extract_info(url, download=False)
        thumbnail_url = info_dict.get("thumbnail")
        if thumbnail_url:
            response = ydl.urlopen(thumbnail_url)
            image_data = response.read()
            image = Image.open(io.BytesIO(image_data))
            image = image.convert('RGB') # Convert to RGB to avoid transparency or RGBA issues
            image_path = os.path.join(output_path, clear_filename + " [CO].jpg")
            image.save(image_path, "JPEG")
            crop_image_to_square(image_path)


def download_youtube_video(url: str, clear_filename: str, output_path: str) -> None:
    """Download video from YouTube"""

    print(f"{ULTRASINGER_HEAD} Downloading Video")
    ydl_opts = {
        "format": "bestvideo[ext=mp4]/mp4",
        "outtmpl": output_path + "/" + clear_filename + ".mp4",
    }
    start_download(ydl_opts, url)


def start_download(ydl_opts, url: str) -> None:
    """Start the download the ydl_opts"""

    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        errors = ydl.download(url)
        if errors:
            raise Exception("Download failed with error: " + str(errors))