0

I'm using split_on_silence to split a mp3 file to multiple segments:

sound        = AudioSegment.from_mp3(TEST_FILE)
audio_chunks = split_on_silence(sound, min_silence_len=300, keep_silence=50, silence_thresh=-40 )

Is it possible (How can I do it) to get the origin start-stop times for each chunk?

user3668129
  • 4,318
  • 6
  • 45
  • 87

1 Answers1

0

You can make a little change to the code of split_on_silence function:

for range_i, range_ii in pairwise(output_ranges):
        print(">>", timestamp(range_i[0]/1000), timestamp(range_i[1]/1000))
        last_end = range_i[1]
        next_start = range_ii[0]
        if next_start < last_end:
            range_i[1] = (last_end+next_start)//2
            range_ii[0] = range_i[1]

Just add print line or make anything with those variable of range_i which is seemingly the time of those crops. Didn't test the precision but it seem to be ok. This function is /pydub/silence.py in your installation place.

Timestamp is just:

def timestamp(mseconds):
    (hours, mseconds) = divmod(mseconds, 3600)
    (minutes, mseconds) = divmod(mseconds, 60)
    return f"{hours:02.0f}:{minutes:02.0f}:{mseconds:06.3f}"

used with datetime library to be more exact.

Peter.k
  • 1,475
  • 23
  • 40