/* MP3Identifier.java Copyright (c) 2009 Noa Resare Below is the MIT license, for more information about how this works please see http://en.wikipedia.org/wiki/MIT_License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.UnsupportedAudioFileException; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; /** * Identifies MP3 files using the javax.sound.sampled framework. This code assumes * that there is a jarfile in the classpath that adds MP3 reading functionality * to the java environment, such as SUN's MP3 plugin available from * http://java.sun.com/javase/technologies/desktop/media/jmf/mp3/download.html * * @author Noa Resare (noa@voxbiblia.com) */ public class MP3Identifier { /** * If the format of the referenced file can be determined by * javax.sound.sampled.AudioSystem this program prints the name of the * format and exits. If the format of the refrenced file is not recognized * an exception is thrown. * * @param args args[0] is assumed to be the name of a file to read * @throws IOException if file read fails * @throws UnsupportedAudioFileException if the file format can not be * determined */ public static void main(String[] args) throws IOException, UnsupportedAudioFileException { BufferedInputStream audioStream = new BufferedInputStream( new FileInputStream(args[0])); possiblySkipID3(audioStream); AudioFileFormat aff = AudioSystem.getAudioFileFormat(audioStream); System.out.printf("Format of '%s' is %s\n", args[0], aff.getType()); } /** * If the BufferedInputStream references input that contains an ID3v2 * metadata tag, determine the length of the tag and skip past it. * * @param audioStream the BufferedInputStram to read from and possibly * skip in * @throws IOException if IO operation fails */ private static void possiblySkipID3(BufferedInputStream audioStream) throws IOException { audioStream.mark(10); byte[] header = new byte[10]; if (audioStream.read(header) != header.length) { throw new Error("read failed"); } audioStream.reset(); if (header[0] == 'I' && header[1] == 'D' && header[2] == '3') { // tag length does not include the 10 byte header int toSkip = getTagLength(header) + 10; toSkip -= audioStream.skip(toSkip); while (toSkip > 0) { toSkip -= audioStream.skip(toSkip); } } } private static int getTagLength(byte[] tagHeader) { int i = tagHeader[6] << 21; i += tagHeader[7] << 14; i += tagHeader[8] << 7; return i + tagHeader[9]; } }