Skip to content

Commit

Permalink
Support ISRC metadata in MP3, OGG and Matroska files (#110)
Browse files Browse the repository at this point in the history
* Extract ISRC tag from MP3, Vorbis and, (loosely) Matroska

* Extract title out of matroska tags if not available from segment info

* only update title from segment info if null

* equalsIgnoreCase title + isrc
  • Loading branch information
devoxin authored May 12, 2024
1 parent c0f90e7 commit 2a94ae5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.sedmelluq.discord.lavaplayer.container.MediaContainerHints;
import com.sedmelluq.discord.lavaplayer.container.MediaContainerProbe;
import com.sedmelluq.discord.lavaplayer.container.matroska.format.MatroskaFileTrack;
import com.sedmelluq.discord.lavaplayer.tools.DataFormatTools;
import com.sedmelluq.discord.lavaplayer.tools.io.SeekableInputStream;
import com.sedmelluq.discord.lavaplayer.track.AudioReference;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
import com.sedmelluq.discord.lavaplayer.track.info.AudioTrackInfoBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -59,14 +61,11 @@ public MediaContainerDetectionResult probe(AudioReference reference, SeekableInp
return unsupportedFormat(this, "No supported audio tracks present in the file.");
}

String title = file.getTitle();
String actualTitle = title == null || title.isEmpty() ? UNKNOWN_TITLE : title;
String title = DataFormatTools.defaultOnNull(file.getTitle(), UNKNOWN_TITLE);
String artist = DataFormatTools.defaultOnNull(file.getArtist(), UNKNOWN_ARTIST);

String artist = file.getArtist();
String actualArtist = artist == null || artist.isEmpty() ? UNKNOWN_ARTIST : artist;

return supportedFormat(this, null, new AudioTrackInfo(actualTitle, actualArtist,
(long) file.getDuration(), reference.identifier, false, reference.identifier, null, null));
return supportedFormat(this, null, new AudioTrackInfo(title, artist,
(long) file.getDuration(), reference.identifier, false, reference.identifier, null, file.getIsrc()));
}

private boolean hasSupportedAudioTrack(MatroskaStreamingFile file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class MatroskaStreamingFile {

private String title;
private String artist;
private String isrc;

private long timecodeScale = 1000000;
private double duration;
Expand Down Expand Up @@ -49,11 +50,15 @@ public long getTimecodeScale() {
* @return The title for this file.
*/
public String getTitle() {
return title;
return title != null && title.isEmpty() ? null : title;
}

public String getArtist() {
return artist;
return artist != null && artist.isEmpty() ? null : artist;
}

public String getIsrc() {
return isrc != null && isrc.isEmpty() ? null : isrc;
}

/**
Expand Down Expand Up @@ -394,7 +399,7 @@ private void parseSegmentInfo(MatroskaElement infoElement) throws IOException {
duration = reader.asDouble(child);
} else if (child.is(MatroskaElementType.TimecodeScale)) {
timecodeScale = reader.asLong(child);
} else if (child.is(MatroskaElementType.Title)) {
} else if (child.is(MatroskaElementType.Title) && title == null) {
title = reader.asString(child);
}

Expand Down Expand Up @@ -446,8 +451,13 @@ private void parseSimpleTag(MatroskaElement simpleTagElement) throws IOException
if (child.is(MatroskaElementType.TagName)) {
tagName = reader.asString(child);
} else if (child.is(MatroskaElementType.TagString)) {
if ("artist".equalsIgnoreCase(tagName)) {
// https://www.matroska.org/technical/tagging.html
if ("title".equalsIgnoreCase(tagName) && title == null) {
title = reader.asString(child);
} else if ("artist".equalsIgnoreCase(tagName)) {
artist = reader.asString(child);
} else if ("isrc".equalsIgnoreCase(tagName)) {
isrc = reader.asString(child);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public class Mp3TrackProvider implements AudioTrackInfoProvider {

private static final String TITLE_TAG = "TIT2";
private static final String ARTIST_TAG = "TPE1";
private static final String ISRC_TAG = "TSRC";

private static final List<String> knownTextExtensions = Arrays.asList(TITLE_TAG, ARTIST_TAG);
private static final List<String> knownTextExtensions = Arrays.asList(TITLE_TAG, ARTIST_TAG, ISRC_TAG);

private final AudioProcessingContext context;
private final SeekableInputStream inputStream;
Expand Down Expand Up @@ -369,7 +370,7 @@ public String getArtworkUrl() {

@Override
public String getISRC() {
return null;
return getIdv3Tag(ISRC_TAG);
}

private static class FrameHeader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class OggMetadata implements AudioTrackInfoProvider {

private static final String TITLE_FIELD = "TITLE";
private static final String ARTIST_FIELD = "ARTIST";
private static final String ISRC_FIELD = "ISRC";

private final Map<String, String> tags;
private final Long length;
Expand Down Expand Up @@ -58,6 +59,6 @@ public String getArtworkUrl() {

@Override
public String getISRC() {
return null;
return tags.get(ISRC_FIELD);
}
}

0 comments on commit 2a94ae5

Please sign in to comment.