日本語

Detecting Silent Sections in a Video with FFmpeg silencedetect

​​FFmpeg​audio,command line,video

The silencedetect filter in FFmpeg lists the sections of a video where the sound drops out, with start and end times in seconds. It is useful when you want to know where the pauses are and how long each one lasts before cutting out silence (tightening the edit by removing gaps) from a talking video such as a commentary or a talk.

In a test environment, I made a 30-second video with four silent sections and searched it with silencedetect. With the default settings, only two sections were found. The other two were a short pause and a section where a quiet noise remained. With noise=-50dB:d=0.5, all four were found.

Note

The test environment was Ubuntu 24.04.5, FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5), on a virtual machine with 4 CPU cores. The audio was synthetic: pink noise generated by FFmpeg, band-limited to 300–3000 Hz, with its level varied (48 kHz stereo, AAC 192 kbps). I did not test recorded speech or music, audio codecs other than AAC, or actually cutting out the silent sections in an edit.

The test video

I made the audio of a 30-second video as follows. The "silent" sections have their amplitude set to 0.

Section Content
0–5 s Sound
5–8 s Silence (3 s)
8–14 s Sound
14–14.8 s Silence (a short 0.8 s pause)
14.8–20 s Sound
20–24 s Quiet noise (about 1/100 of the sound level)
24–27 s Sound
27–30 s Silence (to the end)

The peak level measured with the astats filter was about -17.5 to -17.9 dB for each of the four sound sections, and -58.2 dB for the quiet noise section. I made the quiet noise section to represent a recording where a small sound stays in the background even when nobody is speaking.

First, run with the defaults

silencedetect is a filter that prints its results to the log without changing the audio. You don't need to create an output file, so the output is discarded with -f null -.

bash
ffmpeg -hide_banner -nostats -i talk.mp4 -af silencedetect -f null - 2>&1 | grep silencedetect

The results go to the log (standard error), so 2>&1 merges them into standard output, and grep filters them. The output was:

text
[silencedetect @ 0x…] silence_start: 5.0134
[silencedetect @ 0x…] silence_end: 7.99971 | silence_duration: 2.98631
[silencedetect @ 0x…] silence_start: 27.0081
[silencedetect @ 0x…] silence_end: 30.016 | silence_duration: 3.00787

The 0x… part changes on every run, so I have left it out.

silence_start is where the silence begins, silence_end is where it ends, and silence_duration is its length. All of them are in seconds. Only two sections were found, 5–8 s and 27–30 s. The 0.8-second pause and the quiet noise section did not appear.

The silencedetect results are logged at info level, so adding -loglevel error hides them. In my test, -loglevel error produced 0 lines of silencedetect output, and -loglevel info produced 8 lines.

Two options decide what counts as silence: noise and d

The FFmpeg filter documentation lists three options.

Option Meaning Default
noise (n) Sounds quieter than this level count as silence. Written in dB, such as -50dB, or as an amplitude ratio -60dB (0.001)
duration (d) Report silence only when it lasts at least this many seconds 2
mono (m) Check each channel separately disabled

The 0.8-second pause was missed because the default for d is 2 seconds. The quiet noise section has a peak of -58.2 dB, which is above the default noise of -60 dB. Looking at the silencedetect source code in FFmpeg 6.1.1, the filter checks each sample to see whether its absolute value is below noise. If even one sample in a section is above noise, the silence is broken at that point.

So I used noise=-50dB:d=0.5.

bash
ffmpeg -hide_banner -nostats -i talk.mp4 -af silencedetect=noise=-50dB:d=0.5 -f null - 2>&1 | grep silencedetect

All four sections were found (the [silencedetect @ …] prefix is left out):

text
silence_start: 5.01333
silence_end: 7.99994 | silence_duration: 2.9866
silence_start: 14.016
silence_end: 14.8053 | silence_duration: 0.789333
silence_start: 20.0107
silence_end: 24 | silence_duration: 3.98933
silence_start: 27.008
silence_end: 30.016 | silence_duration: 3.008
Graph of the peak level in 0.05-second steps for the 30-second video. The sound sections are around -20 dB. The 5–8 s, 14–14.8 s and 27–30 s sections are at 0 amplitude. The 20–24 s section moves up and down around the -60 dB line and sometimes goes slightly above it. Bars for the four silent sections, 2.99 s, 0.79 s, 3.99 s and 3.01 s, lie over the four parts that are below the -50 dB line. (The labels in this image are in Japanese.)
Peak level in 0.05-second steps, with the silent sections found by noise=-50dB:d=0.5. The noise in 20–24 s slightly exceeds the default -60 dB line.

The times are 5.01333 s rather than exactly 5 s because the sound changes happen on boundaries of 1024 samples (about 0.021 s). The calculated boundaries are 5.01333 s, 14.016 s, 20.0107 s and 27.008 s, which match the start times that were found.

noise can also be written as an amplitude ratio. -50 dB corresponds to 0.00316, and noise=0.00316:d=0.5 gave the same result.

If noise is too large, parts with sound are also treated as silence

