fix(audiobook): sum folder track durations (#2684)
This commit is contained in:
@@ -261,7 +261,11 @@ public class AudiobookProcessor extends AbstractFileProcessor implements BookFil
|
||||
return;
|
||||
}
|
||||
|
||||
audiobookFile.setDurationSeconds(audiobookDto.getDurationSeconds());
|
||||
Long durationSeconds = audiobookFile.isFolderBased()
|
||||
? calculateFolderDurationSeconds(audiobookFile, audiobookDto.getDurationSeconds())
|
||||
: audiobookDto.getDurationSeconds();
|
||||
|
||||
audiobookFile.setDurationSeconds(durationSeconds);
|
||||
audiobookFile.setBitrate(audiobookDto.getBitrate());
|
||||
audiobookFile.setSampleRate(audiobookDto.getSampleRate());
|
||||
audiobookFile.setChannels(audiobookDto.getChannels());
|
||||
@@ -270,6 +274,33 @@ public class AudiobookProcessor extends AbstractFileProcessor implements BookFil
|
||||
audiobookFile.setChapters(mapChapters(audiobookDto.getChapters()));
|
||||
}
|
||||
|
||||
private Long calculateFolderDurationSeconds(BookFileEntity audiobookFile, Long firstTrackDurationSeconds) {
|
||||
List<Path> audioFiles = FileUtils.listAudioFilesInFolder(audiobookFile.getFullFilePath());
|
||||
if (audioFiles.isEmpty()) {
|
||||
return firstTrackDurationSeconds;
|
||||
}
|
||||
|
||||
long totalDurationMillis = 0;
|
||||
boolean hasDuration = false;
|
||||
|
||||
for (int i = 0; i < audioFiles.size(); i++) {
|
||||
Long trackDurationMillis = audiobookMetadataExtractor.extractDurationMillis(audioFiles.get(i).toFile());
|
||||
if (trackDurationMillis == null && i == 0 && firstTrackDurationSeconds != null) {
|
||||
trackDurationMillis = firstTrackDurationSeconds * 1000;
|
||||
}
|
||||
|
||||
if (trackDurationMillis != null && trackDurationMillis > 0) {
|
||||
totalDurationMillis += trackDurationMillis;
|
||||
hasDuration = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasDuration) {
|
||||
return firstTrackDurationSeconds;
|
||||
}
|
||||
return Math.round(totalDurationMillis / 1000.0);
|
||||
}
|
||||
|
||||
private List<BookFileEntity.AudioFileChapter> mapChapters(List<AudiobookMetadata.ChapterInfo> chapters) {
|
||||
if (chapters == null || chapters.isEmpty()) {
|
||||
return null;
|
||||
|
||||
+16
-1
@@ -167,12 +167,27 @@ public class AudiobookMetadataExtractor implements FileMetadataExtractor {
|
||||
}
|
||||
}
|
||||
|
||||
public Long extractDurationMillis(File audioFile) {
|
||||
try {
|
||||
AudioHeader header = AudioFileIO.read(audioFile).getAudioHeader();
|
||||
if (header == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
long durationMillis = Math.round(header.getPreciseTrackLength() * 1000);
|
||||
return durationMillis > 0 ? durationMillis : null;
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to read duration from audio file {}: {}", audioFile.getName(), e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void extractAudioHeaderInfo(AudiobookMetadata.AudiobookMetadataBuilder builder, AudioHeader header) {
|
||||
if (header == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
long durationSeconds = (long) header.getPreciseTrackLength();
|
||||
long durationSeconds = Math.round(header.getPreciseTrackLength());
|
||||
if (durationSeconds > 0) {
|
||||
builder.durationSeconds(durationSeconds);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user