openslr-slr67 / convert.py
daniel-dona's picture
Use TAR
21dc725
raw
history blame
No virus
1.25 kB
import os
import csv
import tqdm
import ffmpeg
import hashlib
# This script was used to encode and organize the files from the original dataset https://www.openslr.org/67/
# Each file is renamed using the sha256 sum of the content and encoded using OPUS codec
transcriptions_file = "./files/TEDx_Spanish.transcription"
audio_dir = "./speech"
output_dir = "./cuts"
lines = open(transcriptions_file).readlines()
transcriptions = [ [line.split("TEDX_")[0], "TEDX_"+line.split("TEDX_")[1].replace("\n", "")] for line in lines]
transcriptions = {k:v for v,k in transcriptions}
output = []
for filename in tqdm.tqdm(list(os.scandir(audio_dir))):
if filename.name.endswith(".wav"):
if filename.name.replace(".wav", "") in transcriptions:
digest = hashlib.sha256(open(filename.path, 'rb').read()).hexdigest()
ffmpeg.input(filename.path).output(f"{output_dir}/{digest}.opus").overwrite_output().run(quiet=True)
output.append([f"{digest}.opus", ffmpeg.probe(filename.path)['streams'][0]['duration'] , transcriptions[filename.name.replace(".wav", "")]])
with open('output.csv', 'w', newline='') as csvfile:
csv_writer = csv.writer(csvfile,delimiter='\t')
csv_writer.writerows(output)