Search code examples
pythonpython-3.xwav

How can i pad wav file to specific length?


I am using wave files for making deep learning model they are in different length , so i want to pad all of them to 16 sec length using python


Solution

  • Using pydub:

    from pydub import AudioSegment
    
    pad_ms = 1000  # milliseconds of silence needed
    silence = AudioSegment.silent(duration=pad_ms)
    audio = AudioSegment.from_wav('you-wav-file.wav')
    
    padded = audio + silence  # Adding silence after the audio
    padded.export('padded-file.wav', format='wav')
    

    AudioSegment objects are immutable