How can i pad wav file to specific length?

蓝咒 提交于 2021-01-05 10:33:06

问题


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


回答1:


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




回答2:


If I understood correctly, the question wants to fix all lengths to a given length. Therefore, the solution will be slightly different:

from pydub import AudioSegment

pad_ms = 1000  # Add here the fix length you want (in milliseconds)
audio = AudioSegment.from_wav('you-wav-file.wav')
assert pad_ms > len(audio), "Audio was longer that 1 second. Path: " + str(full_path)
silence = AudioSegment.silent(duration=pad_ms-len(audio)+1)


padded = audio + silence  # Adding silence after the audio
padded.export('padded-file.wav', format='wav')

This answer differs from this one in the sense that this one creates all audios from the same length where the other adds the same size of silence at the end.



来源:https://stackoverflow.com/questions/52841335/how-can-i-pad-wav-file-to-specific-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!