-
Notifications
You must be signed in to change notification settings - Fork 0
/
ytdlp-split.py
59 lines (45 loc) · 1.42 KB
/
ytdlp-split.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
import yt_dlp
import ffmpeg
import sys
import os
import glob
import music_tag
forbiddenChars = ['/','<','>',':','\\','|','?','*']
def main():
url = input("URL:")
foldername = input("Album Name:")
addNumbers = input("Add Numbers (y/n): ")
dl_opts = {
'format': 'bestaudio/best',
}
if (os.path.isdir(foldername)==False):
os.mkdir(foldername)
os.chdir(foldername)
dl = yt_dlp.YoutubeDL(dl_opts)
chapters = dl.extract_info(url, download=False)["chapters"]
if chapters[0]["title"] == "<Untitled Chapter 1>":
chapters.pop(0)
print(chapters)
zeroes = len(str(len(chapters)))
dl.download(url)
originalFile = glob.glob("*")[0]
#split by chapters
for i,chapter in enumerate(chapters):
startTime = chapter["start_time"]
endTime = chapter["end_time"]
name = chapter["title"]
for char in forbiddenChars:
name = name.replace(char,'')
name = name+".mp3"
if addNumbers == 'y':
name = str(i+1).zfill(zeroes)+" - "+name
ffmpeg.run(ffmpeg.output(ffmpeg.input(originalFile).audio.filter('atrim',start=startTime,end=endTime),name))
f = music_tag.load_file(name)
f["title"] = chapter["title"]
f["album"] = foldername
if addNumbers == 'y':
f["tracknumber"] = i+1
f.save()
os.remove(originalFile)
if __name__ == "__main__":
main()