How to trim silence only from beginning and end of mp3 files using FFMpeg?

4

3

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?

acman123

Posted 2018-09-28T10:02:55.580

Reputation: 383

Answers

7

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.

Paul B. Mahol

Posted 2018-09-28T10:02:55.580

Reputation: 560

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? – CreativiTimothy – 2018-10-10T00:41:40.507

1Yes, 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. – Paul B. Mahol – 2018-10-10T10:58:44.443

what does durations setting do here? Could you give an example? – acman123 – 2018-10-10T13:28:12.830

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. – Paul B. Mahol – 2018-10-10T16:49:38.370

What is "WIP patch"? – acman123 – 2018-10-10T18:47:30.570

Work in progress code additions to silenceremove filter to extend its functionality. – Paul B. Mahol – 2018-10-11T07:12:03.793

0

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

Idcmp

Posted 2018-09-28T10:02:55.580

Reputation: 109

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 – 2019-01-12T20:40:26.343

sample mp3 file to test: https://1drv.ms/u/s%21Atv-gP4NyojQoGrC_EAICpOcWTP4

– acman123 – 2019-01-12T20:46:06.160