WIP: replacement content for existing PR (#3011)

This commit is contained in:
Max
2026-03-12 19:13:30 -06:00
committed by GitHub
parent 5b901fc21e
commit 5fa511a26c
49 changed files with 202 additions and 53 deletions
@@ -282,7 +282,7 @@ public class KomgaMapper {
case FB2 -> "application/fictionbook2+zip";
case MOBI -> "application/x-mobipocket-ebook";
case AZW3 -> "application/vnd.amazon.ebook";
case AUDIOBOOK -> "audio/mp4";
case AUDIOBOOK -> "audio/*";
};
}
@@ -128,7 +128,7 @@ public class BookFileEntity {
.filter(java.nio.file.Files::isRegularFile)
.filter(p -> {
String name = p.getFileName().toString().toLowerCase();
return name.endsWith(".mp3") || name.endsWith(".m4a") || name.endsWith(".m4b");
return name.endsWith(".mp3") || name.endsWith(".m4a") || name.endsWith(".m4b") || name.endsWith(".opus");
})
.sorted()
.findFirst()
@@ -20,7 +20,8 @@ public enum BookFileExtension {
FB2("fb2", BookFileType.FB2),
M4B("m4b", BookFileType.AUDIOBOOK),
M4A("m4a", BookFileType.AUDIOBOOK),
MP3("mp3", BookFileType.AUDIOBOOK);
MP3("mp3", BookFileType.AUDIOBOOK),
OPUS("opus", BookFileType.AUDIOBOOK);
private final String extension;
private final BookFileType type;
@@ -15,7 +15,7 @@ public enum BookFileType {
FB2(Set.of("fb2")),
MOBI(Set.of("mobi")),
AZW3(Set.of("azw3", "azw")),
AUDIOBOOK(Set.of("m4b", "m4a", "mp3"));
AUDIOBOOK(Set.of("m4b", "m4a", "mp3", "opus"));
private final Set<String> extensions;
@@ -85,6 +85,6 @@ public class FileFingerprint {
private static boolean isAudioFile(String fileName) {
if (fileName == null) return false;
String lower = fileName.toLowerCase();
return lower.endsWith(".mp3") || lower.endsWith(".m4a") || lower.endsWith(".m4b");
return lower.endsWith(".mp3") || lower.endsWith(".m4a") || lower.endsWith(".m4b") || lower.endsWith(".opus");
}
}
@@ -40,7 +40,7 @@ public class MetadataExtractorFactory {
case FB2 -> fb2MetadataExtractor.extractMetadata(file);
case MOBI -> mobiMetadataExtractor.extractMetadata(file);
case AZW3, AZW -> azw3MetadataExtractor.extractMetadata(file);
case M4B, M4A, MP3 -> audiobookMetadataExtractor.extractMetadata(file);
case M4B, M4A, MP3, OPUS -> audiobookMetadataExtractor.extractMetadata(file);
};
}
@@ -52,7 +52,7 @@ public class MetadataExtractorFactory {
case FB2 -> fb2MetadataExtractor.extractCover(file);
case MOBI -> mobiMetadataExtractor.extractCover(file);
case AZW3, AZW -> azw3MetadataExtractor.extractCover(file);
case M4B, M4A, MP3 -> audiobookMetadataExtractor.extractCover(file);
case M4B, M4A, MP3, OPUS -> audiobookMetadataExtractor.extractCover(file);
};
}
@@ -694,6 +694,7 @@ public class OpdsFeedService {
case AUDIOBOOK -> {
String lower = bookFile.getFileName().toLowerCase();
if (lower.endsWith(".mp3")) yield "audio/mpeg";
if (lower.endsWith(".opus")) yield "audio/opus";
yield "audio/mp4";
}
};
@@ -20,7 +20,7 @@ import java.util.stream.Stream;
public class AudioFileUtilityService {
private static final Set<String> AUDIO_EXTENSIONS = Set.of(
".mp3", ".m4a", ".m4b"
".mp3", ".m4a", ".m4b", ".opus"
);
/**
@@ -55,6 +55,8 @@ public class AudioFileUtilityService {
return "audio/mp4";
} else if (fileName.endsWith(".mp3")) {
return "audio/mpeg";
} else if (fileName.endsWith(".opus")) {
return "audio/opus";
}
return "application/octet-stream";
}
@@ -140,11 +140,10 @@ public class AudioMetadataService {
totalDurationMs += trackDurationMs;
if (i == 0) {
long bitrateValue = header.getBitRateAsNumber();
bitrate = bitrateValue > 0 ? (int) bitrateValue : null;
codec = header.getEncodingType();
sampleRate = header.getSampleRateAsNumber();
channels = parseChannels(header.getChannels());
bitrate = safeBitrate(header, trackPath);
codec = safeEncodingType(header, trackPath);
sampleRate = safeSampleRate(header, trackPath);
channels = parseChannels(safeChannels(header, trackPath));
if (tag != null) {
title = getTagValue(tag, FieldKey.ALBUM, FieldKey.TITLE);
author = getTagValue(tag, FieldKey.ALBUM_ARTIST, FieldKey.ARTIST);
@@ -222,13 +221,12 @@ public class AudioMetadataService {
AudioHeader header = audioFile.getAudioHeader();
Tag tag = audioFile.getTag();
long durationMs = (long) (header.getPreciseTrackLength() * 1000);
long bitrateValue = header.getBitRateAsNumber();
long durationMs = safeDurationMs(header, audioPath);
builder.durationMs(durationMs)
.bitrate(bitrateValue > 0 ? (int) bitrateValue : null)
.codec(header.getEncodingType())
.sampleRate(header.getSampleRateAsNumber())
.channels(parseChannels(header.getChannels()));
.bitrate(safeBitrate(header, audioPath))
.codec(safeEncodingType(header, audioPath))
.sampleRate(safeSampleRate(header, audioPath))
.channels(parseChannels(safeChannels(header, audioPath)));
if (tag != null) {
builder.title(getTagValue(tag, FieldKey.TITLE, FieldKey.ALBUM))
@@ -360,4 +358,52 @@ public class AudioMetadataService {
return null;
}
}
private long safeDurationMs(AudioHeader header, Path audioPath) {
try {
return (long) (header.getPreciseTrackLength() * 1000);
} catch (RuntimeException e) {
log.warn("Failed to read track duration from {}", audioPath, e);
return 0L;
}
}
private Integer safeBitrate(AudioHeader header, Path audioPath) {
try {
long bitrateValue = header.getBitRateAsNumber();
return bitrateValue > 0 ? (int) bitrateValue : null;
} catch (RuntimeException e) {
log.debug("Audio header has no bitrate for {}", audioPath, e);
return null;
}
}
private Integer safeSampleRate(AudioHeader header, Path audioPath) {
try {
int sampleRate = header.getSampleRateAsNumber();
return sampleRate > 0 ? sampleRate : null;
} catch (RuntimeException e) {
log.debug("Audio header has no sample rate for {}", audioPath, e);
return null;
}
}
private String safeEncodingType(AudioHeader header, Path audioPath) {
try {
String encodingType = header.getEncodingType();
return encodingType != null && !encodingType.isBlank() ? encodingType : null;
} catch (RuntimeException e) {
log.debug("Audio header has no encoding type for {}", audioPath, e);
return null;
}
}
private String safeChannels(AudioHeader header, Path audioPath) {
try {
return header.getChannels();
} catch (RuntimeException e) {
log.debug("Audio header has no channel info for {}", audioPath, e);
return null;
}
}
}
@@ -155,7 +155,7 @@ public class FileUtils {
public boolean isAudioFile(String fileName) {
if (fileName == null) return false;
String lower = fileName.toLowerCase();
return lower.endsWith(".mp3") || lower.endsWith(".m4a") || lower.endsWith(".m4b");
return lower.endsWith(".mp3") || lower.endsWith(".m4a") || lower.endsWith(".m4b") || lower.endsWith(".opus");
}
/**