Search code examples
pythonaudiopydub

How to find where the silence begins from an audio starting from certain timestamp?


I'm trying to index an audio file at a certain non-silent timestamp, say 20 seconds from the beginning (0:20:00), and find the closest timestamp from there that silence begins, say 21 seconds from the beginning (0:21:00).

I'm aware that pydub has detect_silence feature that may be useful but not sure how to index an audio file using timestamp. I'm open to using other libraries as well.


Solution

  • I didn't test it but

    detect_silence gives list with pairs (start, end) so you could run on original audio and later search this list to find first pair which have start bigger than 0:20:00 (or rather bigger than 20_000)

    But it can use slice [start:end] to work only with some part of audio.

    part = audio[20_000:]
    

    It could look like this

    from pydub import AudioSegment
    from pydub.silence import detect_silence
    
    audio = AudioSegment.from_mp3("your_audio.mp3")
    part = audio[20_000:]
    
    chunks = detect_silence(part, ... other options ...)
    
    print('start in part :', chunks[0][0])
    print('start in audio:', chunks[0][0] + 20_000)