-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoiceIsolator.py
More file actions
142 lines (104 loc) · 4.27 KB
/
voiceIsolator.py
File metadata and controls
142 lines (104 loc) · 4.27 KB
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
import os
import sys
from spleeter.separator import Separator
import yt_dlp
import shutil
import glob
def filterName(name):
name = name.replace(' ', '')
name = name.replace('-', '')
name = name.replace('|', '')
name = name.replace('[', '')
name = name.replace(']', '')
return name
def download_mp3(youtube_url, output_directory):
video_opts = {
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4', # Ensure the format is MP4
'merge_output_format': 'mp4', # Ensure the final file is in MP4 format
'outtmpl': os.path.join(output_directory, '%(title)s.%(ext)s'),
'noplaylist': True,
'verbose': True,
}
with yt_dlp.YoutubeDL(video_opts) as ydl:
info_dict = ydl.extract_info(youtube_url, download=False)
video_title = info_dict.get('title', None)
# Create a directory named after the video title
folder_name = filterName(video_title)
output_path = os.path.join(output_directory, folder_name)
if not os.path.exists(output_path):
os.makedirs(output_path)
# Update the output template to include the new folder
video_opts['outtmpl'] = os.path.join(output_path, '%(title)s.%(ext)s')
with yt_dlp.YoutubeDL(video_opts) as ydl:
ydl.download([youtube_url])
File = ""
for file in glob.glob(output_path+"/*.mp4"):
File = file
os.rename(File, output_path+"/original.mp4")
ydl_opts_audio = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'outtmpl': os.path.join(output_directory, '%(title)s.%(ext)s'),
'noplaylist': True,
'verbose': True,
}
with yt_dlp.YoutubeDL(ydl_opts_audio) as ydl:
info_dict = ydl.extract_info(youtube_url, download=False)
video_title = info_dict.get('title', None)
# Create a directory named after the video title
folder_name = filterName(video_title)
output_path = os.path.join(output_directory, folder_name)
if not os.path.exists(output_path):
os.makedirs(output_path)
ydl_opts_audio['outtmpl'] = os.path.join(output_path, '%(title)s.%(ext)s')
with yt_dlp.YoutubeDL(ydl_opts_audio) as ydl:
ydl.download([youtube_url])
File = ""
print(output_path+"/*.mp3")
for file in glob.glob(output_path+"/*.mp3"):
File = file
os.rename(File, output_path+"/original.mp3")
return output_path
def isolate_vocals(input_file, output_dir, used_ytdl):
# Ensure the output directory exists
if not os.path.exists(output_dir):
os.makedirs(output_dir)
print("beginning with isolating vocals")
# Initialize the Spleeter separator with 2stems (vocals + accompaniment)
separator = Separator('spleeter:2stems')
print("isolating completed!, now saving to file")
# Perform the separation
separator.separate_to_file(input_file, output_dir)
folderName = output_dir + "/" + os.path.splitext(os.path.basename(input_file))[0]
# if yt_dlp:
# folderName = output_dir + "/"
# print("folder name ", folderName)
os.rename(folderName+"/vocals.wav", output_dir+"/vocals.wav")
print(f"Vocals and accompaniment saved to directory: {output_dir}")
def run(arg1):
input_file = arg1
output_dir = "Songs/"
used_ytdl = False
if "http" in input_file:
#is youtube link
dir = download_mp3(input_file, output_dir)
input_file = dir + "/original.mp3"
output_dir = dir
used_ytdl = True
else:
output_dir += os.path.splitext(os.path.basename(input_file))[0] + "Folder"
print("output directory ", output_dir)
print("input file ", input_file)
if not os.path.isfile(input_file):
print(f"Error: The file {input_file} does not exist.")
sys.exit(1)
isolate_vocals(input_file, output_dir, used_ytdl)
# if __name__ == "__main__":
# if len(sys.argv) != 2:
# print("Usage: python isolate_vocals.py <input_mp3_file / http>")
# sys.exit(1)
# run(sys.argv[1])