How to convert to normal audio files?

#2
by Mavisfsew - opened

Hi, Please i downloaded this dataset and I want to extract the files from the parquet but the instructions provided and the stracture of the data is not the same.

Here is the script

import pandas as pd
import os
import csv
import numpy as np

Read the Parquet file into a pandas DataFrame

df = pd.read_parquet('train-00000-of-00001-49c48937e7376123.parquet')

Create folders to store the audio files and metadata

audio_folder = 'Wavs'
os.makedirs(audio_folder, exist_ok=True)

Open the metadata.csv file for writing

metadata_file = 'metadata.csv'
with open(metadata_file, 'w', newline='') as csvfile:
fieldnames = ['line_id', 'text', 'speaker_id']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()

# Iterate over each row in the DataFrame
for idx, row in df.iterrows():
    # Get the audio array and sampling rate
    audio_array = row['audio']['bytes']
    sampling_rate = 48000

    # Convert the audio array to bytes
    audio_bytes = np.array(audio_array, dtype=np.float32).tobytes()

    # Save the audio file
    audio_path = os.path.join(audio_folder, f"{row['line_id']}.wav")
    with open(audio_path, 'wb') as f:
        f.write(audio_bytes)

    # Write metadata to the CSV file
    writer.writerow({
        'line_id': row['line_id'],
        'text': row['text'],
        'speaker_id': row['speaker_id']
    })

Sign up or log in to comment