I kept d=0.5 and changed noise to compare.

noise Sections found
-60 dB (default) 3 sections. The 0.8-second pause is found, but the noise section is not
-50 dB, -40 dB, -30 dB 4 sections. Start and end times differ by 0.002 s or less
-25 dB 4 sections. The noise section becomes a bit wider, 19.99–24.01
-20 dB 5 sections. The sound at 3.54–4.18 s is also treated as silence, and the edges of the other sections shift by up to 0.2 s

For this video, any value from -50 dB to -30 dB gave the same sections. When I split the sound sections into 0.05-second steps and checked the peak levels, they moved between -24.3 dB and -17.5 dB. When noise was set close to that range, it also picked up places where the sound had become quiet.

When choosing noise, measure the loudness of the parts where nobody is speaking and the parts where someone is speaking with astats first. That makes it easier to pick a value in between.

bash
ffmpeg -hide_banner -nostats -ss 20.1 -to 23.9 -i talk.mp4 -vn -af astats=measure_perchannel=none:measure_overall=Peak_level+RMS_level -f null -

End time for silence that runs to the end

The silence from 27 s to the end was shown as silence_end: 30.016. ffprobe reports the audio length as 30.000000 s. In the source code, if the silence is still continuing when the input ends, the end time printed as silence_end is the end time of the last audio frame that was read. This can be slightly later than the length of the video, so when you use the times for cutting, clamp them to the video length.

For silence on only one side, use mono=true

I also tested a video where the left channel was silent from 10 to 15 s and the right channel had sound the whole time.

bash
ffmpeg -hide_banner -nostats -i left-silent.mp4 -af silencedetect=noise=-50dB:d=0.5:mono=true -f null - 2>&1 | grep silencedetect

With mono=false (the default), nothing was printed. By default, the samples from all channels are checked together, so if the right channel has sound, the silence is not detected. With mono=true, the results were printed with the channel number.

text
[silencedetect @ 0x…] channel: 0 | silence_start: 10.0053
[silencedetect @ 0x…] channel: 0 | silence_end: 15.0187 | silence_duration: 5.01333

channel: 0 is the left channel.

Listing the silent sections as a table

Reading the log directly is tedious, so I wrote a Bash script that prints the start, end and length as a tab-separated table. Since the video track is not used, -vn tells FFmpeg not to read it.

bash
#!/bin/bash
# Usage: bash list.sh video-file
# Prints each silent section as "start end length" (seconds, tab-separated), one per line
ffmpeg -hide_banner -nostats -i "$1" -vn -af silencedetect=noise=-50dB:d=0.5 -f null - 2>&1 \
  | awk '/silence_start/ {s=$NF} /silence_end/ {printf "%s\t%s\t%s\n", s, $5, $8}'
text
5.01333	7.99994	2.9866
14.016	14.8053	0.789333
20.0107	24	3.98933
27.008	30.016	3.008

When cutting out silence, what you keep is the gaps between the silent sections, that is, the sections with sound. Based on the output of the script above, I also wrote a script that prints those gaps. If the end of the last silent section is past the end of the video, the script stops without adding a sound section after it.

bash
#!/bin/bash
# Usage: bash sound.sh video-file
# Prints the gaps between silent sections, as sound sections "start end" (seconds)
len=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$1")
bash "$(dirname "$0")/list.sh" "$1" | awk -v len="$len" -v prev=0 -F'\t' '
  { if ($1 > prev) printf "%s\t%s\n", prev, $1; prev = $2 }
  END { if (prev < len) printf "%s\t%s\n", prev, len }'
text
0	5.01333
7.99994	14.016
14.8053	20.0107
24	27.008

Adding -vn makes it finish in about a tenth of the time

Without -vn, FFmpeg reads and decodes the video even though it isn't used. For a 30-second 1920×1080 video, measured three times each, the run without -vn took 1.48–1.72 seconds, and the run with -vn took 0.14 seconds every time. The sections found were the same in both cases.

Writing to a file with ametadata, and its caveats

silencedetect also attaches its results as frame metadata (such as lavfi.silence_start). Adding ametadata after it lets you write that metadata to a file.

bash
ffmpeg -hide_banner -nostats -loglevel error -y -i talk.mp4 -vn -af silencedetect=noise=-50dB:d=0.5,ametadata=mode=print:file=silence.txt -f null -

The end of the written file looked like this:

text
frame:1125 pts:1152000 pts_time:24
lavfi.silence_end=24
lavfi.silence_duration=3.98933
frame:1289 pts:1319936 pts_time:27.4987
lavfi.silence_start=27.008

The silence that runs to the end has only silence_start and no silence_end. In the source code, the silence_end printed after the input ends is only shown in the log and is not attached as metadata. If you also need to handle videos that end in silence, reading the log is simpler.

Also, silence_start is attached to the frame that contains the moment the silence reaches the d duration. In this example, the silence starting at 27.008 s was attached to the frame starting at 27.4987 s. With mono=true, the keys are numbered from 1, such as lavfi.silence_start.1, so the numbers do not match the channel: 0 in the log.

​Posted by 86