-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReformat_Files.py
53 lines (43 loc) · 1.57 KB
/
Reformat_Files.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
import json
import os
import shutil
import soundfile as sf
"""
This file identifies the audio files that are shorter than 2 seconds and remove those segments.
Then it renames the remaining files with the speaker name nd meeting ID.
"""
# get file paths from configuration file
with open('config.json') as config_file:
data = json.load(config_file)
filenames = []
vocal_sound_ids = []
audio_folder = data['SpeakerOutputFolder']['path']
output_folder = data['FinalOutputFolder']['path']
speakers = os.listdir(audio_folder)
def get_duration(directory):
f = sf.SoundFile(directory)
audio_duration = len(f) / f.samplerate
print('seconds = {}'.format(audio_duration))
return audio_duration
for speaker in speakers:
files = os.listdir(audio_folder + '/' + speaker)
for audio_file in files:
dir = audio_folder + '/' + speaker + '/' + audio_file
duration = get_duration(dir)
dest = output_folder + '/' + speaker
if not (os.path.exists(dest)):
os.mkdir(dest)
print(dest)
if duration > 2.0:
shutil.copy(dir, dest)
speakers = os.listdir(output_folder)
for speaker in speakers:
files = os.listdir(output_folder + '/' + speaker)
file_ID = 0
for audio_file in files:
src_file = output_folder + '/' + speaker + '/' + audio_file
file_name = speaker + '_' + audio_file.split('-')[0] + '_' + str(file_ID) + '.wav'
dest_file = output_folder + '/' + speaker + '/' + file_name
print('file name : ', file_name)
os.rename(src_file, dest_file)
file_ID += 1