I want to write a script in Python that split an audio file with speech in it.
I want to split my audio file in pieces of 20 minutes each. I don't want to split the audio file on a speech, so for that you have to detect an accurate VAD using Silero VAD and the closest to each 20 minutes segments.
The input audio format is a mp3 192K.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To split an audio file with speech into pieces of 20 minutes each, you can use the librosa library in Python to detect speech using the Silero VAD model, and then split the audio file into segments closest to 20-minute intervals. Here's a sample script to get you started:
python
import librosa
import numpy as np
# Load audio file
audio, sr = librosa.load('audio_file.mp3')
# Load Silero VAD model
vad = librosa.models.VAD_Silero(model='sre16_vad_low_delay')
# Detect speech regions using Silero VAD
speech_regions = vad.process(audio)
# Split speech regions into segments of 20 minutes or less
segments = []
for i, region in enumerate(speech_regions):
start, end = region[0], region[1]
segment_duration = end - start
if segment_duration <= 20 * 60:
segments.append((start, end))
else:
# Split long segments into shorter segments
while segment_duration > 20 * 60:
midpoint = (start + end) // 2
segments.append((start, midpoint))
segments.append((midpoint, end))
start = midpoint
end = midpoint + segment_duration // 2
segment_duration = segment_duration // 2
# Save segments as separate audio files
for i, segment in enumerate(segments):
start, end = segment
segment_audio = audio[start:end]
librosa.output.write_wav('segment_{:d}.wav'.format(i), segment_audio, sr)
In this script, we first load the audio file using librosa.load() and set the sampling rate to sr. We then load the Silero VAD model using librosa.models.VAD_Silero() and process the audio using vad.process(audio). This gives us a list of speech regions, where each region is represented by a tuple containing the start and end time of the speech segment.
We then iterate through the speech regions and split them into segments of 20 minutes or less. If a segment is longer than 20 minutes, we split it into shorter segments by finding the midpoint and splitting the segment into two parts. We repeat this process until all segments are 20 minutes or less.
Finally, we save each segment as a separate audio file using librosa.output.write_wav(). The file name for each segment includes the segment number (starting from 0) and the .wav extension.
Note that this script assumes that the input audio file is in the format of 16-bit signed integer PCM data, which is the format used by the Silero VAD model. If your audio file is in a different format, you may need to convert it before running this script. Additionally, you may need to adjust the model parameters for the Silero VAD model to optimize its performance on your specific audio file.