File size: 9,417 Bytes
b8f40f2
 
63ec9ef
 
 
b8f40f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63ec9ef
b8f40f2
 
 
 
 
 
 
 
 
 
 
63ec9ef
b8f40f2
 
 
 
 
 
63ec9ef
b8f40f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63ec9ef
b8f40f2
63ec9ef
b8f40f2
 
 
63ec9ef
 
 
 
 
 
 
 
 
 
 
b8f40f2
 
 
 
 
 
 
 
 
 
 
 
 
 
63ec9ef
 
b8f40f2
 
 
 
 
 
 
 
 
 
63ec9ef
 
 
 
b8f40f2
 
 
 
 
 
 
 
 
 
 
 
 
 
63ec9ef
b8f40f2
 
63ec9ef
b8f40f2
 
 
63ec9ef
 
 
 
 
 
 
 
 
 
b8f40f2
 
 
63ec9ef
b8f40f2
 
 
 
63ec9ef
b8f40f2
 
 
 
63ec9ef
b8f40f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63ec9ef
b8f40f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63ec9ef
b8f40f2
 
 
63ec9ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8f40f2
 
63ec9ef
 
 
 
 
 
 
 
 
 
 
 
 
b8f40f2
 
 
63ec9ef
 
 
 
 
 
b8f40f2
 
 
 
 
 
63ec9ef
 
 
 
 
 
 
 
 
 
 
 
 
 
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import os, sys, re, json
import argparse
import shutil
import warnings
import whisper_timestamped as wt
from pdb import set_trace as b
from pprint import pprint as pp
from profanity_check import predict, predict_prob
from pydub import AudioSegment
from pydub.playback import play
from subprocess import Popen, PIPE

def parse_args(): 
    """ 
    """ 
    parser = argparse.ArgumentParser(
        description=('Tool to mute profanities in a song (source separation -> speech recognition -> profanity detection -> mask profanities -> re-mix)'),
        usage=('see <py main.py --help> or run as local web app with streamlit: <streamlit run main.py>')
    )

    parser.add_argument( 
        '-i', 
        '--input', 
        default=None, 
        nargs='?', 
        #required=True,
        help=("path to a mp3")
    )
    parser.add_argument( 
        '-m', 
        '--model', 
        default='small', 
        nargs='?', 
        help=("model used by whisper for speech recognition: tiny, small (default) or medium")
    )
    parser.add_argument( 
        '-p', 
        '--play', 
        default=False, 
        action='store_true',
        help=("play output audio at the end")
    )
    parser.add_argument( 
        '-v', 
        '--verbose', 
        default=True, 
        action='store_true',
        help=("print transcribed text and detected profanities to screen")
    )
    return parser.parse_args()


def main(args, input_file=None, model_size=None, verbose=False, play_output=False, skip_ss=False):
    """
    """
    if not input_file:
        input_file = args.input

    if not model_size:
        model_size = args.model 

    if not verbose:
        verbose = args.verbose
    
    if not play_output:
        play_output = args.play

    # exit if input file not found
    if len(sys.argv)>1 and not os.path.isfile(input_file):
        print('Error: --input file not found')
        raise Exception
    
    print(f'\nProcessing input file: {input_file}')

    if not skip_ss:
        # split audio into vocals + accompaniment
        print('Running source separation')
        stems_dir = source_separation(input_file, use_demucs=False, use_spleeter=True)
        vocal_stem = os.path.join(stems_dir, 'vocals.wav')
        #instr_stem = os.path.join(stems_dir, 'no_vocals.wav') # demucs
        instr_stem = os.path.join(stems_dir, 'accompaniment.wav') # spleeter
        print(f'Vocal stem written to: {vocal_stem}')
    else:
        vocal_stem = input_file
        instr_stem = None

    audio = wt.load_audio(vocal_stem)
    model = wt.load_model(model_size, device='cpu')
    text = wt.transcribe(model, audio, language='en')
    
    if verbose:
        print('\nTranscribed text:')
        print(text['text']+'\n')
    
    # checking for profanities in text
    print('Run profanity detection on text')
    profanities = profanity_detection(text)
    if not profanities:
        print(f'No profanities found in {input_file} - exiting')
        return 'No profanities found', None, None

    if verbose:
        print('profanities found in text:')
        pp(profanities)
 
    # masking
    print('Mask profanities in vocal stem')
    vocals = mask_profanities(vocal_stem, profanities)

    # re-mixing
    print('Merge instrumentals stem and masked vocals stem')
    if not skip_ss:
        mix = AudioSegment.from_wav(instr_stem).overlay(vocals)
    else:
        mix = vocals

    # write mix to file
    outpath = input_file.replace('.mp3', '_masked.mp3').replace('.wav', '_masked.wav')
    if input_file.endswith('.wav'):
        mix.export(outpath, format="wav")
    elif input_file.endswith('.mp3'):
        mix.export(outpath, format="mp3")
    print(f'Mixed file written to: {outpath}')

    # play output
    if play_output:
        print('\nPlaying output...')
        play(mix)

    return outpath, vocal_stem, instr_stem


