|
|
|
|
|
from datasets import load_dataset, Audio |
|
|
import soundfile as sf, os, re, neologdn, librosa |
|
|
from tqdm import tqdm |
|
|
import shutil |
|
|
|
|
|
def have(a): |
|
|
return a is not None |
|
|
|
|
|
def aorb(a, b): |
|
|
return a if have(a) else b |
|
|
|
|
|
dataset = load_dataset("Sin2pi/JA_audio_JA_text_180k_samples", trust_remote_code=True)["train"].filter(lambda sample: bool(sample["sentence" if "sentence" in sample else aorb("text", "transcription")])) |
|
|
name = "JA_audio_JA_text_180k" |
|
|
|
|
|
ouput_dir = "./datasets/" |
|
|
out_file = 'metadata.csv' |
|
|
os.makedirs(ouput_dir + name, exist_ok=True) |
|
|
folder_path = ouput_dir + name |
|
|
|
|
|
top_db=30 |
|
|
|
|
|
def is_silent(mp3_file, threshold=0.025): |
|
|
if not os.path.exists(mp3_file): |
|
|
return True |
|
|
y, sr = librosa.load(mp3_file, sr=None) |
|
|
rms = librosa.feature.rms(y=y)[0] |
|
|
return all(value < threshold for value in rms) |
|
|
|
|
|
def remove_silence(input_file, output_file, top_db=top_db): |
|
|
y, sr = sf.read(input_file) |
|
|
intervals = librosa.effects.split(y, top_db=top_db) |
|
|
y_trimmed = [] |
|
|
for start, end in intervals: |
|
|
y_trimmed.extend(y[start:end]) |
|
|
if not os.path.exists(output_file): |
|
|
sf.write(output_file, y_trimmed, sr) |
|
|
with open(csv_file2, "a", encoding='utf-8') as f: |
|
|
file_name = os.path.basename(output_file) |
|
|
f.write(file_name + "\n") |
|
|
|
|
|
def process_directory(input_dir, output_dir, top_db=top_db): |
|
|
if not os.path.exists(output_dir): |
|
|
os.makedirs(output_dir) |
|
|
if not os.path.exists(removed_dir): |
|
|
os.makedirs(removed_dir) |
|
|
open(csv_file, 'w', encoding='utf-8').close() |
|
|
open(csv_file2, 'w', encoding='utf-8').close() |
|
|
|
|
|
for filename in os.listdir(input_dir): |
|
|
if filename.endswith(".mp3"): |
|
|
input_file = os.path.join(input_dir, filename) |
|
|
output_file = os.path.join(output_dir, filename) |
|
|
removed_file = os.path.join(removed_dir, filename) |
|
|
|
|
|
if not os.path.exists(output_file): |
|
|
remove_silence(input_file, output_file, top_db) |
|
|
|
|
|
if os.path.exists(output_file) and is_silent(output_file): |
|
|
with open(csv_file, "a", encoding='utf-8') as f: |
|
|
f.write(os.path.basename(output_file) + "\n") |
|
|
shutil.move(output_file, removed_file) |
|
|
|
|
|
if os.path.exists(input_file): |
|
|
os.remove(input_file) |
|
|
|
|
|
input_dir = folder_path |
|
|
output_dir = folder_path + "/trimmed/" |
|
|
removed_dir = folder_path + "/removed/" |
|
|
csv_file = folder_path + "/removed.csv" |
|
|
csv_file2 = folder_path + "/not_removed.csv" |
|
|
|
|
|
min_char = 4 |
|
|
max = 20.0 |
|
|
min = 1.0 |
|
|
|
|
|
char = '[ 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890♬♪♩♫]' |
|
|
special_characters = '[“%‘”~゛#$%&()*+:;〈=〉@^_{|}~"█』『.;:<>_()*&^$#@`, ]' |
|
|
|
|
|
|
|
|
dataset = dataset.cast_column("audio", Audio(sampling_rate=16000)) |
|
|
sentence_map = {} |
|
|
|
|
|
open(os.path.join(folder_path, out_file), 'w', encoding='utf-8').close() |
|
|
|
|
|
for i, sample in tqdm(enumerate(dataset)): |
|
|
if sample["sentence"] != "": |
|
|
audio_sample_name = name + f'_{i}.mp3' |
|
|
audio_path_original = os.path.join(folder_path, audio_sample_name) |
|
|
|
|
|
patterns = [(r"…",'。'), (r"!!",'!'), (special_characters,""), (r"\s+", "")] |
|
|
for pattern, replace in patterns: |
|
|
|
|
|
sample["sentence"] = re.sub(pattern, replace, sample["sentence"]) |
|
|
sample["sentence"] = (neologdn.normalize(sample["sentence"], repeat=1)) |
|
|
if sample["sentence"][-1] not in ["!", "?", "。"]: |
|
|
sample["sentence"] += "。" |
|
|
|
|
|
sentence_length = len(sample["sentence"]) |
|
|
audio_length = len(sample['file_url' if "file_url" in sample else "audio"]["array"]) / sample['file_url' if "file_url" in sample else "audio"]["sampling_rate"] |
|
|
|
|
|
if max > audio_length > min and not re.search(char, sample["sentence"]) and sentence_length > min_char and bool(sample["sentence"]): |
|
|
if not os.path.exists(audio_path_original): |
|
|
sf.write(audio_path_original, sample['file_url' if "file_url" in sample else "audio"]["array"], sample['file_url' if "file_url" in sample else "audio"]["sampling_rate"]) |
|
|
sentence_map[audio_sample_name] = sample['sentence'] |
|
|
|
|
|
print(f"Downloaded {len(sentence_map)} audio files to {folder_path}. Starting silence trimming...") |
|
|
process_directory(input_dir, output_dir) |
|
|
print(f"Silence trimming complete. Trimmed files are in {output_dir}, silent files moved to {removed_dir}.") |
|
|
|
|
|
print(f"Generating final metadata.csv in {folder_path}...") |
|
|
with open(csv_file2, 'r', encoding='utf-8') as f_not_removed: |
|
|
for line in f_not_removed: |
|
|
trimmed_filename = line.strip() |
|
|
|
|
|
if trimmed_filename in sentence_map: |
|
|
sentence = sentence_map[trimmed_filename] |
|
|
with open(os.path.join(folder_path, out_file), 'a', encoding='utf-8') as transcription_file: |
|
|
transcription_file.write(trimmed_filename + ",") |
|
|
transcription_file.write(sentence) |
|
|
transcription_file.write('\n') |
|
|
print(f"Metadata.csv generated for {os.path.join(folder_path, out_file)}.") |