Skip to content

src/run.py

Set artwork from YouTube thumbnail.

Parameters:

Name Type Description Default
videoid str

YouTube Video ID

required
mp3_file_path str

MP3 file Path

required
Source code in src/run.py
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
def set_artwork(videoid, mp3_file_path):
    """
    Set artwork from YouTube thumbnail.

    Args:
        videoid (str): YouTube Video ID
        mp3_file_path (str): MP3 file Path
    """
    url = f"https://img.youtube.com/vi/{videoid}/0.jpg"
    artwork_file_path = f"output/{videoid}.jpg"

    with request.urlopen(url) as r:
        data = r.read()
        with open(artwork_file_path, mode="wb") as o:
            o.write(data)

    with Image.open(artwork_file_path) as img:
        resized_img = img.resize((300, 300))
        resized_img.save(artwork_file_path)

    with open(artwork_file_path, mode="rb") as r:
        data = r.read()
        tags = ID3(mp3_file_path)
        tags.add(APIC(mime="image/jpeg", type=3, data=data))
        tags.save()

    os.remove(artwork_file_path)

Set Metadata from metadata dict.

Parameters:

Name Type Description Default
metadata dict

metadata dict in data.py

required
mp3_file_path str

MP3 file Path

required
Source code in src/run.py
54
55
56
57
58
59
60
61
62
63
64
65
def set_metadata(metadata, mp3_file_path):
    """
    Set Metadata from metadata dict.

    Args:
        metadata (dict): metadata dict in data.py
        mp3_file_path (str): MP3 file Path
    """
    tags = EasyID3(mp3_file_path)
    tags["title"] = metadata["title"]
    tags["artist"] = metadata["artist"]
    tags.save()

Downloads YouTube videos as MP3 files, validates data.py, and sets metadata.

Raises:

Type Description
RuntimeError

Raised if Validation fails.

RuntimeError

Raised if Thumbnail Image URL returns a 404 status.

RuntimeError

Raised if Downloading fails with non-zero return code.

Source code in src/run.py
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def main():
    """
    Downloads YouTube videos as MP3 files, validates data.py, and sets metadata.

    Raises:
        RuntimeError: Raised if Validation fails.
        RuntimeError: Raised if Thumbnail Image URL returns a 404 status.
        RuntimeError: Raised if Downloading fails with non-zero return code.
    """
    if not validate_yt_list():
        raise RuntimeError("Validation failed.")

    for item in yt_list:
        videoid = item["id"]
        output_videoid = f"output/{videoid}"
        mp3_file_path = output_videoid + ".mp3"

        if os.path.isfile(mp3_file_path):
            continue

        url = f"https://i.ytimg.com/vi/{videoid}/hqdefault.jpg"
        if check_video_deletion(url):
            raise RuntimeError(
                f"The Thumbnail Image URL returned a 404 status. ({videoid})"
            )

        ydl_opts["outtmpl"] = output_videoid
        with YoutubeDL(ydl_opts) as ydl:
            url = f"https://www.youtube.com/watch?v={videoid}"
            retcode = ydl.download([url])
            if retcode != 0:
                raise RuntimeError(
                    f"Downloading failed with non-zero return code. ({videoid})"
                )

            set_metadata(item["metadata"], mp3_file_path)
            set_artwork(videoid, mp3_file_path)