def source_separation(inpath, use_demucs=False, use_spleeter=True): 
    """
    Execute shell command to run demucs and pipe stdout/stderr back to python
    """
    infile = os.path.basename(inpath)
    
    if use_demucs:
        cmd = f'demucs --two-stems=vocals --jobs 8 "{inpath}"'
        #stems_dir = os.path.join(re.findall('/.*', stdout)[0], infile.replace('.mp3','').replace('.wav',''))
    elif use_spleeter:
        outdir = 'audio/separated'
        cmd = f'spleeter separate {inpath} -p spleeter:2stems -o {outdir}' 
        stems_dir = os.path.join(outdir, os.path.splitext(infile)[0])

    stdout, stderr = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True, executable='/bin/bash').communicate()
    stdout = stdout.decode('utf8')
    
    # exit if lib error'd out
    if stderr:
        stderr = stderr.decode('utf-8').lower()
        if 'error' in stderr or 'not exist' in stderr:
            print(stderr.decode('utf8').split('\n')[0])
            raise Exception

    # parse stems directory path from stdout and return it if successful
    if not os.path.isdir(stems_dir):
        print(f'Error: output stem directory "{stems_dir}" not found')
        raise Exception

    return stems_dir


def profanity_detection(text):
    """
    """
    # detect profanities in text
    profs = []
    for segment in text['segments']:
        for word in segment['words']:
            #if word['confidence']<.25:
            #    print(word)
            text = word['text'].replace('.','').replace(',','').lower()

            # skip false positives
            if text in ['cancer','hell','junk','die','lame','freak','freaky','white','stink','shut','spit','mouth','orders','eat','clouds','ugly','dirty','wet']:
                continue

            # assume anything returned by whisper with more than 1 * is profanity e.g n***a
            if '**' in text:
                profs.append(word)
                continue

            # add true negatives
            if text in ['bitchy', 'puss']:
                profs.append(word)
                continue

            # run profanity detection - returns 1 (True) or 0 (False)
            if predict([word['text']])[0]:
                profs.append(word)

    return profs


def mask_profanities(vocal_stem, profanities):
    """
    """
    # load vocal stem and mask profanities
    vocals = AudioSegment.from_wav(vocal_stem)
    for prof in profanities:
        mask   = vocals[prof['start']*1000:prof['end']*1000] # pydub works in milliseconds
        mask  -= 50 # reduce lvl by some dB (enough to ~mute it)
        #mask   = mask.silent(len(mask))
        #mask   = mask.fade_in(100).fade_out(100) # it prepends/appends fades so end up with longer mask
        start  = vocals[:prof['start']*1000]
        end    = vocals[prof['end']*1000:]
        #print(f"masking {prof['text']} from {prof['start']} to {prof['end']}")
        vocals = start + mask + end

    return vocals


if __name__ == "__main__":
    args = parse_args()
    
    if len(sys.argv)>1:
        main(args, skip_ss=False)
    else:
        import streamlit as st
        st.title('Saylss')
        with st.expander("About", expanded=False):
            st.markdown('''
            This app processes an input audio track (.mp3 or .wav) with the purpose of identifying and muting profanities in the song.
            

            A larger model takes longer to run and is more accurate, and vice-versa.


            Simply select the model size and upload your file!
            ''')
        model = st.selectbox('Choose model size:', ('tiny','small','medium'), index=1)

        uploaded_file = st.file_uploader(
            "Choose input track:",
            type=[".mp3",".wav"],
            accept_multiple_files=False,
        )

        if uploaded_file is not None:
            uploaded_file.name = uploaded_file.name.replace(' ','_')
            ext = os.path.splitext(uploaded_file.name)[1]
            if ext == '.wav':
                st_format = 'audio/wav'
            elif ext == '.mp3':
                st_format = 'audio/mp3'

            uploaded_file_content = uploaded_file.getvalue()
            with open(uploaded_file.name, 'wb') as f:
                f.write(uploaded_file_content)

            audio_bytes_input = uploaded_file_content
            st.audio(audio_bytes_input, format=st_format)
            
            # run code
            with st.spinner('Processing input audio...'):
                inpath = os.path.abspath(uploaded_file.name)
                outpath, vocal_stem, instr_stem = main(args, input_file=inpath, model_size=model)
                
                if outpath == 'No profanities found':
                    st.text(outpath + ' - Refresh the page and try a different song or model size')
                    sys.exit()
            
            # display output audio
            #st.text('Play output Track:')
            st.text('\nOutput:')
            audio_file = open(outpath, 'rb')
            audio_bytes = audio_file.read()
            st.audio(audio_bytes, format=st_format)

            # flush all media
            if os.path.isfile(inpath):
                os.remove(inpath)
            if os.path.isfile(outpath):
                os.remove(outpath)
            if os.path.isfile(vocal_stem):
                os.remove(vocal_stem)
            if os.path.isfile(instr_stem):
                os.remove(instr_stem)
            sep_dir = os.path.split(instr_stem)[0]
            if os.path.isdir(sep_dir):
                os.rmdir(sep_dir)