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.
Sure, you can use the silero library to detect speech and silence in your audio file, and then split the file into 20-minute segments based on the silence detection. Here's an example script that should do what you're looking for:
code
import silero
import numpy as np
import librosa
# Load the audio file
audio, sr = librosa.load('your_audio_file.mp3')
# Initialize Silero VAD
vad = silero.VAD(audio, sr)
# Set the threshold for silence detection
vad.threshold = 0.5 # Adjust this value based on your needs
# Split the audio into 20-minute segments
segments = []
for i in range(0, len(audio), 20 * 60): # 20 minutes * 60 seconds
segment = audio[i:i + 20 * 60]
silence_duration = vad.get_silence_duration(segment)
if silence_duration > 0:
# If there is silence in the current segment, add it to the list
segments.append(segment)
else:
# If there is no silence in the current segment, break out of the loop
break
# Save the segments to separate audio files
for i, segment in enumerate(segments):
librosa.export('segment_{}.mp3'.format(i), segment, sr)
In this script, we first load the audio file using librosa.load(). We then initialize the Silero VAD with the silero.VAD() function, passing in the audio data and the sample rate. We set the threshold for silence detection with the vad.threshold attribute, which should be adjusted based on your specific needs.
Next, we loop over the audio data in chunks of 20 minutes each, using the range() function to iterate over the audio data in increments of 20 minutes. For each chunk, we use the vad.get_silence_duration() function to determine the duration of silence in the chunk, and if there is silence in the chunk, we add it to a list of segments. If there is no silence in the chunk, we break out of the loop.
Finally, we save each segment to a separate audio file using the librosa.export() function. The sr variable, which is the sample rate of the audio file, is passed to the librosa.export() function to ensure that the exported audio files have the correct sample rate.
Note that the silero library requires the numpy library to be installed, so make sure you have that installed before running this script. Additionally, the librosa library is not included in the silero package, so you'll need to install that separately as well.