-1

I am working on programmatically removing/redacting parts of an .mp3 audio file. An example scenario is to mute/ remove the audio from 1:00 - 2:00 in a 3 minute (0:00 - 3:00) audio file. Do we have any libraries that can be useful for this scenario?

I know how to achieve this for .wav audio files using the Javax Sound API (package: javax.sound), but it looks like this API doesn't support .mp3 files

This is how I am thinking to achieve it technically if I were to work with .wav

  1. The audio is composed as frames. Each frame represent a time slot. Use the AudioInputStream read() method to convert audio file to raw audio data (byte[])
  2. Find the frame which represents the start time slot (Using the audioInputStream.getFrameLength() and audioInputStream.getFrameRate() APIs)
  3. Find the frame which represents the end time slot (Using the audioInputStream.getFrameLength() and audioInputStream.getFrameRate() APIs)
  4. Remove the frames between the start and end time slots in the array
  5. Convert the byte array to the AudioInputStream - AudioSystem.getAudioInputStream(ByteArrayInputStream)

References- AudioInputStream AudioSystem

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Have a look at [Playing MP3 using Java Sound API](https://stackoverflow.com/questions/5667454/playing-mp3-using-java-sound-api) – Erwin Bolwidt Nov 19 '19 at 22:05

1 Answers1

0

I think you are on the right track, as far as .wav files are concerned.

The conversion of frame to time depends on the sample rate. For example 44100 fps puts time 0.5 = frame location 22050. The raw data will probably have multiple bytes per frame. 16 bit encoding is of course 2 bytes, and stereo 16-bit is 4 bytes per frame.

Often it becomes worthwhile to convert the byte data to and from signed PCM floats (ranging -1 to 1) before working on it.

For working with mp3 files, one has to first decompress the file, do your edits, then recompress back to mp3. A library for mp3 coding/decoding can be found at git hub at the following location: pdudits/soundlibs A lot will depend on the format of the sound file prior to its being compressed.

I don't know about the specifics of getting to PCM frames from these tools for .mp3 files. I recall having to tinker with the code in JORBIS to intercept the data prior to its being sent to playback output. I wouldn't be surprised if you would have to go through something similar to get JLayer working for you.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41