-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileinput.py
103 lines (80 loc) · 2.71 KB
/
fileinput.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
import datetime
import os
import analysis
import SoundFormInfo
import time
import tinytag
from spleeter.separator import Separator, AudioAdapter
import soundfile as sf
import shutil
'''
def _wav_to_mp3(filename):
directory = "./temp/" + filename + '/accompaniment.wav'
origin = AudioSegment.from_wav(directory)
os.mkdir('./additionalData/' + filename)
new_file = './additionalData/' + filename + '/' + filename + '.mp3'
origin.export(new_file, format='mp3')
os.remove(directory)
return
'''
def filename_fetch(directory):
directory = os.path.abspath(directory) # Convert to absolute path
filename = os.path.basename(directory)[:-4]
return filename, directory
def rename_directory(old_name, new_name):
try:
os.rename(old_name, new_name)
except :
pass
return
def _separate(directory,filename,spl):
audio_adapter = AudioAdapter.default()
raw_wave, _ = audio_adapter.load(directory)
stems = spl.separate(raw_wave)
accompaniment = stems["accompaniment"]
vocal = stems["vocals"]
directory = f'./additionalData/{filename}/'
if os.path.exists(directory):
shutil.rmtree(directory)
try:
os.makedirs(directory)
except:
pass
new_file = f'./additionalData/{filename}/{filename}.mp3'
new_file = os.path.abspath(new_file)
sf.write(new_file, accompaniment, samplerate=44100, format="MP3")
#temp = f'./additionalData/{filename}/{filename}_temp.mp3'
#sf.write(temp, vocal, samplerate=44100, format="MP3")
del accompaniment
return vocal
'''
def _remove_tmp(filename):
remove_directory = './temp/' + filename
rmtree(remove_directory)
return
'''
def _export_basic_info(directory, filename):
file_path = directory
audio = tinytag.TinyTag.get(file_path)
metadata = str(datetime.timedelta(seconds=audio.duration))[2:-7]
name=filename.split('-')[1].strip()
artist=filename.split('-')[0].strip()
return SoundFormInfo.SoundFormInfo(name,
artist,
metadata)
def input_file(directory,spl):
filename, abs_directory= filename_fetch(directory)
res = _export_basic_info(abs_directory, filename)
try:
os.makedirs('./OriginalSong/')
except:
pass
shutil.copy(abs_directory, f'./OriginalSong/')
vocal_waveform = _separate(directory,filename,spl) # 음원 분리
analysis.file_analysis(vocal_waveform,filename) # 보컬 정보 추출
return res
if __name__ == '__main__':
GLOBAL_SPLITTER = Separator('spleeter:2stems', stft_backend='tensorflow', multiprocess=False)
ti=time.time()
input_file('./박효신-야생화.mp3',GLOBAL_SPLITTER)
print(time.time()-ti)