-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·211 lines (180 loc) · 7.69 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3
import youtube_dl
import tempfile
import os
import sys
import re
import json
from pydub import AudioSegment
import argparse
import urllib
from mutagen.easyid3 import EasyID3
from mutagen.id3 import ID3, APIC, error
from mutagen.mp3 import MP3
import tracklist
import concurrent.futures
class MyLogger(object):
def debug(self, msg):
print(msg)
def warning(self, msg):
print(msg)
def error(self, msg):
print(msg)
def export_song_update_metadata(song):
print("Song: " + song.title)
print("Album: " + song.album)
song.export()
song.update_metadata()
# Simple class to hold individual song data and metadata
class Song(object):
def __init__(self, data, song_name, album_name, track_num, bitrate, file_name, coverfile=None):
self.data = data
self.title = song_name
self.album = album_name
self.track_num = track_num
self.coverfile = coverfile
self.bitrate = bitrate
self.file_name = file_name
def export(self, file_path=None, bitrate=None, fileformat="mp3"):
if bitrate == None:
bitrate = "%dk" % self.bitrate
if file_path == None:
file_path = self.file_name
self.data.export(file_path, format=fileformat,bitrate=bitrate)
def update_metadata(self, file_path=None):
if file_path == None:
file_path = self.file_name
# Too lazy to read the spec, more efficient just to use ID3 to do
# textual metadata and album art, but ¯\_(ツ)_/¯
audio = EasyID3(file_path)
audio['title'] = self.title
audio['album'] = self.album
audio['tracknumber'] = str(self.track_num)
audio.save()
if self.coverfile:
audio = ID3(file_path)
with open(self.coverfile, 'rb') as f:
audio['APIC'] = APIC(
encoding=3, # 3 is for utf-8
mime='image/jpeg', # image/jpeg or image/png
type=3, # 3 is for the cover image
desc=u'Cover',
data=f.read()
)
audio.save()
# Simple class to hold info about the OST
class FullOst(object):
def __init__(self, description_file, info_dict, audio_filename, ext, bitrate):
self.description_file = description_file
self.info = info_dict
self.audio_file = audio_filename
self.ext = ext
self.bitrate = bitrate
# If chapters was not populated, then we take matters into our own hands
# 😤😤😤😤
if not self.info['chapters']:
with open(self.description_file) as f:
self.tracklist = tracklist.TrackList.from_description((f.read()))
else:
self.tracklist = tracklist.TrackList.from_chapters(self.info['chapters'])
# Youtube-dl removes the timestamp, but often the description will wrap the timestamp in
# parens or square brackets.
def __scrub_song_name(self, name):
return name.replace('[]','').replace('()','').strip()
def fetch_album_art(self, directory):
urllib.request.urlretrieve(self.info['thumbnail'], "%s/art.jpeg" % (directory))
# split single audio file into individual songs, saved at output_dir
def splitOST(self, output_dir):
album_folder = "%s/%s" %(output_dir, self.info['fulltitle'])
try:
os.makedirs(album_folder)
except FileExistsError:
if not os.path.isdir(album_folder):
print("File %s already exists and is not a folder", album_folder)
sys.exit(1)
fullOSTAudio = AudioSegment.from_file(self.audio_file, self.ext)
self.fetch_album_art(album_folder)
def track_to_song(i,track):
# Seconds --> Milliseconds
start_time = int(track.start.to_seconds()) * 1000
# Last track in the tracklist may not have an end timestamp
if track.end:
if track.end == None:
print(track)
end_time = int(track.end.to_seconds()) * 1000
else:
end_time = len(fullOSTAudio)
audio_segment = fullOSTAudio[start_time:end_time]
song_name = self.__scrub_song_name(track.title)
# Only supporting mp3 for now; downloading from YT so it ain't
# exactly audiophile grade material we're working with here
song_filename = "%s/%s.%s" % (album_folder, song_name, 'mp3')
# Use 1 indexing instead of 0 index
track_num = i+1
return Song(audio_segment, song_name, self.info['fulltitle'], track_num, self.bitrate, song_filename, "%s/art.jpeg" % (album_folder))
songs = map(lambda t: track_to_song(*t), enumerate(self.tracklist.tracks))
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.map(export_song_update_metadata, songs)
# Remove .m4a, .description, and .json files in tmp dir
def cleanUpTempDir(tmpdir):
os.remove(tmpdir + "/art.jpeg")
def downloadAudio(url, download_dir=None):
if download_dir == None:
download_dir = os.getcwd()
ydl_opts = {
'format': 'bestaudio',
'writedescription': True,
'writeinfojson': True,
'outtmpl': download_dir + '/' + 'AUDIO.%(ext)s',
'logger': MyLogger(),
}
# ydl_opts = {
# 'format': 'best',
# 'postprocessors': [{
# 'key': 'FFmpegExtractAudio',
# 'preferredcodec': 'mp3',
# 'preferredquality': 'best',
# 'nopostoverwrites': True,
# }],
# 'keepvideo': True,
# 'writedescription': True,
# 'writeinfojson': True,
# 'outtmpl': download_dir + '/' + 'AUDIO.%(ext)s',
# 'logger': MyLogger(),
# }
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
info_file="%s/%s" % (download_dir,'AUDIO.info.json')
info_dict={}
with open(info_file,"r") as f:
data = f.read()
info_dict = json.loads(data)
audio_ext = info_dict['ext']
audio_filename = info_dict['_filename']
bitrate = info_dict['abr']
return (audio_filename,audio_ext,bitrate)
#TODO Support downloading from list of urls
#TODO Optimization: Do smart detection, if there is an audio only download option, use it; otherwise download hq video and ffmpeg extract
#TODO Make a reuseable ostDL class
#TODO scan comments for a valid tracklist if no tracklist is provided in the comments
def main():
parser = argparse.ArgumentParser(description='Download and split Full OST from Youtube link')
parser.add_argument('url', type=str, help='youtube link to download from')
parser.add_argument('--outputdir', type=str, default=os.getcwd(), required=False, help='directory to output songs to')
parser.add_argument('--tmpdir', type=str, default=tempfile.mkdtemp(), required=False, help='staging directory for store full ost and metadata')
parser.add_argument('--tracklist', type=str, default=None, required=False, help='text file with the track listing timestamps')
args = parser.parse_args()
tmpdir = os.path.abspath(args.tmpdir)
outputdir = os.path.abspath(args.outputdir)
audio_filename,ext,bitrate= downloadAudio(args.url, download_dir=args.tmpdir)
description_file=args.tracklist if args.tracklist else "%s/%s" % (args.tmpdir,'AUDIO.description')
info_file="%s/%s" % (args.tmpdir,'AUDIO.info.json')
info_dict = {}
with open(info_file,"r") as f:
data = f.read()
info_dict = json.loads(data)
ost = FullOst(description_file,info_dict, audio_filename, ext, bitrate)
ost.splitOST(outputdir)
# cleanUpTempDir(outputdir)
if __name__ == '__main__':
main()