- Notifications
You must be signed in to change notification settings - Fork286
Python script that slices audio with silence detection
License
openvpi/audio-slicer
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Python script that slices audio with silence detection
This is the 2.0 version of audio slicer, which provides:
- Great improvements on speed (400x compared to previous 15x)
- Enhanced slicing logic with fewer errors
The 1.0 version can be foundhere.
GUI version can be foundhere.
This script uses RMS (root mean score) to measure the quiteness of the audio and detect silent parts. RMS values of each frame (frame length set ashop size) are calculated and all frames with an RMS below thethreshold will be regarded as silent frames.
Once the valid (sound) part reachedmin length since last slice and a silent part longer thanmin interval are detected, the audio will be sliced apart from the frame(s) with the lowest RMS value within the silent area. Long silence parts may be deleted.
pip install numpy
pip install librosapip install soundfile
or
pip install -r requirements.txt
importlibrosa# Optional. Use any library you like to read audio files.importsoundfile# Optional. Use any library you like to write audio files.fromslicer2importSliceraudio,sr=librosa.load('example.wav',sr=None,mono=False)# Load an audio file with librosa.slicer=Slicer(sr=sr,threshold=-40,min_length=5000,min_interval=300,hop_size=10,max_sil_kept=500)chunks=slicer.slice(audio)fori,chunkinenumerate(chunks):iflen(chunk.shape)>1:chunk=chunk.T# Swap axes if the audio is stereo.soundfile.write(f'clips/example_{i}.wav',chunk,sr)# Save sliced audio files with soundfile.
The script can be run with CLI as below:
python slicer2.py audio [--out OUT] [--db_thresh DB_THRESH] [--min_length MIN_LENGTH] [--min_interval MIN_INTERVAL] [--hop_size HOP_SIZE] [--max_sil_kept MAX_SIL_KEPT]
whereaudio
refers to the audio to be sliced,--out
defaults to the same directory as the audio, and other options have default values as listedhere.
Sampling rate of the input audio.
The RMS threshold presented in dB. Areas where all RMS values are below this threshold will be regarded as silence. Increase this value if your audio is noisy. Defaults to -40.
The minimum length required for each sliced audio clip, presented in milliseconds. Defaults to 5000.
The minimum length for a silence part to be sliced, presented in milliseconds. Set this value smaller if your audio contains only short breaks. The smaller this value is, the more sliced audio clips this script is likely to generate. Note that this value must be smaller than min_length and larger than hop_size. Defaults to 300.
Length of each RMS frame, presented in milliseconds. Increasing this value will increase the precision of slicing, but will slow down the process. Defaults to 10.
The maximum silence length kept around the sliced audio, presented in milliseconds. Adjust this value according to your needs. Note that setting this value does not mean that silence parts in the sliced audio have exactly the given length. The algorithm will search for the best position to slice, as described above. Defaults to 1000.
This script runs over 400x faster than real-time on an Intel i7 8750H CPU. Speed may vary according to your CPU and your disk. ThoughSlicer
is thread-safe, multi-threading does not seem neccessary due to the I/O bottleneck.
About
Python script that slices audio with silence detection
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.