Skip to content

Instantly share code, notes, and snippets.

@JeffPlsFix
Last active December 4, 2023 09:56
Show Gist options
  • Save JeffPlsFix/f4c54f68e8a9b3d4c8093dccd7ad0664 to your computer and use it in GitHub Desktop.
Save JeffPlsFix/f4c54f68e8a9b3d4c8093dccd7ad0664 to your computer and use it in GitHub Desktop.
Trim wav file from end with scipy
'''
Modified version of "Trim wave file example with scipy" from link below
https://www.janpijpers.com/trim-wave-file-example-with-scipy/
Trims a wav file from beginning and end, specified in seconds
'''
from scipy.io import wavfile
def trim_wav( originalWavPath, newWavPath , start, end ):
'''
:param originalWavPath: the path to the source wav file
:param newWavPath: output wav file * can be same path as original
:param start: amount to trim from start, in seconds
:param end: amount to trim from end, in seconds
:return:
'''
sampleRate, waveData = wavfile.read( originalWavPath )
startSample = int( start * sampleRate )
endSample = int( end * sampleRate )
wavfile.write( newWavPath, sampleRate, waveData[startSample:-endSample])
'''
trim 0.1 seconds from the end of wav file:
'''
filepath = "foo/bar.wav"
trim_wav(filepath, filepath.replace(".wav", ".wav"), 0, 0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment