0

I want to split a mp3 file into several pieces e.g. from 0:30 to 0:45 and from 0:45 to 1:00 in ActionScript.

The closest thing I've found is this short snippet from documentation. I cannot find any articles or libraries that would help me with this. Is it even possible to achieve?

Thanks in advance for any tips.

Example of what I want: mp3cut

dev9
  • 2,202
  • 4
  • 20
  • 29

1 Answers1

2

I haven't done this myself, but the way I would go about it follows this process:

  1. Load the MP3 file
  2. Decode the MP3 file
  3. Extract the bits you need
  4. Encode the extracted data

All of the below is to be considered untested pseudo code.

Step 1: Load the MP3 file

I assume that you already have a way to load the compressed MP3 file into memory. If not, there is plenty of information about this step to be found.

Step 2: decode the MP3 file

The Sound class exposes the method loadCompressedDataFromByteArray (docs) which takes the compressed MP3 file as a ByteArray and decodes it to raw sound data.

Example:

var mySound:Sound = new Sound();
mySound.loadCompressedDataFromByteArray(myMP3Data, myMP3Data.length);

Step 3: Extract a part of the sound

Using the extract (docs) method (as described in the link in your question) you can extract raw sound data from a Sound object. You pass in a ByteArray object to be populated with the data, specify where you want the "cut" to be, and the length of the clip to extract.

The data is stored using 44100 samples per second (left + right channel), and the calculations below is based on this. I might be wrong about this, so if it doesn't work as expected, please look this up further.

Example:

var sampleFrequency:int = 44100;
var startTimeInSeconds:Number = 30.0;
var lengthInSeconds:Number = 15.0;

// Calculate the position to start the clip (in samples)
var startPosition:Number = Math.round(startTimeInSeconds * sampleFrequency);

// Calculate the number of samples to extract
var samplesLength:Number = Math.round(lengthInSeconds * sampleFrequency);

var extractedBytes:ByteArray = new ByteArray();

mySound.extract(extractedBytes, samplesLength, startPosition);

Step 4: Encode the extracted sound back to MP3

Now that you have the sound data as a ByteArray, theres a few way to encode it back to the MP3 format. This previous StackOverflow answer mentions this library, but there might be other libraries out there better suited for your tasks.

Community
  • 1
  • 1
david.emilsson
  • 2,313
  • 1
  • 20
  • 24