Skip to content

Commit

Permalink
Merge pull request #511 from yuygfgg/patch-1
Browse files Browse the repository at this point in the history
Fix Metadata Extraction from FFMPEG with Leading Zeros in Track Numbers and Mixed-Case Tags and Add Support for Discnumber
  • Loading branch information
kagemomiji authored Jun 22, 2024
2 parents d7b8ff6 + 1e7dd06 commit 8e6bcbf
Showing 1 changed file with 42 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
* Parses meta data from video files using FFmpeg (http://ffmpeg.org/).
Expand Down Expand Up @@ -103,12 +102,28 @@ public MetaData getRawMetaData(Path file) {
metaData.setAlbumName(getData(result, "album"));
metaData.setGenre(getData(result, "genre"));
metaData.setTitle(getData(result, "title"));

String data = getData(result, "track");
if (NumberUtils.isCreatable(data)) {
metaData.setTrackNumber(NumberUtils.createInteger(data));
if (data != null) {
data = data.replaceFirst("^[\\s\\p{C}]*0+(?!$)", "");
if (NumberUtils.isCreatable(data)) {
metaData.setTrackNumber(NumberUtils.createInteger(data));
}
}
data = getData(result, "disc");
if (data != null) {
data = data.replaceFirst("^[\\s\\p{C}]*0+(?!$)", "");
if (NumberUtils.isCreatable(data)) {
metaData.setDiscNumber(NumberUtils.createInteger(data));
}
}

data = getData(result, "discnumber");
if (data != null) {
data = data.replaceFirst("^[\\s\\p{C}]*0+(?!$)", "");
if (NumberUtils.isCreatable(data)) {
metaData.setDiscNumber(NumberUtils.createInteger(data));
}
}
data = getData(result, "date");
if (NumberUtils.isCreatable(data)) {
metaData.setYear(NumberUtils.createInteger(data));
Expand Down Expand Up @@ -138,24 +153,33 @@ public MetaData getRawMetaData(Path file) {
}

private static String getData(JsonNode node, String keyName) {
List<String> keys = ImmutableList.of("/tags/" + keyName, "/tags/" + keyName.toUpperCase(), "/tags/" + keyName.toLowerCase());
Optional<String> nonNullKey = keys.stream().map(k -> node.at("/format" + k).asText()).filter(StringUtils::isNotBlank).findFirst();
if (nonNullKey.isPresent()) {
return nonNullKey.get();
} else {
// Create a list of key variations to handle different cases
List<String> keyVariations = ImmutableList.of(
keyName.toLowerCase(),
keyName.toUpperCase(),
StringUtils.capitalize(keyName) // Capitalizes only the first letter
);
// Try to find data in /format/tags/ with different key cases
for (String key : keyVariations) {
String path = "/format/tags/" + key;
String value = node.at(path).asText();
if (StringUtils.isNotBlank(value)) {
return value;
}
}
// If not found in /format/tags/, check each stream's tags
if (node.has("streams")) {
for (JsonNode stream : node.at("/streams")) {
// skip coverart streams
if (stream.hasNonNull("codec_name") && stream.get("codec_name").asText().equalsIgnoreCase("mjpeg")) {
continue;
}
nonNullKey = keys.stream().map(k -> stream.at(k).asText()).filter(StringUtils::isNotBlank).findFirst();
if (nonNullKey.isPresent()) {
return nonNullKey.get();
for (String key : keyVariations) {
String tagPath = "/tags/" + key;
String value = stream.at(tagPath).asText();
if (StringUtils.isNotBlank(value)) {
return value;
}
}
}
}

return null;
return null; // Return null if no matching data is found
}

/**
Expand Down

0 comments on commit 8e6bcbf

Please sign in to comment.