import json from emotion import get_emotion from genre import get_genre from instrument import get_instrument import os from pydub import AudioSegment import tqdm EXCERPT_LENGTH = 30 * 1000 # 30 seconds in milliseconds MIN_LENGTH = 5 * 1000 # 5 seconds in milliseconds # Save to JSONL format for split in ["test", "train", "valid"]: output_file_path = f'MTG-Jamendo_{split}.jsonl' # Replace with the desired output path emotion = get_emotion(_=split) genre = get_genre(_=split) instrument = get_instrument(_=split) data_samples = emotion + genre + instrument with open(output_file_path, 'w') as outfile: for sample in tqdm.tqdm(data_samples): # json.dump(sample, outfile) audio_path = f"/home/intern-2024-02/dataset/MTG-Jamendo/MTG/audio-low/{sample['audioid'][:-4]}.low.mp3" # Check if the audio file exists if not os.path.exists(audio_path): print(f"File not found: {audio_path}") continue # Load the audio file audio = AudioSegment.from_file(audio_path) # Process and split the audio into 30-second excerpts for i in range(0, len(audio), EXCERPT_LENGTH): end = i + EXCERPT_LENGTH if end < len(audio): excerpt = audio[i:end] else: excerpt = audio[i:] # Discard short audio clips if len(excerpt) < MIN_LENGTH: break end = len(audio) # # Save the excerpt to the same directory with a new name excerpt_path = f"/work/fast_data_yinghao/MTG/audio-low/{sample['audioid'][:-4]}.low_{i//EXCERPT_LENGTH}.wav" # excerpt.export(excerpt_path, format="wav") # Update the sample dictionary with the new audio path new_sample = sample.copy() new_sample["audioid"] = excerpt_path # Dump the new sample to the JSONL file json.dump(new_sample, outfile) outfile.write('\n') # print(excerpt_path) # break # Remove the original audio file # os.remove(audio_path) # outfile.write('\n') outfile.close() # print(f"Data successfully transformed and saved to {output_file_path}")