Trim an MP3 Programmatically

2020-07-27 08:10发布

问题:

What is the best way to trim a mp3 file programmatically. For example, say I want to get rid of the first 2 minutes or last 2 minutes or both. Is there a good way to do this from .NET? Or .NET calling out to a command line tool?

回答1:

There are two approaches to trimming MP3 files:

First, convert to WAV, trim off the samples you don't want, and then convert back to MP3. The disadvantage is that there will be a very slight loss of quality in the process. The advantage is that you will find plenty of command line tools to do the conversions for you, leaving you simply to trim the WAV file yourself (NAudio would let you do that).

Second, parse the MP3 frames themselves, and throw away whole frames. It doesn't give you so much granularity but there is no loss of quality in the process. You also need to be able to understand the format of the CBR and VBR MP3 frames as well as ID3 frames. There are various .NET libraries around that can read these, but you will still need to write a fair amount of code yourself.



回答2:

My bet is on a CLI tool. Take this for example.



回答3:

I wrapped mp3 decoder library and made it available for .net developers. You can find it here:

http://sourceforge.net/projects/mpg123net/

Included are the samples to convert mp3 file to PCM, and read ID3 tags.

Maybe you can use it to find mp3 frames and write out only ones that you are interested in keeping?