mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2024-12-28 09:38:56 +01:00
Add:Smart audio track order based on best match track and disc #155
This commit is contained in:
parent
c22eaae3bf
commit
afd2b8c98f
@ -22,7 +22,8 @@
|
||||
<span class="text-white">Track From Metadata</span>
|
||||
<span class="material-icons ml-1" :class="currentSort === 'metadata' ? 'text-white text-opacity-100 text-lg' : 'text-sm'">{{ currentSort === 'metadata' ? 'expand_more' : 'unfold_more' }}</span>
|
||||
</div>
|
||||
<div class="font-mono w-20 text-center">CD From Filename</div>
|
||||
<div class="font-mono w-20 text-center">Disc From Filename</div>
|
||||
<div class="font-mono w-20 text-center">Disc From Metadata</div>
|
||||
<div class="font-book text-center px-4 flex-grow flex items-center cursor-pointer text-white text-opacity-40 hover:text-opacity-100" @click="sortByFilename" @mousedown.prevent>
|
||||
<span class="text-white">Filename</span>
|
||||
<span class="material-icons ml-1" :class="currentSort === 'filename' ? 'text-white text-opacity-100 text-lg' : 'text-sm'">{{ currentSort === 'filename' ? 'expand_more' : 'unfold_more' }}</span>
|
||||
@ -49,7 +50,10 @@
|
||||
{{ audio.trackNumFromMeta }}
|
||||
</div>
|
||||
<div class="font-book truncate px-4 w-20">
|
||||
{{ audio.cdNumFromFilename }}
|
||||
{{ audio.discNumFromFilename }}
|
||||
</div>
|
||||
<div class="font-book truncate px-4 w-20">
|
||||
{{ audio.discNumFromMeta }}
|
||||
</div>
|
||||
<div class="font-book truncate px-4 flex-grow">
|
||||
{{ audio.filename }}
|
||||
|
@ -14,8 +14,9 @@ class AudioFile {
|
||||
this.addedAt = null
|
||||
|
||||
this.trackNumFromMeta = null
|
||||
this.discNumFromMeta = null
|
||||
this.trackNumFromFilename = null
|
||||
this.cdNumFromFilename = null
|
||||
this.discNumFromFilename = null
|
||||
|
||||
this.format = null
|
||||
this.duration = null
|
||||
@ -42,16 +43,6 @@ class AudioFile {
|
||||
}
|
||||
}
|
||||
|
||||
// Sort number takes cd num into account
|
||||
// get sortNumber() {
|
||||
// if (this.manuallyVerified) return this.index
|
||||
// var num = this.index
|
||||
// if (this.cdNumFromFilename && !isNaN(this.cdNumFromFilename)) {
|
||||
// num += (Number(this.cdNumFromFilename) * 1000)
|
||||
// }
|
||||
// return num
|
||||
// }
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
index: this.index,
|
||||
@ -62,8 +53,9 @@ class AudioFile {
|
||||
fullPath: this.fullPath,
|
||||
addedAt: this.addedAt,
|
||||
trackNumFromMeta: this.trackNumFromMeta,
|
||||
discNumFromMeta: this.discNumFromMeta,
|
||||
trackNumFromFilename: this.trackNumFromFilename,
|
||||
cdNumFromFilename: this.cdNumFromFilename,
|
||||
discNumFromFilename: this.discNumFromFilename,
|
||||
manuallyVerified: !!this.manuallyVerified,
|
||||
invalid: !!this.invalid,
|
||||
exclude: !!this.exclude,
|
||||
@ -97,8 +89,11 @@ class AudioFile {
|
||||
this.error = data.error || null
|
||||
|
||||
this.trackNumFromMeta = data.trackNumFromMeta
|
||||
this.discNumFromMeta = data.discNumFromMeta
|
||||
this.trackNumFromFilename = data.trackNumFromFilename
|
||||
this.cdNumFromFilename = data.cdNumFromFilename
|
||||
|
||||
if (data.cdNumFromFilename !== undefined) this.discNumFromFilename = data.cdNumFromFilename // TEMP:Support old var name
|
||||
else this.discNumFromFilename = data.discNumFromFilename
|
||||
|
||||
this.format = data.format
|
||||
this.duration = data.duration
|
||||
@ -132,8 +127,9 @@ class AudioFile {
|
||||
this.addedAt = Date.now()
|
||||
|
||||
this.trackNumFromMeta = fileData.trackNumFromMeta
|
||||
this.discNumFromMeta = fileData.discNumFromMeta
|
||||
this.trackNumFromFilename = fileData.trackNumFromFilename
|
||||
this.cdNumFromFilename = fileData.cdNumFromFilename
|
||||
this.discNumFromFilename = fileData.discNumFromFilename
|
||||
|
||||
this.format = probeData.format
|
||||
this.duration = probeData.duration
|
||||
|
@ -7,6 +7,7 @@ class AudioFileMetadata {
|
||||
this.tagSeries = null
|
||||
this.tagSeriesPart = null
|
||||
this.tagTrack = null
|
||||
this.tagDisc = null
|
||||
this.tagSubtitle = null
|
||||
this.tagAlbumArtist = null
|
||||
this.tagDate = null
|
||||
@ -42,6 +43,7 @@ class AudioFileMetadata {
|
||||
this.tagSeries = metadata.tagSeries || null
|
||||
this.tagSeriesPart = metadata.tagSeriesPart || null
|
||||
this.tagTrack = metadata.tagTrack || null
|
||||
this.tagDisc = metadata.tagDisc || null
|
||||
this.tagSubtitle = metadata.tagSubtitle || null
|
||||
this.tagAlbumArtist = metadata.tagAlbumArtist || null
|
||||
this.tagDate = metadata.tagDate || null
|
||||
@ -63,6 +65,7 @@ class AudioFileMetadata {
|
||||
this.tagSeries = payload.file_tag_series || null
|
||||
this.tagSeriesPart = payload.file_tag_seriespart || null
|
||||
this.tagTrack = payload.file_tag_track || null
|
||||
this.tagDisc = payload.file_tag_disc || null
|
||||
this.tagSubtitle = payload.file_tag_subtitle || null
|
||||
this.tagAlbumArtist = payload.file_tag_albumartist || null
|
||||
this.tagDate = payload.file_tag_date || null
|
||||
@ -84,6 +87,7 @@ class AudioFileMetadata {
|
||||
tagSeries: payload.file_tag_series || null,
|
||||
tagSeriesPart: payload.file_tag_seriespart || null,
|
||||
tagTrack: payload.file_tag_track || null,
|
||||
tagDisc: payload.file_tag_disc || null,
|
||||
tagSubtitle: payload.file_tag_subtitle || null,
|
||||
tagAlbumArtist: payload.file_tag_albumartist || null,
|
||||
tagDate: payload.file_tag_date || null,
|
||||
|
@ -9,11 +9,7 @@ const { LogLevel } = require('../utils/constants')
|
||||
class AudioFileScanner {
|
||||
constructor() { }
|
||||
|
||||
getTrackNumberFromMeta(scanData) {
|
||||
return !isNaN(scanData.trackNumber) && scanData.trackNumber !== null ? Math.trunc(Number(scanData.trackNumber)) : null
|
||||
}
|
||||
|
||||
getTrackNumberFromFilename(bookScanData, filename) {
|
||||
getTrackAndDiscNumberFromFilename(bookScanData, filename) {
|
||||
const { title, author, series, publishYear } = bookScanData
|
||||
var partbasename = Path.basename(filename, Path.extname(filename))
|
||||
|
||||
@ -23,39 +19,24 @@ class AudioFileScanner {
|
||||
if (series) partbasename = partbasename.replace(series, '')
|
||||
if (publishYear) partbasename = partbasename.replace(publishYear)
|
||||
|
||||
// Remove eg. "disc 1" from path
|
||||
partbasename = partbasename.replace(/\bdisc \d\d?\b/i, '')
|
||||
// Look for disc number
|
||||
var discNumber = null
|
||||
var discMatch = partbasename.match(/\b(disc|cd) ?(\d\d?)\b/i)
|
||||
if (discMatch && discMatch.length > 2 && discMatch[2]) {
|
||||
if (!isNaN(discMatch[2])) {
|
||||
discNumber = Number(discMatch[2])
|
||||
}
|
||||
|
||||
// Remove "cd01" or "cd 01" from path
|
||||
partbasename = partbasename.replace(/\bcd ?\d\d?\b/i, '')
|
||||
// Remove disc number from filename
|
||||
partbasename = partbasename.replace(/\b(disc|cd) ?(\d\d?)\b/i, '')
|
||||
}
|
||||
|
||||
var numbersinpath = partbasename.match(/\d{1,4}/g)
|
||||
if (!numbersinpath) return null
|
||||
|
||||
var number = numbersinpath.length ? parseInt(numbersinpath[0]) : null
|
||||
return number
|
||||
var trackNumber = numbersinpath && numbersinpath.length ? parseInt(numbersinpath[0]) : null
|
||||
return {
|
||||
trackNumber,
|
||||
discNumber
|
||||
}
|
||||
|
||||
getCdNumberFromFilename(bookScanData, filename) {
|
||||
const { title, author, series, publishYear } = bookScanData
|
||||
var partbasename = Path.basename(filename, Path.extname(filename))
|
||||
|
||||
// Remove title, author, series, and publishYear from filename if there
|
||||
if (title) partbasename = partbasename.replace(title, '')
|
||||
if (author) partbasename = partbasename.replace(author, '')
|
||||
if (series) partbasename = partbasename.replace(series, '')
|
||||
if (publishYear) partbasename = partbasename.replace(publishYear)
|
||||
|
||||
var cdNumber = null
|
||||
|
||||
var cdmatch = partbasename.match(/\b(disc|cd) ?(\d\d?)\b/i)
|
||||
if (cdmatch && cdmatch.length > 2 && cdmatch[2]) {
|
||||
if (!isNaN(cdmatch[2])) {
|
||||
cdNumber = Number(cdmatch[2])
|
||||
}
|
||||
}
|
||||
|
||||
return cdNumber
|
||||
}
|
||||
|
||||
getAverageScanDurationMs(results) {
|
||||
@ -76,20 +57,21 @@ class AudioFileScanner {
|
||||
// Logger.debug(`[AudioFileScanner] Finished Probe ${audioFileData.fullPath} elapsed ${msToTimestamp(Date.now() - probeStart, true)}`)
|
||||
|
||||
var audioFile = new AudioFile()
|
||||
audioFileData.trackNumFromMeta = this.getTrackNumberFromMeta(probeData)
|
||||
audioFileData.trackNumFromFilename = this.getTrackNumberFromFilename(bookScanData, audioFileData.filename)
|
||||
audioFileData.cdNumFromFilename = this.getCdNumberFromFilename(bookScanData, audioFileData.filename)
|
||||
audioFile.setDataFromProbe(audioFileData, probeData)
|
||||
if (audioFile.embeddedCoverArt) {
|
||||
audioFileData.trackNumFromMeta = probeData.trackNumber
|
||||
audioFileData.discNumFromMeta = probeData.discNumber
|
||||
|
||||
const { trackNumber, discNumber } = this.getTrackAndDiscNumberFromFilename(bookScanData, audioFileData.filename)
|
||||
audioFileData.trackNumFromFilename = trackNumber
|
||||
audioFileData.discNumFromFilename = discNumber
|
||||
|
||||
audioFile.setDataFromProbe(audioFileData, probeData)
|
||||
|
||||
}
|
||||
return {
|
||||
audioFile,
|
||||
elapsed: Date.now() - probeStart
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Returns array of { AudioFile, elapsed, averageScanDuration } from audio file scan objects
|
||||
async executeAudioFileScans(audioFileDataArray, bookScanData) {
|
||||
var proms = []
|
||||
@ -105,6 +87,84 @@ class AudioFileScanner {
|
||||
}
|
||||
}
|
||||
|
||||
isSequential(nums) {
|
||||
if (!nums || !nums.length) return false
|
||||
if (nums.length === 1) return true
|
||||
var prev = nums[0]
|
||||
for (let i = 1; i < nums.length; i++) {
|
||||
if (nums[i] - prev > 1) return false
|
||||
prev = nums[i]
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
removeDupes(nums) {
|
||||
if (!nums || !nums.length) return []
|
||||
if (nums.length === 1) return nums
|
||||
|
||||
var nodupes = [nums[0]]
|
||||
nums.forEach((num) => {
|
||||
if (num > nodupes[nodupes.length - 1]) nodupes.push(num)
|
||||
})
|
||||
return nodupes
|
||||
}
|
||||
|
||||
// Must be all audiofiles in audiobook
|
||||
runSmartTrackOrder(audiobook, audioFiles) {
|
||||
var discsFromFilename = []
|
||||
var tracksFromFilename = []
|
||||
var discsFromMeta = []
|
||||
var tracksFromMeta = []
|
||||
|
||||
audioFiles.forEach((af) => {
|
||||
if (af.discNumFromFilename !== null) discsFromFilename.push(af.discNumFromFilename)
|
||||
if (af.discNumFromMeta !== null) discsFromMeta.push(af.discNumFromMeta)
|
||||
if (af.trackNumFromFilename !== null) tracksFromFilename.push(af.trackNumFromFilename)
|
||||
if (af.trackNumFromMeta !== null) tracksFromMeta.push(af.trackNumFromMeta)
|
||||
af.validateTrackIndex() // Sets error if no valid track number
|
||||
})
|
||||
discsFromFilename.sort((a, b) => a - b)
|
||||
discsFromMeta.sort((a, b) => a - b)
|
||||
tracksFromFilename.sort((a, b) => a - b)
|
||||
tracksFromMeta.sort((a, b) => a - b)
|
||||
console.log('AB DISCS', audiobook.title, discsFromFilename, discsFromMeta)
|
||||
console.log('AB TRACKS', audiobook.title, tracksFromFilename, tracksFromMeta)
|
||||
|
||||
var discKey = null
|
||||
if (discsFromMeta.length === audioFiles.length && this.isSequential(discsFromMeta)) {
|
||||
discKey = 'discNumFromMeta'
|
||||
} else if (discsFromFilename.length === audioFiles.length && this.isSequential(discsFromFilename)) {
|
||||
discKey = 'discNumFromFilename'
|
||||
}
|
||||
|
||||
var trackKey = null
|
||||
tracksFromFilename = this.removeDupes(tracksFromFilename)
|
||||
tracksFromMeta = this.removeDupes(tracksFromMeta)
|
||||
if (tracksFromFilename.length > tracksFromMeta.length) {
|
||||
trackKey = 'trackNumFromFilename'
|
||||
} else {
|
||||
trackKey = 'trackNumFromMeta'
|
||||
}
|
||||
|
||||
|
||||
if (discKey !== null) {
|
||||
Logger.debug(`[AudioFileScanner] Smart track order for "${audiobook.title}" using disc key ${discKey} and track key ${trackKey}`)
|
||||
audioFiles.sort((a, b) => {
|
||||
let Dx = a[discKey] - b[discKey]
|
||||
if (Dx === 0) Dx = a[trackKey] - b[trackKey]
|
||||
return Dx
|
||||
})
|
||||
} else {
|
||||
Logger.debug(`[AudioFileScanner] Smart track order for "${audiobook.title}" using track key ${trackKey}`)
|
||||
audioFiles.sort((a, b) => a[trackKey] - b[trackKey])
|
||||
}
|
||||
|
||||
for (let i = 0; i < audioFiles.length; i++) {
|
||||
audioFiles[i].index = i + 1
|
||||
audiobook.addAudioFile(audioFiles[i])
|
||||
}
|
||||
}
|
||||
|
||||
async scanAudioFiles(audioFileDataArray, bookScanData, audiobook, preferAudioMetadata, libraryScan = null) {
|
||||
var hasUpdated = false
|
||||
|
||||
@ -114,9 +174,14 @@ class AudioFileScanner {
|
||||
libraryScan.addLog(LogLevel.DEBUG, `Book "${bookScanData.path}" Audio file scan took ${audioScanResult.elapsed}ms for ${audioScanResult.audioFiles.length} with average time of ${audioScanResult.averageScanDuration}ms`)
|
||||
}
|
||||
|
||||
var totalAudioFilesToInclude = audiobook.audioFilesToInclude.filter(af => !audioScanResult.audioFiles.find(_af => _af.ino === af.ino)).length + audioScanResult.audioFiles.length
|
||||
var numExistingAudioFilesToInclude = audiobook.audioFilesToInclude.filter(af => !audioScanResult.audioFiles.find(_af => _af.ino === af.ino)).length
|
||||
var totalAudioFilesToInclude = numExistingAudioFilesToInclude + audioScanResult.audioFiles.length
|
||||
|
||||
// validate & add/update audio files to audiobook
|
||||
if (numExistingAudioFilesToInclude <= 0) { // SMART TRACK ORDER for New or empty audiobooks
|
||||
this.runSmartTrackOrder(audiobook, audioScanResult.audioFiles)
|
||||
hasUpdated = true
|
||||
} else {
|
||||
// validate & add/update audio files to existing audiobook
|
||||
for (let i = 0; i < audioScanResult.audioFiles.length; i++) {
|
||||
var newAF = audioScanResult.audioFiles[i]
|
||||
var existingAF = audiobook.getAudioFileByIno(newAF.ino)
|
||||
@ -146,6 +211,7 @@ class AudioFileScanner {
|
||||
hasUpdated = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set book details from audio file ID3 tags, optional prefer
|
||||
if (audiobook.setDetailsFromFileMetadata(preferAudioMetadata)) {
|
||||
|
@ -19,6 +19,9 @@ class AudioProbeData {
|
||||
|
||||
this.trackNumber = null
|
||||
this.trackTotal = null
|
||||
|
||||
this.discNumber = null
|
||||
this.discTotal = null
|
||||
}
|
||||
|
||||
getEmbeddedCoverArt(videoStream) {
|
||||
@ -55,10 +58,22 @@ class AudioProbeData {
|
||||
if (this.audioFileMetadata.tagTrack) {
|
||||
var trackParts = this.audioFileMetadata.tagTrack.split('/').map(part => Number(part))
|
||||
if (trackParts.length > 0) {
|
||||
this.trackNumber = trackParts[0]
|
||||
// Fractional track numbers not supported
|
||||
this.trackNumber = !isNaN(trackParts[0]) ? Math.trunc(trackParts[0]) : null
|
||||
}
|
||||
if (trackParts.length > 1) {
|
||||
this.trackTotal = trackParts[1]
|
||||
this.trackTotal = !isNaN(trackParts[1]) ? trackParts[1] : null
|
||||
}
|
||||
}
|
||||
|
||||
// Parse disc tag
|
||||
if (this.audioFileMetadata.tagDisc) {
|
||||
var discParts = this.audioFileMetadata.tagDisc.split('/').map(p => Number(p))
|
||||
if (discParts.length > 0) {
|
||||
this.discNumber = !isNaN(discParts[0]) ? Math.trunc(discParts[0]) : null
|
||||
}
|
||||
if (discParts.length > 1) {
|
||||
this.discTotal = !isNaN(discParts[1]) ? discParts[1] : null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -128,7 +128,7 @@ function parseMediaStreamInfo(stream, all_streams, total_bit_rate) {
|
||||
function parseChapters(chapters) {
|
||||
if (!chapters) return []
|
||||
return chapters.map(chap => {
|
||||
var title = chap['TAG:title'] || chap.title || chap.tags.title || ''
|
||||
var title = chap['TAG:title'] || chap.title || chap.tags.title || ''
|
||||
var timebase = chap.time_base && chap.time_base.includes('/') ? Number(chap.time_base.split('/')[1]) : 1
|
||||
return {
|
||||
id: chap.id,
|
||||
@ -152,6 +152,7 @@ function parseTags(format, verbose) {
|
||||
file_tag_title: tryGrabTags(format, 'title', 'tit2', 'tt2'),
|
||||
file_tag_subtitle: tryGrabTags(format, 'subtitle', 'tit3', 'tt3'),
|
||||
file_tag_track: tryGrabTags(format, 'track', 'trck', 'trk'),
|
||||
file_tag_disc: tryGrabTags(format, 'discnumber', 'disc', 'disk', 'tpos'),
|
||||
file_tag_album: tryGrabTags(format, 'album', 'talb', 'tal'),
|
||||
file_tag_artist: tryGrabTags(format, 'artist', 'tpe1', 'tp1'),
|
||||
file_tag_albumartist: tryGrabTags(format, 'albumartist', 'album_artist', 'tpe2'),
|
||||
|
Loading…
Reference in New Issue
Block a user