I have two scripts, one of them splits audio of a certain length, the other one splits audio on every time there is a silent passage. Would it be possible to split the audio
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")