-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
103 lines (91 loc) · 3.1 KB
/
main.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
import re
from pathlib import Path
import yt_dlp
from mutagen.id3 import TALB, TIT2, TPE1
from mutagen.mp3 import MP3
FOLDER: str = "music"
URLS: list[str] = None
YDL_OPTS = {
"format": "mp3/bestaudio",
# if music prepend "Artist, Artist - " else do nothing
"outtmpl": {
"default": f"{FOLDER}/%(artist&{{}} - |)s%(title)s.%(ext)s",
"pl_thumbnail": "",
},
"ignoreerrors": True,
"writethumbnail": True,
"postprocessors": [
{
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
},
{
"key": "FFmpegMetadata",
"add_metadata": True,
},
{
"key": "EmbedThumbnail",
"already_have_thumbnail": False,
},
],
"postprocessor_args": {
# square thumbnail
"embedthumbnail+ffmpeg_o": [
"-c:v",
"mjpeg",
"-vf",
"crop='if(gt(ih,iw),iw,ih)':'if(gt(iw,ih),ih,iw)'",
],
},
}
Path(FOLDER).mkdir(exist_ok=True)
if URLS is not None and len(URLS) > 0:
with yt_dlp.YoutubeDL(YDL_OPTS) as ydl:
error_code = ydl.download(URLS)
for file in Path(FOLDER).iterdir():
# standardising filename
new_file = Path(file).stem
# replacing characters
new_file = (
new_file.replace(" ", " ")
.replace("|", "|")
.replace("⧸", "/")
.replace("?", "?")
.replace("*", "*")
)
# standardising title string
title = new_file.split(" - ")[1]
# remove | <channel>, eg Artist - Song | Pressplay
title = re.sub(r"\|.*", "", title)
# remove (.*?), eg Artist - Song (Official Music Video)
title = re.sub(r"\((official|music).*?\)", "", title, flags=re.IGNORECASE)
# remove #.*?, eg Artist - Song #Album
title = re.sub(r"#\w*", "", title)
# remove [.*], eg Artist - Song [S2.E1]
title = re.sub(r"\[.*\]", "", title)
# use w/ (not W/) for with, eg Artist - Song W/ Artist
title = re.sub(r"(w|W)/.*", "", title)
title = title.strip()
# standardising artist list
artist_list = re.split(r', | X | x ', new_file.split(" - ")[0])
# remove duplicate artists that appear in artist_list / title
artist_list = [i for i in list(dict.fromkeys(artist_list)) if i.lower() not in title.lower()]
# creating artist string
artist_str = " x ".join(artist_list)
artist_str = artist_str.strip()
new_file_name = Path(f"{FOLDER}/{artist_str} - {title}.mp3")
Path(file).rename(new_file_name) # rename file
print(f"{artist_str} - {title}")
# standardising artists further for metadata
artists = re.sub(r"\(.*?\) ", "", artist_str) # remove parentheses & hashtags
artists = re.sub(r"#\w* ", "", artists)
# add audio tags
audio = MP3(new_file_name)
if audio.tags is None:
audio.add_tags()
audio.tags.add(TPE1(text=artists)) # add artists
audio.tags.add(TIT2(text=title)) # add title
audio.tags.add(TALB(text=title)) # add album name, possibly incorrect but idc
audio.tags.pop("TXXX:description", None) # del comments
audio.tags.pop("TXXX:synopsis", None)
audio.save()