using FFMPEG with silencedetect to remove audio silence
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
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"
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