9

I've got multiple audiobooks that are stored in large mp3s. And I'm trying to split these large mp3s into multiple smaller files.

I've found a tool that can detect silence in audio files and split audio files based on this "delimiter".

Here is an example:

sox -V3 audiobook.mp3 audiobook_part_.mp3 \
silence 1 0.5 0.1% 1 0.5 0.1% : newfile : restart

This will basically split audiobook.mp3 into audiobook_part_001.mp3, audiobook_part_002.mp3, ... where silence >= 0.5 seconds.

Now the problem is that this command not only splits the file but it also removes the silence.

Therefore when you play the new files in a playlist the tracks/paragraphs sound squeezed together.

So how do you tell sox to only split the file but to keep the silence (at the end of each track)?

1
  • 3
    Consider using mp3splt instead.
    – Pablo A
    Commented Aug 8, 2017 at 18:51

2 Answers 2

13

You can preserve all the silences in the split parts with some small changes. Starting with your original command:

silence 1 0.5 0.1%   1 0.5 0.1% 

The first triplet of values means removes silence, if any, at the start until .5 seconds of sound above .1%. The second triplet means stop when there is at least .5 seconds of silence below .1%. The rest of your command, : newfile : restart, then starts a new output file and begins again to look for sound at the start. So the first file ends when the silence begins, and the second file will start when the silence ends.

The simplest option available to improve this is silence -l. It will preserve the .5 seconds of silence that triggered the end of file. Unfortunately, any longer silence will be removed because it is the start of the next file. An easy way to keep a longer gap is to combine -l with a longer detection time, eg 2 seconds:

silence -l  1 0.5 0.1%   1 2.0 0.1%

You will now only split if there is at least 2 seconds of silence, but you will preserve the first 2 seconds of the gap. To avoid losing all silence, simply remove the detection of silence at the start. You need to replace the triplet by a single 0:

silence -l  0   1 2.0 0.1%

If you want to play with simple sound files to see how sox handles situations, you can easily create 2 sound files, one consisting of 1 second of a tone, and one consisting of 1 second of silence, then join them together as you wish before presenting the result as input to the silence effect. For example, create:

sox -n gap.wav   trim 0 1
sox -n tone.wav  synth 1.001t sine C5

then join gap-tone-gap-tone and create out.wav using your effect and listen to the result:

sox gap.wav tone.wav gap.wav tone.wav out.wav silence 1 0.5 0.1%
play out.wav
2
  • 1
    "The first triplet of values means removes silence, if any, at the start until .5 seconds of sound above .1%." Does that mean that the first 0.5 seconds of the original audio file will be lost, even if it contains audio we want to keep? Commented Apr 25, 2017 at 6:16
  • 1
    @rudolfbyker You shouldn't have any unexpected loss. The sound above silence is preserved, you don't lose .5 seconds of it. I've updated my answer with how you can do simple tests to see what might happen.
    – meuh
    Commented Apr 25, 2017 at 11:52
1

I would use sox pad to add back silence to the beginning and end of each split file. At the end, it won't be a single sox command that does everything in a single go, but it's much more straight forward, and gives you some extra control such as choosing the length of padded silence.

See this answer on how to use sox pad. E.g.

files="*.wav"
for f in $files
do
  sox "$f" "${f%.*}-pad.wav" pad 2 3
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .