-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractAudio.py
214 lines (154 loc) · 8.16 KB
/
extractAudio.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
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
import os
import configparser
# Loading configurations
configParser = configparser.RawConfigParser()
configFilePath = r'configuration.txt'
configParser.read(configFilePath)
insert_amd_env_vars = int(configParser.get('COMMON', 'insert_amd_env_vars'))
HSA_OVERRIDE_GFX_VERSION = configParser.get('COMMON', 'HSA_OVERRIDE_GFX_VERSION')
ROCM_PATH = configParser.get('COMMON', 'ROCM_PATH')
if(insert_amd_env_vars != 0):
os.environ["HSA_OVERRIDE_GFX_VERSION"] = HSA_OVERRIDE_GFX_VERSION
os.environ["ROCM_PATH"] = ROCM_PATH
import subprocess
import os
import pathlib
import configparser
import sqlite3 as sl
import speechbrain as sb
import torchaudio
from speechbrain.pretrained import EncoderClassifier
import cv2
from pydub import AudioSegment
import math
import pickle
import shutil
import time
import multiprocessing
from multiprocessing import Process,Queue
import itertools
from threading import Thread
start_time = time.time() # To measure execution time in seconds
print("PLEASE EDIT configuration.txt BEFORE EXECUTION")
print(".wav files might be generated in path. The program will automatically delete them. If execuetion stops unexpectedly, please delete them yourself")
datasetPathVideo = configParser.get('COMMON', 'datasetPathVideo')
datasetPathAudio = configParser.get('extractAudio', 'datasetPathAudio')
p = configParser.get('extractAudio', 'dbChunk')
ttwbdf = int(configParser.get('extractAudio', 'time_to_wait_before_deleting_files'))
cuda = int(configParser.get('COMMON', 'cuda'))
cpus = int(configParser.get('COMMON', 'cpus'))
datasetPathDatabase = configParser.get('COMMON', 'datasetPathDatabase') + '/dataset.db'
# TODO dataset.db in configuration too
print("Video dataset at " + datasetPathVideo )
print("Number of cpus to use for multiprocessing : ", cpus)
con = sl.connect(datasetPathDatabase) # Connection to databases
print('------------------- ABOUT TO START --------------------')
#TODO what if two files have the same name in the same batch
def extractAudio(rows):
#print(rows)
con2 = sl.connect(datasetPathDatabase)
# Embedding extractor and language classifier
if(cuda == 0):
classifier = EncoderClassifier.from_hparams(source="speechbrain/spkrec-ecapa-voxceleb")
classifierLang = EncoderClassifier.from_hparams(source="speechbrain/lang-id-commonlanguage_ecapa", savedir="pretrained_models/lang-id-commonlanguage_ecapa")
else:
classifier = EncoderClassifier.from_hparams(source="speechbrain/spkrec-ecapa-voxceleb",run_opts={"device":"cuda"})
classifierLang = EncoderClassifier.from_hparams(source="speechbrain/lang-id-commonlanguage_ecapa", savedir="pretrained_models/lang-id-commonlanguage_ecapa", run_opts={"device":"cuda"})
for row in rows:
#print(row)
absPathVideo = row[1] # for this one video
rowId = row[0] # id in database
absPathAudio = y = absPathVideo.replace(datasetPathVideo,datasetPathAudio) # for this one audio
absPathAudio = os.path.splitext(absPathAudio)[0]
absPathAudio_w = absPathAudio # without the end
absPathAudio = absPathAudio + "_audio.wav" # full path to extracted audio from the video
#Create Directory
pathlib.Path(os.path.dirname(absPathAudio)).mkdir(parents=True, exist_ok=True)
# Extract audio monochannel and with 16khz and put it in absPathAudio
command = "ffmpeg -nostats -loglevel 0 -y -i '" + absPathVideo + "' -acodec pcm_s16le -ab 160k -ac 1 -ar 16000 -vn '" + absPathAudio + "'"
subprocess.call(command, shell=True)
# Get original duration of video
audio = AudioSegment.from_file(absPathVideo)
audio_length_og = math.floor(audio.duration_seconds)
#print(audio_length_og)
# Will either truncate or loop the original video to reach audio_length (3,6,12 or 24)
audio_length_list = [6,12,24]
for audio_length in audio_length_list:
path_var_len_audio = absPathAudio_w + "audio" + str(audio_length) + "s.wav" # path to the variable length audio
path_var_len_audio_temp = absPathAudio_w + "audio_temp" + str(audio_length) + "s.wav" # path to a temp version of the variable length audio
if(audio_length_og > audio_length):
# Truncate
command = "ffmpeg -nostats -loglevel 0 -y -ss 0 -t "+str(audio_length)+" -i \"" + absPathAudio + "\" \"" + path_var_len_audio + "\""
subprocess.call(command, shell=True)
else:
# Loop then truncaate
#print("lesa")
twoDigitLenStr = f"{audio_length:02}"
#print(twoDigitLenStr)
command = "ffmpeg -nostats -loglevel 0 -y -stream_loop -1 -i '" + absPathAudio + "' -t \"00:00:"+twoDigitLenStr+".000\" -codec:a \"aac\" -f \"wav\" -c copy '"+ path_var_len_audio_temp + "'"
subprocess.call(command, shell=True)
command = "ffmpeg -nostats -loglevel 0 -y -ss 0 -t "+str(audio_length)+" -i \"" + path_var_len_audio_temp + "\" \"" + path_var_len_audio + "\""
subprocess.call(command, shell=True)
# Extract speaker embeddings
signal, fs = torchaudio.load(path_var_len_audio)
embeddings = classifier.encode_batch(signal)
embeddingsPickle = pickle.dumps(embeddings.cpu().detach().numpy()) # pickle embeddings to put in database
# Language classification
out_prob, score, index, text_lab = classifierLang.classify_file(path_var_len_audio)
lang = text_lab[0]
# Insert speaker embeddings and language into database
sql = ''' INSERT INTO AUDIO (VIDEO_ID,AUDIO_LENGTH,SPEAKER_EMB,LANG) VALUES(?,?,?,?)'''
cur = con2.cursor()
data = [rowId,audio_length,embeddingsPickle,lang]
cur.execute(sql, data)
con2.commit()
# Will delete those files after a little bit
ftd = [absPathAudio,path_var_len_audio,os.path.basename(path_var_len_audio),path_var_len_audio_temp]
tDelete = Thread(target=delFiles, args=(ftd,)) # spawn a process
tDelete.start()
sql = '''UPDATE VIDEO SET AUDIO_PRE = 1 WHERE ID = ?'''
data = [rowId]
cur.execute(sql, data)
con2.commit()
cur.close()
# Function to delete audio temp files
def delFiles(filesToDelete):
time.sleep(ttwbdf) # wait a bit
for file in filesToDelete:
try:
os.remove(file)
except OSError:
pass
# TODO Better display of progress and handling of exceptions
contLoop = True # Flag to continue to get chunks of videos from database
offset = 0
while(contLoop):
data = con.execute("SELECT * FROM VIDEO WHERE AUDIO_PRE = 0 ORDER BY ID ASC LIMIT " + p + " OFFSET " + str(offset))
contLoop = False
offset = offset + int(p)
print("Got chunk of videos from database. Extracting audio and features...")
# TODO write time
#print(data.fetchall())
dataGotten = data.fetchall()
rowsPerProcess = math.ceil(len(dataGotten) / cpus) # Will spawn no. of processes = cpus, each will get rows = rowsperprocess
procs = []
while(len(dataGotten) > 0):
rows=dataGotten[:rowsPerProcess] # rows to be sent to a process
dataGotten = dataGotten[rowsPerProcess:] # Deletes the rows that are going to be sent from dataGotten
#print(rows)
contLoop = True # Continue to get data from database since data length is not 0
proc = Process(target=extractAudio, args=(rows,)) # spawn a process
procs.append(proc)
proc.start()
for proc in procs: # wait for all processes to finish
proc.join()
print('---------------------------------------------------------------- FINISHED -----------------------------------------')
with con:
data = con.execute("SELECT count(*) FROM VIDEO")
for row in data:
print("THERE WERE " + str(row) + " VIDEO FILES")
with con:
data = con.execute("SELECT count(*) FROM AUDIO")
for row in data:
print(str(row) + " AUDIO FILES PRESENT IN DATABASE")
print("--- %s seconds ---" % (time.time() - start_time))