-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
67 lines (51 loc) · 1.62 KB
/
base.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
from urllib.parse import urlparse
from yt_dlp import YoutubeDL
from youtubesearchpython import VideosSearch
from spleeter.separator import Separator
import os
SONGS_DIR = 'songs/'
SPLITS_DIR = 'splits/'
def is_url(item):
return bool(urlparse(item).scheme)
def get_title(item):
if item[-4:] == 'song':
return item[:-5]
else:
return item
def get_song_fp(item):
return SONGS_DIR + get_title(song) + '.mp3'
def fetch_song(item, force=False):
search = VideosSearch(item, limit=1).result()['result'][0]
if is_url(item):
# noinspection PyTypeChecker
title = search['title']
to_fetch = item
else:
title = get_title(item)
# noinspection PyTypeChecker
to_fetch = search['link']
file_path = get_song_fp(title)
dl_opts = {
'format': 'bestaudio/best',
'outtmpl': file_path,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
if force or not os.path.exists(file_path):
YoutubeDL(dl_opts).extract_info(to_fetch)
else:
print('Using saved version of song: ', title)
def process_song(item, seperator, force=False):
file_path = SPLITS_DIR + get_title(item) + '/'
if force or not os.path.exists(file_path):
separator.separate_to_file(get_song_fp(item), SPLITS_DIR)
else:
print('Reusing splits for song: ', item)
if __name__ == '__main__':
separator = Separator('spleeter:2stems')
song = 'The Sound of Silence (Audio)'
fetch_song(song)
process_song(song, separator)