2021-08-18 00:01:11 +02:00
|
|
|
const Path = require('path')
|
|
|
|
const Logger = require('../Logger')
|
2021-08-26 00:36:54 +02:00
|
|
|
const prober = require('./prober')
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
function getDefaultAudioStream(audioStreams) {
|
|
|
|
if (audioStreams.length === 1) return audioStreams[0]
|
|
|
|
var defaultStream = audioStreams.find(a => a.is_default)
|
|
|
|
if (!defaultStream) return audioStreams[0]
|
|
|
|
return defaultStream
|
|
|
|
}
|
|
|
|
|
|
|
|
async function scan(path) {
|
|
|
|
var probeData = await prober(path)
|
|
|
|
if (!probeData || !probeData.audio_streams || !probeData.audio_streams.length) {
|
|
|
|
return {
|
|
|
|
error: 'Invalid audio file'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!probeData.duration || !probeData.size) {
|
|
|
|
return {
|
|
|
|
error: 'Invalid duration or size'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var audioStream = getDefaultAudioStream(probeData.audio_streams)
|
|
|
|
|
|
|
|
const finalData = {
|
|
|
|
format: probeData.format,
|
|
|
|
duration: probeData.duration,
|
|
|
|
size: probeData.size,
|
|
|
|
bit_rate: audioStream.bit_rate || probeData.bit_rate,
|
|
|
|
codec: audioStream.codec,
|
|
|
|
time_base: audioStream.time_base,
|
|
|
|
language: audioStream.language,
|
|
|
|
channel_layout: audioStream.channel_layout,
|
|
|
|
channels: audioStream.channels,
|
2021-09-08 16:15:54 +02:00
|
|
|
sample_rate: audioStream.sample_rate,
|
|
|
|
chapters: probeData.chapters || []
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const key in probeData) {
|
|
|
|
if (probeData[key] && key.startsWith('file_tag')) {
|
|
|
|
finalData[key] = probeData[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (finalData.file_tag_track) {
|
|
|
|
var track = finalData.file_tag_track
|
|
|
|
var trackParts = track.split('/').map(part => Number(part))
|
|
|
|
if (trackParts.length > 0) {
|
|
|
|
finalData.trackNumber = trackParts[0]
|
|
|
|
}
|
|
|
|
if (trackParts.length > 1) {
|
|
|
|
finalData.trackTotal = trackParts[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return finalData
|
|
|
|
}
|
|
|
|
module.exports.scan = scan
|
|
|
|
|
|
|
|
|
|
|
|
function isNumber(val) {
|
|
|
|
return !isNaN(val) && val !== null
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTrackNumberFromMeta(scanData) {
|
2021-09-06 23:11:37 +02:00
|
|
|
return !isNaN(scanData.trackNumber) && scanData.trackNumber !== null ? Math.trunc(Number(scanData.trackNumber)) : null
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:02:42 +02:00
|
|
|
function getTrackNumberFromFilename(title, author, series, publishYear, filename) {
|
2021-08-18 00:01:11 +02:00
|
|
|
var partbasename = Path.basename(filename, Path.extname(filename))
|
2021-09-05 01:02:42 +02:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
var numbersinpath = partbasename.match(/\d+/g)
|
|
|
|
if (!numbersinpath) return null
|
|
|
|
|
|
|
|
var number = numbersinpath.length ? parseInt(numbersinpath[0]) : null
|
|
|
|
return number
|
|
|
|
}
|
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
async function scanAudioFiles(audiobook, newAudioFiles) {
|
|
|
|
if (!newAudioFiles || !newAudioFiles.length) {
|
|
|
|
Logger.error('[AudioFileScanner] Scan Audio Files no files', audiobook.title)
|
2021-08-18 00:01:11 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var tracks = []
|
2021-09-18 01:40:30 +02:00
|
|
|
var numDuplicateTracks = 0
|
|
|
|
var numInvalidTracks = 0
|
2021-08-26 00:36:54 +02:00
|
|
|
for (let i = 0; i < newAudioFiles.length; i++) {
|
|
|
|
var audioFile = newAudioFiles[i]
|
|
|
|
var scanData = await scan(audioFile.fullPath)
|
2021-08-18 00:01:11 +02:00
|
|
|
if (!scanData || scanData.error) {
|
2021-08-26 00:36:54 +02:00
|
|
|
Logger.error('[AudioFileScanner] Scan failed for', audioFile.path)
|
|
|
|
// audiobook.invalidAudioFiles.push(parts[i])
|
2021-08-18 00:01:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var trackNumFromMeta = getTrackNumberFromMeta(scanData)
|
2021-09-05 01:02:42 +02:00
|
|
|
var book = audiobook.book || {}
|
|
|
|
var trackNumFromFilename = getTrackNumberFromFilename(book.title, book.author, book.series, book.publishYear, audioFile.filename)
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2021-08-19 01:31:19 +02:00
|
|
|
var audioFileObj = {
|
2021-08-26 00:36:54 +02:00
|
|
|
ino: audioFile.ino,
|
|
|
|
filename: audioFile.filename,
|
|
|
|
path: audioFile.path,
|
|
|
|
fullPath: audioFile.fullPath,
|
|
|
|
ext: audioFile.ext,
|
2021-08-18 00:01:11 +02:00
|
|
|
...scanData,
|
|
|
|
trackNumFromMeta,
|
|
|
|
trackNumFromFilename
|
|
|
|
}
|
2021-08-26 14:09:23 +02:00
|
|
|
var audioFile = audiobook.addAudioFile(audioFileObj)
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2021-08-20 00:29:36 +02:00
|
|
|
var trackNumber = 1
|
2021-08-26 00:36:54 +02:00
|
|
|
if (newAudioFiles.length > 1) {
|
2021-08-20 00:29:36 +02:00
|
|
|
trackNumber = isNumber(trackNumFromMeta) ? trackNumFromMeta : trackNumFromFilename
|
|
|
|
if (trackNumber === null) {
|
2021-09-18 01:40:30 +02:00
|
|
|
Logger.debug('[AudioFileScanner] Invalid track number for', audioFile.filename)
|
2021-08-26 14:09:23 +02:00
|
|
|
audioFile.invalid = true
|
|
|
|
audioFile.error = 'Failed to get track number'
|
2021-09-18 01:40:30 +02:00
|
|
|
numInvalidTracks++
|
2021-08-18 00:01:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tracks.find(t => t.index === trackNumber)) {
|
2021-09-18 01:40:30 +02:00
|
|
|
Logger.debug('[AudioFileScanner] Duplicate track number for', audioFile.filename)
|
2021-08-26 14:09:23 +02:00
|
|
|
audioFile.invalid = true
|
|
|
|
audioFile.error = 'Duplicate track number'
|
2021-09-18 01:40:30 +02:00
|
|
|
numDuplicateTracks++
|
2021-08-18 00:01:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-08-26 14:09:23 +02:00
|
|
|
audioFile.index = trackNumber
|
|
|
|
tracks.push(audioFile)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!tracks.length) {
|
2021-08-24 14:15:56 +02:00
|
|
|
Logger.warn('[AudioFileScanner] No Tracks for audiobook', audiobook.id)
|
2021-08-18 00:01:11 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-18 01:40:30 +02:00
|
|
|
if (numDuplicateTracks > 0) {
|
|
|
|
Logger.warn(`[AudioFileScanner] ${numDuplicateTracks} Duplicate tracks for "${audiobook.title}"`)
|
|
|
|
}
|
|
|
|
if (numInvalidTracks > 0) {
|
|
|
|
Logger.error(`[AudioFileScanner] ${numDuplicateTracks} Invalid tracks for "${audiobook.title}"`)
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
tracks.sort((a, b) => a.index - b.index)
|
2021-08-20 00:29:36 +02:00
|
|
|
audiobook.audioFiles.sort((a, b) => {
|
|
|
|
var aNum = isNumber(a.trackNumFromMeta) ? a.trackNumFromMeta : isNumber(a.trackNumFromFilename) ? a.trackNumFromFilename : 0
|
|
|
|
var bNum = isNumber(b.trackNumFromMeta) ? b.trackNumFromMeta : isNumber(b.trackNumFromFilename) ? b.trackNumFromFilename : 0
|
|
|
|
return aNum - bNum
|
|
|
|
})
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
// If first index is 0, increment all by 1
|
|
|
|
if (tracks[0].index === 0) {
|
|
|
|
tracks = tracks.map(t => {
|
|
|
|
t.index += 1
|
|
|
|
return t
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:15:56 +02:00
|
|
|
var hasTracksAlready = audiobook.tracks.length
|
2021-08-18 00:01:11 +02:00
|
|
|
tracks.forEach((track) => {
|
|
|
|
audiobook.addTrack(track)
|
|
|
|
})
|
2021-08-24 14:15:56 +02:00
|
|
|
if (hasTracksAlready) {
|
|
|
|
audiobook.tracks.sort((a, b) => a.index - b.index)
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
2021-08-26 00:36:54 +02:00
|
|
|
module.exports.scanAudioFiles = scanAudioFiles
|