From e2b8ef0813d22d38dc8c817b3467b36653ca6af2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 17 Mar 2013 10:57:25 +0100 Subject: [PATCH] Parse ID3v2 tag from MP3 streams. --- pom.xml | 5 +++++ .../net/pterodactylus/sonitus/io/Mp3Identifier.java | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index b78f4ba..d4fff91 100644 --- a/pom.xml +++ b/pom.xml @@ -29,6 +29,11 @@ jlayer 1.0.1 + + org.blinkenlights.jid3 + JID3 + 0.46 + diff --git a/src/main/java/net/pterodactylus/sonitus/io/Mp3Identifier.java b/src/main/java/net/pterodactylus/sonitus/io/Mp3Identifier.java index e9a550b..62285c4 100644 --- a/src/main/java/net/pterodactylus/sonitus/io/Mp3Identifier.java +++ b/src/main/java/net/pterodactylus/sonitus/io/Mp3Identifier.java @@ -26,6 +26,8 @@ import com.google.common.base.Optional; import javazoom.jl.decoder.Bitstream; import javazoom.jl.decoder.BitstreamException; import javazoom.jl.decoder.Header; +import org.blinkenlights.jid3.ID3Exception; +import org.blinkenlights.jid3.v2.ID3V2Tag; /** * Identifies MP3 files. @@ -46,12 +48,25 @@ public class Mp3Identifier { */ public static Optional identify(InputStream inputStream) throws IOException { Bitstream bitstream = new Bitstream(inputStream); + Optional id3v2Tag = Optional.absent(); + try { + InputStream id3v2Stream = bitstream.getRawID3v2(); + id3v2Stream.read(new byte[3]); + id3v2Tag = Optional.fromNullable(ID3V2Tag.read(id3v2Stream)); + } catch (ID3Exception id3e1) { + /* ID3v2 tag could not be parsed, don’t cry about it. */ + } try { Header frame = bitstream.readFrame(); if (frame == null) { return Optional.absent(); } - return Optional.of(new Metadata(frame.mode() == Header.SINGLE_CHANNEL ? 1 : 2, frame.frequency(), "MP3")); + Metadata metadata = new Metadata(frame.mode() == Header.SINGLE_CHANNEL ? 1 : 2, frame.frequency(), "MP3"); + if (id3v2Tag.isPresent()) { + metadata = metadata.artist(id3v2Tag.get().getArtist()); + metadata = metadata.name(id3v2Tag.get().getTitle()); + } + return Optional.of(metadata); } catch (BitstreamException be1) { return Optional.absent(); } -- 2.7.4