22

After investigating to find a solution to the problem, I ended up empty-handed.

I want a context menu option in Windows 10 so that whenever I right click on a MP3 file I have this option to trim silences from start and end of MP3 files. I don't want to remove silences from middle parts of audio files.

Can anyone please provide me with the appropriate command line of FFmpeg to remove silences only from beginning and end of MP3 files?

6 Answers 6

37

Something like this:

ffmpeg -i input.mp3 -af "silenceremove=start_periods=1:start_duration=1:start_threshold=-60dB:detection=peak,aformat=dblp,areverse,silenceremove=start_periods=1:start_duration=1:start_threshold=-60dB:detection=peak,aformat=dblp,areverse" output.flac

You'll need to adjust both thresholds (start_threshold) and durations (start_duration) to your specific case.

12
  • 1
    This works amazingly, but could you explain the part of two codes + areverse? Is it basically scanning from the beginning of the audio, then reversing and scanning from the end? Oct 10, 2018 at 0:41
  • 3
    Yes, because removing silence from end of audio is problematic in sense that you need to know where is end of audio before starting trimming silence. And silenceremove filter can not know end of audio before it actually reach it except if user was tasked to enter it somehow before filtering process.
    – user564335
    Oct 10, 2018 at 10:58
  • 2
    what does durations setting do here? Could you give an example?
    – acman123
    Oct 10, 2018 at 13:28
  • 1
    If duration of non-silence part after silence is more or exact 1 second (durations are set usually in seconds) than it will not be trimmed. Silence is detected using threshold parameter among other options like peak detection and window size. This way you can trim short burst of noises too. I have WIP patch for this filter, adding support for keeping some silence before and after trimming so it is more natural for listening.
    – user564335
    Oct 10, 2018 at 16:49
  • 2
    silenceremove has a new feature that can cut silence from the end: underpop.online.fr/f/ffmpeg/help/silenceremove.htm.gz ➔ This did the trick for me: ffmpeg -i in.mp3 -af "silenceremove=start_periods=1:start_threshold=-60dB:start_silence=1:stop_periods=1:stop_silence=1:detection=peak" out.mp3 Aug 3, 2022 at 13:11
7

For me, the best option for do this work with an mp4 file is this filter:

ffmpeg -i INPUT.mp4 -af "areverse,atrim=start=0.2,silenceremove=start_periods=1:start_silence=0.1:start_threshold=0.02,areverse,atrim=start=0.2,silenceremove=start_periods=1:start_silence=0.1:start_threshold=0.02"
-acodec aac OUTPUT.mp4,

To understanding all filter parts:

  • areverse: Reverse the entire audio.
  • atrim=start=0.2: cut out the first 0.2s to avoid that possible hight stop recording noise in the ending of audio (remember that the audio was reversed).
  • silenceremove=start_periods=1:start_silence=0.1:start_threshold=0.02: This part cut the end silence out.
  • areverse: This part back the audio to original way.
  • atrim=start=0.2 Cut out the first 0.2s to avoid that possible hight noise in the beginning of audio
  • silenceremove=start_periods=1:start_silence=0.1:start_threshold=0.02": This part cut the beginning silence out
1
  • finally something that works... Thanks!!
    – JRichardsz
    Sep 10, 2022 at 16:08
6

FWIW, while the original question specifically requests using FFmpeg, there is a tool called mp3splt which will trim silence (-r) without re-encoding the MP3. This leaves the original quality of your MP3 intact.

5
  • Sounds great! But I want to remove all the silences from the start and end of any mp3 file in spite of its length. Currently it seems that the gui program of mp3splt won't trim audio files less than 2 seconds. Can you give me a commandline that will trim all the silences from the start and end of any mp3 file even if the file is less than 2 seconds long? I also want to replace the original file instead of creating new mp3 files.
    – acman123
    Jan 12, 2019 at 20:40
  • sample mp3 file to test: 1drv.ms/u/s!Atv-gP4NyojQoGrC_EAICpOcWTP4
    – acman123
    Jan 12, 2019 at 20:46
  • mp3splt man page states: To trim using silence detection we need to decode files, so this option can be really slow if used with big files.
    – c24w
    Mar 4, 2020 at 15:22
  • 1
    This option was still way faster for me than figuring out what encoder settings to use etc. with ffmpeg. mp3splt easily accepts a directory as input and you can customize the trim settings. I ended up using mp3splt -r -p min=0.5 ./trimthese which leaves half a second of silence at the beginning and end. In less than an hour, it trimmed a folder containing about 20 four-hour long mp3 files.
    – brandonjp
    May 26, 2020 at 18:17
  • 1
    I would also suggest having a look at sox, which I found very easy to adjust and customise (trimming example from the website: sox in.wav out1.wav silence 1 0.1 1% -1 0.5 1%) Oct 25, 2022 at 13:41
2

I tried, as suggested in this answer:

ffmpeg -i in.aac -af "silenceremove=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" out.aac

... but this threw errors when the file type was not *.flac (Error marking filters as finished errors, not-enough-ram errors).

I got it working by doing:

ffmpeg -i in.aac -af silenceremove=start_periods=1:start_silence=0.1:start_threshold=-50dB,areverse,silenceremove=start_periods=1:start_silence=0.1:start_threshold=-50dB,areverse out.aac
2

I tried all the answers with ffmpeg and mp3split and couldn't do it for my MP3 files. Finally I got the result by usig sox:

sox input.mp3 ./output.mp3 silence 1 0.1 0.1% reverse silence 1 0.1 0.1% reverse

I downloaded it from here because other sources didn't have the lib for mp3s and without it it didn't work.

0

My original wav was

enter image description here

And my requirement was to remove completely the silence from the beginning and the end. This ffmpeg command do it

ffmpeg -i /home/foo/input.wav  \
-af "areverse,atrim=start=0,silenceremove=start_periods=1:start_silence=0:start_threshold=0.02,areverse,atrim=start=0,silenceremove=start_periods=1:start_silence=0:start_threshold=0.02" \
/home/foo/OUT.WAV

The result was that the silence was removed :)

enter image description here

You must log in to answer this question.

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