Author Topic: "Cut" specific part of Mp3 file and save  (Read 711 times)

yaniv51

  • Posts: 4
"Cut" specific part of Mp3 file and save
« on: 25 Aug '15 - 09:01 »
Hi all,
I'm using last version of Bass in .Net.

I have a player and I want to edit&save the mp3 file.
I know how to get start position and end position (and play the segmet), and now I want to save the new segment.
(start from the new millisec position, end in new millisec position)

Is there an easy way with Bass to do so or should I need to calculate it?(with byte array size and stream length)

Thank you all,
Yaniv.

Ian @ un4seen

  • Administrator
  • Posts: 20389
Re: "Cut" specific part of Mp3 file and save
« Reply #1 on: 25 Aug '15 - 16:47 »
If you don't mind re-encoding the extracted segment, then you could use the BASSenc add-on and LAME encoder to do it, something like this:

Code: [Select]
decoder=BASS_StreamCreateFile(FALSE, filename, 0, 0, BASS_STREAM_DECODE|BASS_STREAM_PRESCAN); // create decoder for MP3 file and pre-scan it
BASS_Encode_Start(decoder, "lame -o segment.mp3 -", 0, 0, 0); // set a LAME encoder on it
BASS_ChannelSetPosition(decoder, startpos, BASS_POS_BYTE); // seek to start of segment
QWORD todo=endpos-startpos; // the amount of data to process
while (todo) {
BYTE buf[20000]; // processing buffer
int c=BASS_ChannelGetData(decoder, buf, min(todo, sizeof(buf)); // process some data
if (c==-1) break; // error/end
todo-=c; // count down
}
BASS_Encode_Stop(decoder); // stop the encoder
BASS_StreamFree(decoder); // free the decoder

If you would prefer to extract the original MP3 data from the file, then the start/end positions may be fractionally off but you could try something like this:

Code: [Select]
BASS_ChannelSetPosition(decoder, startpos, BASS_POS_BYTE); // seek to start of segment
QWORD filestart=BASS_StreamGetFilePosition(decoder, BASS_FILEPOS_START)+BASS_StreamGetFilePosition(decoder, BASS_FILEPOS_CURRENT); // get file position
BASS_ChannelSetPosition(decoder, endpos, BASS_POS_BYTE); // seek to end of segment
QWORD fileend=BASS_StreamGetFilePosition(decoder, BASS_FILEPOS_START)+BASS_StreamGetFilePosition(decoder, BASS_FILEPOS_CURRENT); // get file position
// extract "filestart" to "fileend" from the original MP3 file here...

Please see the documentation for details on all of the aforementioned functions.

yaniv51

  • Posts: 4
Re: "Cut" specific part of Mp3 file and save
« Reply #2 on: 25 Aug '15 - 16:55 »
Hi,
Many thanks for the fast replay.

I think that the second option is good for me, but do I need to save the audio file with an header?
If no, it means that BASS create new audio file for byte array?
If yes, do I need to create new one or just use old one?( and how should I know the header size?)

Thanks alot again.

Ian @ un4seen

  • Administrator
  • Posts: 20389
Re: "Cut" specific part of Mp3 file and save
« Reply #3 on: 25 Aug '15 - 17:14 »
MP3 files don't need a header, but if you want to copy the tags, then you could copy them from the start (ID3v2) or end (ID3v1) of the file.

Please note BASS won't create a new file for you in the 2nd method, it just tells you what part you should extract/copy from the original file.