Author Topic: Re-encoding an MP3 to only contain first 10 seconds ....  (Read 386 times)

axisdj

  • Posts: 42
I would like suggestions on Re-encoding an MP3 to only contain first 10 seconds and last 10 seconds of audio and the middle completely silent. Meaning the new track will be the same length as the original track, but only contains the first and last 10 seconds of data from the original.
 
 
Please let me know your ideas

Ian @ un4seen

  • Administrator
  • Posts: 20389
One way you could achieve that is with the BASS_FX add-on's volume envelope (BASS_FX_BFX_VOLUME_ENV) effect. That will allow you to fade-in/out the silence, if you want. It could be setup something like this:

Code: [Select]
decoder=BASS_StreamCreateFile(FALSE, filename, 0, 0, BASS_STREAM_DECODE); // create decoder for MP3 file
volenvfx=BASS_ChannelSetFX(decoder, BASS_FX_BFX_VOLUME_ENV, 0); // set volume envelope effect on it
double end=BASS_ChannelBytes2Seconds(decoder, BASS_ChannelGetLength(decoder, BASS_POS_BYTE)); // get the length in seconds
// setup the envelope nodes...
BASS_BFX_ENV_NODE nodes[5];
// start at full volume
nodes[0].pos=0;
nodes[0].val=1;
// start fading out after 10 seconds
nodes[1].pos=10;
nodes[1].val=1;
// silent after 10.1 seconds
nodes[2].pos=10.1;
nodes[2].val=0;
// start fading in 10.1 seconds from the end
nodes[3].pos=end-10.1;
nodes[3].val=0;
// back to full volume 10 seconds from the end
nodes[4].pos=end-10;
nodes[4].val=1;
BASS_BFX_VOLUME_ENV volenvparam={
BASS_BFX_CHANALL, // apply to all channels
5, // 5 nodes
nodes, // the nodes
TRUE // the envelope follows the source position
};
BASS_FXSetParameters(volenvfx, volenvpararm); // apply the effect's parameters
BASS_Encode_Start(decoder, "lame - output.mp3", BASS_ENCODE_AUTOFREE, NULL, 0); // set an MP3 encoder (LAME) on the decoder
// processing loop...
while (1) {
BYTE buf[20000];
int r=BASS_ChannelGetData(decoder, buf, sizeof(buf)); // process the decoder (and envelope and encoder)
if (r<0) break; // done
}
BASS_StreamFree(decoder); // free the decoder (and encoder due to AUTOFREE)

You may also want to add the BASS_STREAM_PRESCAN flag to the BASS_StreamCreateFile call, to have BASS pre-scan the MP3 file for an exact length value.

axisdj

  • Posts: 42
Thank you Ian, will try it tonight, seems like a real elegant way to do it...

WP