32

I am trying to use the following command with the latest ffmpeg build to remove silence from my .mp3 files:

ffmpeg -i SILENCE.mp3 -af silencedetect=n=-50dB:d=1 -y -ab 192k  SILENCE_OUT.mp3

However, the following output is produced:

ffmpeg version N-66154-g1654ca7 Copyright (c) 2000-2014 the FFmpeg developers
  built on Sep  5 2014 22:10:38 with gcc 4.8.3 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-av
isynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enab
le-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --
enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-lib
modplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrw
b --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinge
r --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --en
able-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis
 --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-
libx265 --enable-libxavs --enable-libxvid --enable-decklink --enable-zlib
  libavutil      54.  7.100 / 54.  7.100
  libavcodec     56.  1.100 / 56.  1.100
  libavformat    56.  4.100 / 56.  4.100
  libavdevice    56.  0.100 / 56.  0.100
  libavfilter     5.  1.100 /  5.  1.100
  libswscale      3.  0.100 /  3.  0.100
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  0.100 / 53.  0.100
Input #0, mp3, from 'SILENCE.mp3':
  Metadata:
    title           : Snowblind (Featuring Tasha Baxter)
    artist          : Au5
    album           : Snowblind (Featuring Tasha Baxter)
    genre           : Electronica
    performer       : Au5
    track           : 1/1
    date            : 2014
    album_artist    : Au5,Tasha Baxter
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    encoder         : Lavf55.42.100
  Duration: 00:05:50.80, start: 0.025057, bitrate: 192 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 192 kb/s
Output #0, mp3, to 'SILENCE_OUT.mp3':
  Metadata:
    TIT2            : Snowblind (Featuring Tasha Baxter)
    TPE1            : Au5
    TALB            : Snowblind (Featuring Tasha Baxter)
    TCON            : Electronica
    TPE3            : Au5
    TRCK            : 1/1
    TDRL            : 2014
    TPE2            : Au5,Tasha Baxter
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    TSSE            : Lavf56.4.100
    Stream #0:0: Audio: mp3 (libmp3lame), 44100 Hz, stereo, s16p, 192 kb/s
    Metadata:
      encoder         : Lavc56.1.100 libmp3lame
Stream mapping:
  Stream #0:0 -> #0:0 (mp3 (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
[silencedetect @ 0000000004398f40] silence_start: -0.00628118
[silencedetect @ 0000000004398f40] silence_end: 3.21413 | silence_duration: 3.22
041
[silencedetect @ 0000000004398f40] silence_start: 343.844
[libmp3lame @ 00000000043b2940] Trying to remove 1152 samples, but the queue is
empty
size=    8223kB time=00:05:50.79 bitrate= 192.0kbits/s
video:0kB audio:8222kB subtitle:0kB other streams:0kB global headers:0kB muxing
overhead: 0.011485%

The generated audio file however still has the original length without any silence removed. See the following images: http://i.imgur.com/nVxe5lX.png

Any help is appreciated!

EDIT: Alright, silence detect is only DETECTING the silence. Not removing it. I will try to post a solution for this.

Slyx
  • 113
  • 7
