forked from ghodouss/CleanBeats.ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcensor.py
More file actions
49 lines (34 loc) · 1.27 KB
/
censor.py
File metadata and controls
49 lines (34 loc) · 1.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
from pydub import AudioSegment
from pydub.playback import play
import time
import os
# read in audio file and get the two mono tracks
'''
This function takes the file name of the song and the portions to be censored as a list and then removes the vocals from the required bits
Input:
file_name: The name of the song file
snips: Dictionary of sections to be removed
Output: censored file
'''
def censor(file_name,snips=[{"start":20,"end":25},{"start":30,"end":35}]):
#name = str(time.time()) + ".mp3"
name = "clean_file.wav"
sound_stereo = AudioSegment.from_file(file_name, format="mp3")
#os.remove(file_name)
words=snips
for i in words:
start = int(max(i["start"]-.1, 0)*1000)
end = int(min((i["end"]+.1)*1000, len(sound_stereo)-1))
samp=sound_stereo[start: end]
sound_monoL = samp.split_to_mono()[0]
sound_monoR = samp.split_to_mono()[1]
# Invert phase of the Right audio file
sound_monoR_inv = sound_monoR.invert_phase()
# Merge two L and R_inv files, this cancels out the centers
sound_CentersOut = sound_monoL.overlay(sound_monoR_inv)
sound_stereo = sound_stereo[:start]+sound_CentersOut+sound_stereo[end:]
# Export merged audio file
fh = sound_stereo.export("clean_files/"+name, format="wav")
return name
if __name__ == '__main__':
censor("sample.mp3")