Pydub - combine split_on_silence with minimum length / file size

随声附和 提交于 2019-11-30 14:38:01

My advice is to use pydub.silence.split_on_silence() and then recombine the segments as needed so that you have files that are roughly the size you're targeting.

something like

from pydub import AudioSegment
from pydub.silence import split_on_silence

sound = AudioSegment.from_file("/path/to/file.mp3", format="mp3")
chunks = split_on_silence(
    sound,

    # split on silences longer than 1000ms (1 sec)
    min_silence_len=1000,

    # anything under -16 dBFS is considered silence
    silence_thresh=-16, 

    # keep 200 ms of leading/trailing silence
    keep_silence=200
)

# now recombine the chunks so that the parts are at least 90 sec long
target_length = 90 * 1000
output_chunks = [chunks[0]]
for chunk in chunks[1:]:
    if len(output_chunks[-1]) < target_length:
        output_chunks[-1] += chunk
    else:
        # if the last output chunk is longer than the target length,
        # we can start a new one
        output_chunks.append(chunk)

# now your have chunks that are bigger than 90 seconds (except, possibly the last one)

Alternatively, you can use pydub.silence.detect_nonsilent() to find the ranges and make your own decisions about where to slice the original audio

note: I also posted this on a similar/duplicate github issue

The solution is to use mp3splt instead: http://mp3splt.sourceforge.net/mp3splt_page/documentation/man.html

-t TIME[>MIN_TIME] Time mode. This option will create an indefinite number of smaller files with a fixed time length specified by TIME (which has the same format described above). It is useful to split long files into smaller (for example with the time length of a CD). Adjust option (-a) can be used to adjust splitpoints with silence detection. >MIN_TIME can be used to specify the theoretical minimum track length of the last segment; it allows avoiding to create very small files as the last segment. Make sure to quote the argument when using MIN_TIME - "TIME>MIN_TIME".

Then, it can be used in python like this:

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