5

I am working on a C# WPF application that groups a group of words (from about 17,000 words) together and generates a playlist matching mp3 files (each mp3 file being a word).

Currently each mp3 file has a random silence between 1 second to 30 seconds at the end of its wave.

I want to be able to programmatically remove the silence wave from the end of the file.

Using NAudio for .NET how do I remove the silence wave from the end of mp3 file?

  • determining "what is silence?": http://stackoverflow.com/questions/19353/detecting-audio-silence-in-wav-files-using-c-sharp –  Oct 24 '12 at 14:26
  • 1
    trim-example found in the docu of NAudio: http://mark-dot-net.blogspot.co.at/2009/09/trimming-wav-file-using-naudio.html –  Oct 24 '12 at 14:27
  • Here's a basic implementation for [detecting silence duration using NAudio](https://stackoverflow.com/a/46024371/4934172), which can very easily be used to truncate the silence from the file. – 41686d6564 stands w. Palestine Sep 03 '17 at 14:28

2 Answers2

2

I accomplished Using SoX

Doc for SoX Silence: http://digitalcardboard.com/blog/2009/08/25/the-sox-of-silence/

Stackoverflow Help Reference for SoX Silence end of file: https://stackoverflow.com/a/11834671/677607

Command Line execution using C#: https://stackoverflow.com/a/1469790/677607

Community
  • 1
  • 1
  • I would also like to note here that you can use the [silenceremove ffmpeg filter](https://ffmpeg.org/ffmpeg-filters.html#silenceremove) for this task. – CoolOppo Jan 30 '21 at 04:40
1

There are two ways to trim MP3 files. The first is by using the Mp3FileReader to read out audio as WAV, and only write the amount you want to a new WAV file using the WaveFileWriter. Then you would use a tool such as LAME.exe to convert back to MP3 as NAudio does not include an MP3 encoder.

The second is to discard Mp3Frames and has the benefit of not requiring a decode and re-encode. MP3 files consist of a series of Mp3Frames, (and optionally have metadata such as ID3 tags at the start or end). You could then create a new MP3 file by just appending the MP3 frames you want into a new file, skipping over the last ones (and optionally making sure your ID3 tags got included in the truncated file). Look at the source code for Mp3FileReader to see how the Mp3Frames can be read one by one out of an MP3 file.

Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • `Currently each mp3 file has a random silence between 1 second to 30 seconds at the end of its wave.` how will you know how many frames to skip?! i'd guess the OP is after determining silent-frames :) –  Oct 24 '12 at 14:23
  • each Mp3Frame has a SampleCount property from which you can work out its duration. Detecting silence is another matter - you can get at the sample values by using Mp3FileReader to convert to PCM, but usually when people say "silence" they mean below a certain volume threshold, so you'd need to examine the values of each sample and decide if it fell under your threshold – Mark Heath Oct 24 '12 at 15:35
  • Mark, I'm looking your code (naudio) now and have a few questions. I need to remove "silence" from the audio file (format is not important, because I can convert formats with SoX, for example). SoX is working well, but... I need not just remove silence from the audio file, but set special tags for removing parts and write this tags to file. Can you give me any advises? – Alexander May 29 '15 at 08:46