Zahlii
  • 818
  • 1
  • 7
  • 17
  • if anyone is interested in removing silence from video files you may find my [vwrt](https://pypi.org/project/vwrt/) package useful – evn Jan 03 '22 at 03:05

5 Answers5

59

Use the silenceremove filter. This removes silence from the audio track only - it will leave the video unedited, i.e., things will go out of sync

Its arguments are a little cryptic.

An example

ffmpeg -i input.mp3 -af silenceremove=1:0:-50dB output.mp3

This removes silence

  • at the beginning (indicated by the first argument 1)
  • with minimum length zero (indicated by the second argument 0)
  • silence is classified as anything under -50 decibels (indicated by -50dB).

Documentation: FFMPEG silence remove filter

Also anyone looking to find the right value to classify silence as may wish to look into normalising their input audio volume to 0dB first, to do this in ffmpeg see this answer.

Edit

As pointed out by @mems, to detect whether your version of ffmpeg has the filter run

ffmpeg -hide_banner -filters | grep silenceremove

if you have the filter it'll output something like

silenceremove A->A Remove silence

ElDog
  • 1,277
  • 11
  • 19
  • 2
    To know if this filter is available: `ffmpeg -hide_banner -filters | grep silenceremove` should display `... silenceremove A->A Remove silence.` – mems Apr 07 '15 at 10:30
  • 11
    Please note, that `-af silenceremove` will re-encode your audio files and thus change bitrate. You cannot use `-acodec copy` to keep the original quality. – nixda Aug 28 '15 at 12:21
  • 4
    How do you remove silence from both the beginning and end of the audio (but not the middle)? I tried following the documentation with stop_periods but it either did nothing to the file or made it corrupt with 0:00 length – CreativiTimothy Oct 09 '18 at 22:38
  • 4
    how do you remove parts of a video that are silent? – zero_cool May 04 '19 at 03:36
  • Adding the detection peak should make the silence detection more dynamic, like `-af silenceremove=1:0:-50dB:detection=peak` – loretoparisi May 22 '19 at 10:37
  • How to use the decoded file simply as your reference to find the silences, but perform the actual cuts on the encoded file? – johny why Apr 30 '20 at 19:13
14

ffmpeg silence detect only detects the silence. One has to scan the ffmpeg output and cut the mp3 file.

In theory, this would be done as:

ffmpeg -i INPUT.mp3 -af silencedetect=n=-50dB:d=1

and monitoring for output in form of:

[silencedetect @ 0000000004970f80] silence_start: -0.00154195
[silencedetect @ 0000000004970f80] silence_end: 3.20435 | silence_duration: 3.2059
...
[silencedetect @ 0000000004970f80] silence_start: 343.84

And, cutting start and end silence:

ffmpeg -i INPUT.mp3 -ss 3.20435 -t (343.84-3.20435)

I ended up writing a small Java program which does it. Hints:

  • ffmpeg writes to stderr. This means, you need to use ProcessBuilder and redirectErrorStream(true).
  • secondly, you need to extract the silence_start and silence_end information.
  • then you might use the timestamps to cut the video

Following code may be helpful: Using Java and FFMPEG with silencedetect to remove audio silence

Zahlii
  • 818
  • 1
  • 7
  • 17
  • Hi that command doesn't appear to work in the shell. What are the parenthesis doing? `$ echo ffmpeg -i INPUT.mp3 -ss 3.20435 -t (343.84-3.20435)` yields `-bash: syntax error near unexpected token \`('`. – Chloe May 25 '17 at 16:46
  • 1
    You need to actually calculate the difference and then replace the expression in the parentheses with the result – Zahlii May 27 '17 at 06:54
  • you might also use `bc` (bash only does integers), so the section `(343.84-3.20435)` becomes `$(echo 343.84-3.20435 | bc)`. – mariotomo Aug 05 '18 at 03:03
  • 2
    Actually I get a `At least one output file must be specified` for this command. – loretoparisi May 22 '19 at 09:48
  • @loretoparisi I'd say the examples are just there to show roughly how to use `-ss` and -`t`, rather than to to be used as-is. You need to add an output file to the command line, e.g. `ffmpeg -i INPUT.mp3 -ss .... -t .... OUTPUT.wav` – mwfearnley Oct 13 '21 at 12:13
  • 1
    @loretoparisi it should be `ffmpeg -i full-0001.wav -af silencedetect=n=-50dB:d=1 -f null -` – Programus Dec 06 '22 at 11:16
8

After reading the FFmpeg silenceremove documentation, this is how you remove silence at the beginning and end of an audio file (keeps silence in the middle).

ffmpeg -i "INPUT.mp3" -af silenceremove=start_periods=1:stop_periods=1:detection=peak "OUTPUT.mp3"
Elijah
  • 1,814
  • 21
  • 27
5

As indicated by other posters, silencedetect doesn't remove anything. To remove all silence (here lower than -30 dB) from an audio file, and leave 2 second gaps between fragments, use the following.

ffmpeg -i inputfile.mp3 -af "silenceremove=start_periods=1:stop_periods=-1:start_threshold=-30dB:stop_threshold=-30dB:start_silence=2:stop_silence=2" outputfile.mp3
Joris R
  • 51
  • 1
  • 1
2

From the following way can remove silence from the beginning and end of the file.

ffmpeg -i input.mp3 -af "silenceremove=start_periods=1:start_duration=1:start_threshold=-50dB:detection=peak,aformat=dblp,areverse,silenceremove=start_periods=1:start_duration=1:start_threshold=-50dB:detection=peak,aformat=dblp,areverse" input_silence_removed.mp3
Dharman
  • 30,962
  • 25
  • 85
  • 135
Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36
  • I got this message **Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)** – JRichardsz Sep 10 '22 at 13:42