Charles Vaughan
Charles Vaughan

Reputation: 23

Detecting Audio File Bit Rate - Processing / Java

Trying to build a little app for sorting through audio files based on some of their properties. Have managed to grab the Sample Rate and Bit Depth using Minim but can't find anything anywhere for getting the Bit Rate?

Happy to look at taking the program to Javascript if needed but just desperate to find a method for detecting bit rate of a given file.

EDIT: Attempted to try and form an equation based off file size but cannot find a method for detecting MP3 file size either.

Upvotes: 2

Views: 1272

Answers (1)

Arch
Arch

Reputation: 527

You can use jaudiotagger

You will need to download the jar, I managed to get it from maven central

Go to Sketch -> Add File... and select the downloaded jar, it should be added in a folder named code within your sketch folder.

Assuming you have placed an mp3 file in your data folder named audio.mp3 the following code should work, printing out the bit rate in the terminal.

import org.jaudiotagger.audio.mp3.*;
import org.jaudiotagger.audio.AudioFileIO;

void setup() {
  File f = new File(dataPath("audio.mp3"));
  try {
    MP3File mp3 = (MP3File) AudioFileIO.read(f);

    MP3AudioHeader audioHeader = mp3.getMP3AudioHeader();
    println("" + audioHeader.getBitRate());
  }
  catch(Exception e) {
    e.printStackTrace();
  }
}

JAudiotagger supports a variety of file formats and you can use the relevant classes and methods for each one of these.

I suggest you take a look at the javadoc. Be careful of the examples though, the one I used in order to answer your question seems to be faulty, as you can see I had to swap getAudioHeader with getMP3AudioHeader.

Upvotes: 4

Related Questions