28

I have MP3 files that sometimes have silence at the end. I would like to remove this silence automatically. From what I can tell, it is "perfect" silence (0 amplitude), not background noise. The length of the content and the silence varies.

I found some other questions about cropping to the first 30 seconds or cropping to X and X+N seconds using ffmpeg. I would think I could use a similar approach, as long as I have a way to find when the silence starts. How would I do that programatically?

For example, one possible solution would be to have a command that finds the beginning of the "silence". I'd expect a sequence like this

end=$(ffmpeg some-command-to-find-start-of-silence)
ffmpeg -t "$end" -acodec copy -i inputfile.mp3 outputfile.mp3

The solution does not have to use ffmpeg, but it does need to be available on Ubuntu.

4
  • Are you truly only interested in perfect silence? That's very rare, with dithering from 24-bit masters, and lossy compression. FFMPEG is the way to go, in any case. Commented Aug 6, 2012 at 19:06
  • @Brad We're basically working around a bug in a recording service we use. It seems to always give this "perfect" silence when the problem occurs. Commented Aug 6, 2012 at 19:08
  • Sounds like you might be interested in the silencedetect FFmpeg audio filter. Commented Aug 6, 2012 at 19:21
  • @blahdiblah That might just do it. Could you turn that comment into an answer, please? Commented Aug 6, 2012 at 19:24

4 Answers 4

46
sox inputfile.mp3 outputfile.mp3 silence 1 0.1 0.1% reverse silence 1 0.1 0.1% reverse

This will trim any silence longer than 0.1 second from your file. If you're only concerned about trimming the end, this can be simplified to:

sox inputfile.mp3 outputfile.mp3 reverse silence 1 0.1 0.1% reverse

A detailed look into how sox's silence works can be found here.

Sign up to request clarification or add additional context in comments.

1 Comment

The solution works, but your explanation is wrong. The original author said 'trim silence (anything less than 1% volume) until we encounter sound lasting more than 0.1 seconds in duration.'.
12

You probably are looking for a lossless solution, i.e. one that does not require re-encoding (which reduces quality).

I believe mp3splt is what you are looking for. It can be used from command line and GUI.

sudo aptitude install mp3splt mp3splt-gtk

should work on Debian and Ubuntu.

From the man page:

You can also try to split files automatically with silence detection (see -s option), trim files using silence detection (see -r option), or by a fixed time length (see -t option)

5 Comments

What about silence in the middle of the file?
Yes but split produce multiple files, I need to remove all silence from one file.
Join them back afterwards?
Just cut dead silence in my lecture records - this silence was added by a hardware problem.
Then splitting the file, and joining sounds exactly right, doesn't it?
8

Have a look at the silencedetect FFmpeg audio filter:

Detect silence in an audio stream.

This filter logs a message when it detects that the input audio volume is less or equal to a noise tolerance value for a duration greater or equal to the minimum detected noise duration.

The printed times and duration are expressed in seconds.

It has parameters to adjust how quiet something has to be to be considered silence, and how long the silence needs to be to be noted.

2 Comments

The silenceremove filter is documented here ffmpeg.org/ffmpeg-filters.html#toc-silenceremove This answer might prove useful too: stackoverflow.com/a/29411973/1717535
@FabienSnauwaert, thanks for a valuable comment. Actually it should be an accepted answer :)
1

Detecting silence at the beginning and end of a file, and extracting times where silence at the beginning ends (variable Ab for -ss) and at the end begins (Ae for -to). Apparently you can't extract numerical values with filters alone. I use a Batch script for that.

@echo off
set In="%1"
set FFmpeg_output=FFmpeg -i %In% -af "silencedetect,ametadata=print:file=-" -f null - -v -8
set i=0
SetLocal EnableDelayedExpansion
for /f "skip=4 delims== tokens=2" %%T in ('%FFmpeg_output%') do (
    if !i!==0 (set Ab=%%T) else (set Ae=%%T)
    set /a i+=1
)
echo %Ab%
echo %Ae%
pause
:: Cutting without re-encoding.
FFmpeg -i %In% -ss %Ab% -to %Ae% -c copy "%~pn1_%~x1"

Notes:

print:file=- Print to stdout.

-v -8 Loglevel to show the output of the filters alone.

-f null - No output file.

i= A counter enables assigning to different variables.

skip Lines to ignore.

delims== tokens= When processing the remaining output, it stops at = and uses the part after it (2).

In case of video+audio, add: -map 0:a when filtering and extracting, -itsoffset %Ab% -i when merging.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.