logLevel as server setting, logger config page, re-scan audiobook option, fix embedded cover extraction, flac and mobi support, fix series bookshelf not wrapping

This commit is contained in:
Mark Cooper 2021-09-30 18:52:32 -05:00
parent dc18eb408e
commit d6cab8e591
28 changed files with 684 additions and 113 deletions

View file

@ -1,3 +1,4 @@
const Logger = require('../Logger')
const AudioFileMetadata = require('./AudioFileMetadata')
class AudioFile {
@ -33,6 +34,9 @@ class AudioFile {
this.exclude = false
this.error = null
// TEMP: For forcing rescan
this.isOldAudioFile = false
if (data) {
this.construct(data)
}
@ -58,6 +62,7 @@ class AudioFile {
size: this.size,
bitRate: this.bitRate,
language: this.language,
codec: this.codec,
timeBase: this.timeBase,
channels: this.channels,
channelLayout: this.channelLayout,
@ -88,7 +93,7 @@ class AudioFile {
this.size = data.size
this.bitRate = data.bitRate
this.language = data.language
this.codec = data.codec
this.codec = data.codec || null
this.timeBase = data.timeBase
this.channels = data.channels
this.channelLayout = data.channelLayout
@ -98,15 +103,11 @@ class AudioFile {
// Old version of AudioFile used `tagAlbum` etc.
var isOldVersion = Object.keys(data).find(key => key.startsWith('tag'))
if (isOldVersion) {
this.isOldAudioFile = true
this.metadata = new AudioFileMetadata(data)
} else {
this.metadata = new AudioFileMetadata(data.metadata || {})
}
// this.tagAlbum = data.tagAlbum
// this.tagArtist = data.tagArtist
// this.tagGenre = data.tagGenre
// this.tagTitle = data.tagTitle
// this.tagTrack = data.tagTrack
}
setData(data) {
@ -131,7 +132,7 @@ class AudioFile {
this.size = data.size
this.bitRate = data.bit_rate || null
this.language = data.language
this.codec = data.codec
this.codec = data.codec || null
this.timeBase = data.time_base
this.channels = data.channels
this.channelLayout = data.channel_layout
@ -142,10 +143,74 @@ class AudioFile {
this.metadata.setData(data)
}
syncChapters(updatedChapters) {
if (this.chapters.length !== updatedChapters.length) {
this.chapters = updatedChapters.map(ch => ({ ...ch }))
return true
} else if (updatedChapters.length === 0) {
if (this.chapters.length > 0) {
this.chapters = []
return true
}
return false
}
var hasUpdates = false
for (let i = 0; i < updatedChapters.length; i++) {
if (JSON.stringify(updatedChapters[i]) !== JSON.stringify(this.chapters[i])) {
hasUpdates = true
}
}
if (hasUpdates) {
this.chapters = updatedChapters.map(ch => ({ ...ch }))
}
return hasUpdates
}
// Called from audioFileScanner.js with scanData
updateMetadata(data) {
if (!this.metadata) this.metadata = new AudioFileMetadata()
var dataMap = {
format: data.format,
duration: data.duration,
size: data.size,
bitRate: data.bit_rate || null,
language: data.language,
codec: data.codec || null,
timeBase: data.time_base,
channels: data.channels,
channelLayout: data.channel_layout,
chapters: data.chapters || [],
embeddedCoverArt: data.embedded_cover_art || null
}
var hasUpdates = false
for (const key in dataMap) {
if (key === 'chapters') {
var chaptersUpdated = this.syncChapters(dataMap.chapters)
if (chaptersUpdated) {
hasUpdates = true
}
} else if (dataMap[key] !== this[key]) {
// Logger.debug(`[AudioFile] "${key}" from ${this[key]} => ${dataMap[key]}`)
this[key] = dataMap[key]
hasUpdates = true
}
}
if (this.metadata.updateData(data)) {
hasUpdates = true
}
return hasUpdates
}
clone() {
return new AudioFile(this.toJSON())
}
// If the file or parent directory was renamed it is synced here
syncFile(newFile) {
var hasUpdates = false
var keysToSync = ['path', 'fullPath', 'ext', 'filename']

View file

@ -65,5 +65,33 @@ class AudioFileMetadata {
this.tagEncoder = payload.file_tag_encoder || null
this.tagEncodedBy = payload.file_tag_encodedby || null
}
updateData(payload) {
const dataMap = {
tagAlbum: payload.file_tag_album || null,
tagArtist: payload.file_tag_artist || null,
tagGenre: payload.file_tag_genre || null,
tagTitle: payload.file_tag_title || null,
tagTrack: payload.file_tag_track || null,
tagSubtitle: payload.file_tag_subtitle || null,
tagAlbumArtist: payload.file_tag_albumartist || null,
tagDate: payload.file_tag_date || null,
tagComposer: payload.file_tag_composer || null,
tagPublisher: payload.file_tag_publisher || null,
tagComment: payload.file_tag_comment || null,
tagDescription: payload.file_tag_description || null,
tagEncoder: payload.file_tag_encoder || null,
tagEncodedBy: payload.file_tag_encodedby || null
}
var hasUpdates = false
for (const key in dataMap) {
if (dataMap[key] !== this[key]) {
this[key] = dataMap[key]
hasUpdates = true
}
}
return hasUpdates
}
}
module.exports = AudioFileMetadata

View file

@ -20,13 +20,6 @@ class AudioTrack {
this.channels = null
this.channelLayout = null
// Storing tags in audio track is unnecessary, tags are stored on audio file
// this.tagAlbum = null
// this.tagArtist = null
// this.tagGenre = null
// this.tagTitle = null
// this.tagTrack = null
if (audioTrack) {
this.construct(audioTrack)
}
@ -50,12 +43,6 @@ class AudioTrack {
this.timeBase = audioTrack.timeBase
this.channels = audioTrack.channels
this.channelLayout = audioTrack.channelLayout
// this.tagAlbum = audioTrack.tagAlbum
// this.tagArtist = audioTrack.tagArtist
// this.tagGenre = audioTrack.tagGenre
// this.tagTitle = audioTrack.tagTitle
// this.tagTrack = audioTrack.tagTrack
}
get name() {
@ -78,11 +65,6 @@ class AudioTrack {
timeBase: this.timeBase,
channels: this.channels,
channelLayout: this.channelLayout,
// tagAlbum: this.tagAlbum,
// tagArtist: this.tagArtist,
// tagGenre: this.tagGenre,
// tagTitle: this.tagTitle,
// tagTrack: this.tagTrack
}
}
@ -104,12 +86,18 @@ class AudioTrack {
this.timeBase = probeData.timeBase
this.channels = probeData.channels
this.channelLayout = probeData.channelLayout
}
// this.tagAlbum = probeData.file_tag_album || null
// this.tagArtist = probeData.file_tag_artist || null
// this.tagGenre = probeData.file_tag_genre || null
// this.tagTitle = probeData.file_tag_title || null
// this.tagTrack = probeData.file_tag_track || null
syncMetadata(audioFile) {
var hasUpdates = false
var keysToSync = ['format', 'duration', 'size', 'bitRate', 'language', 'codec', 'timeBase', 'channels', 'channelLayout']
keysToSync.forEach((key) => {
if (audioFile[key] !== undefined && audioFile[key] !== this[key]) {
hasUpdates = true
this[key] = audioFile[key]
}
})
return hasUpdates
}
syncFile(newFile) {

View file

@ -205,15 +205,22 @@ class Audiobook {
// this function checks all files and sets the inode
async checkUpdateInos() {
var hasUpdates = false
// Audiobook folder needs inode
if (!this.ino) {
this.ino = await getIno(this.fullPath)
hasUpdates = true
}
// Check audio files have an inode
for (let i = 0; i < this.audioFiles.length; i++) {
var af = this.audioFiles[i]
var at = this.tracks.find(t => t.ino === af.ino)
if (!at) {
at = this.tracks.find(t => comparePaths(t.path, af.path))
if (!at && !af.exclude) {
Logger.warn(`[Audiobook] No matching track for audio file "${af.filename}"`)
}
}
if (!af.ino || af.ino === this.ino) {
af.ino = await getIno(af.fullPath)
@ -229,6 +236,7 @@ class Audiobook {
hasUpdates = true
}
}
for (let i = 0; i < this.tracks.length; i++) {
var at = this.tracks[i]
if (!at.ino) {
@ -252,6 +260,7 @@ class Audiobook {
}
}
}
for (let i = 0; i < this.otherFiles.length; i++) {
var file = this.otherFiles[i]
if (!file.ino || file.ino === this.ino) {
@ -267,6 +276,11 @@ class Audiobook {
return hasUpdates
}
// Scans in v1.3.0 or lower will need to rescan audiofiles to pickup metadata and embedded cover
checkNeedsAudioFileRescan() {
return !!(this.audioFiles || []).find(af => af.isOldAudioFile || af.codec === null)
}
setData(data) {
this.id = (Math.trunc(Math.random() * 1000) + Date.now()).toString(36)
this.ino = data.ino || null
@ -409,19 +423,22 @@ class Audiobook {
}
// On scan check other files found with other files saved
async syncOtherFiles(newOtherFiles) {
async syncOtherFiles(newOtherFiles, forceRescan = false) {
var hasUpdates = false
var currOtherFileNum = this.otherFiles.length
var alreadyHadDescTxt = this.otherFiles.find(of => of.filename === 'desc.txt')
var newOtherFilePaths = newOtherFiles.map(f => f.path)
this.otherFiles = this.otherFiles.filter(f => newOtherFilePaths.includes(f.path))
// Some files are not there anymore and filtered out
if (currOtherFileNum !== this.otherFiles.length) hasUpdates = true
// If desc.txt is new or forcing rescan then read it and update description if empty
var descriptionTxt = newOtherFiles.find(file => file.filename === 'desc.txt')
if (descriptionTxt) {
if (descriptionTxt && (!alreadyHadDescTxt || forceRescan)) {
var newDescription = await readTextFile(descriptionTxt.fullPath)
if (newDescription) {
Logger.debug(`[Audiobook] Sync Other File desc.txt: ${newDescription}`)

View file

@ -1,4 +1,5 @@
const { CoverDestination } = require('../utils/constants')
const Logger = require('../Logger')
class ServerSettings {
constructor(settings) {
@ -11,6 +12,7 @@ class ServerSettings {
this.saveMetadataFile = false
this.rateLimitLoginRequests = 10
this.rateLimitLoginWindow = 10 * 60 * 1000 // 10 Minutes
this.logLevel = Logger.logLevel
if (settings) {
this.construct(settings)
@ -25,6 +27,11 @@ class ServerSettings {
this.saveMetadataFile = !!settings.saveMetadataFile
this.rateLimitLoginRequests = !isNaN(settings.rateLimitLoginRequests) ? Number(settings.rateLimitLoginRequests) : 10
this.rateLimitLoginWindow = !isNaN(settings.rateLimitLoginWindow) ? Number(settings.rateLimitLoginWindow) : 10 * 60 * 1000 // 10 Minutes
this.logLevel = settings.logLevel || Logger.logLevel
if (this.logLevel !== Logger.logLevel) {
Logger.setLogLevel(this.logLevel)
}
}
toJSON() {
@ -36,7 +43,8 @@ class ServerSettings {
coverDestination: this.coverDestination,
saveMetadataFile: !!this.saveMetadataFile,
rateLimitLoginRequests: this.rateLimitLoginRequests,
rateLimitLoginWindow: this.rateLimitLoginWindow
rateLimitLoginWindow: this.rateLimitLoginWindow,
logLevel: this.logLevel
}
}
@ -44,6 +52,9 @@ class ServerSettings {
var hasUpdates = false
for (const key in payload) {
if (this[key] !== payload[key]) {
if (key === 'logLevel') {
Logger.setLogLevel(payload[key])
}
this[key] = payload[key]
hasUpdates = true
}

View file

@ -16,7 +16,6 @@ class Stream extends EventEmitter {
this.audiobook = audiobook
this.segmentLength = 6
this.segmentBasename = 'output-%d.ts'
this.streamPath = Path.join(streamPath, this.id)
this.concatFilesPath = Path.join(this.streamPath, 'files.txt')
this.playlistPath = Path.join(this.streamPath, 'output.m3u8')
@ -51,6 +50,16 @@ class Stream extends EventEmitter {
return this.audiobook.totalDuration
}
get hlsSegmentType() {
var hasFlac = this.tracks.find(t => t.ext.toLowerCase() === '.flac')
return hasFlac ? 'fmp4' : 'mpegts'
}
get segmentBasename() {
if (this.hlsSegmentType === 'fmp4') return 'output-%d.m4s'
return 'output-%d.ts'
}
get segmentStartNumber() {
if (!this.startTime) return 0
return Math.floor(this.startTime / this.segmentLength)
@ -98,7 +107,7 @@ class Stream extends EventEmitter {
var userAudiobook = clientUserAudiobooks[this.audiobookId] || null
if (userAudiobook) {
var timeRemaining = this.totalDuration - userAudiobook.currentTime
Logger.info('[STREAM] User has progress for audiobook', userAudiobook, `Time Remaining: ${timeRemaining}s`)
Logger.info('[STREAM] User has progress for audiobook', userAudiobook.progress, `Time Remaining: ${timeRemaining}s`)
if (timeRemaining > 15) {
this.startTime = userAudiobook.currentTime
this.clientCurrentTime = this.startTime
@ -133,7 +142,7 @@ class Stream extends EventEmitter {
async generatePlaylist() {
fs.ensureDirSync(this.streamPath)
await hlsPlaylistGenerator(this.playlistPath, 'output', this.totalDuration, this.segmentLength)
await hlsPlaylistGenerator(this.playlistPath, 'output', this.totalDuration, this.segmentLength, this.hlsSegmentType)
return this.clientPlaylistUri
}
@ -142,7 +151,7 @@ class Stream extends EventEmitter {
var files = await fs.readdir(this.streamPath)
files.forEach((file) => {
var extname = Path.extname(file)
if (extname === '.ts') {
if (extname === '.ts' || extname === '.m4s') {
var basename = Path.basename(file, extname)
var num_part = basename.split('-')[1]
var part_num = Number(num_part)
@ -238,24 +247,31 @@ class Stream extends EventEmitter {
}
const logLevel = process.env.NODE_ENV === 'production' ? 'error' : 'warning'
const audioCodec = this.hlsSegmentType === 'fmp4' ? 'aac' : 'copy'
this.ffmpeg.addOption([
`-loglevel ${logLevel}`,
'-map 0:a',
'-c:a copy'
`-c:a ${audioCodec}`
])
this.ffmpeg.addOption([
const hlsOptions = [
'-f hls',
"-copyts",
"-avoid_negative_ts disabled",
"-max_delay 5000000",
"-max_muxing_queue_size 2048",
`-hls_time 6`,
"-hls_segment_type mpegts",
`-hls_segment_type ${this.hlsSegmentType}`,
`-start_number ${this.segmentStartNumber}`,
"-hls_playlist_type vod",
"-hls_list_size 0",
"-hls_allow_cache 0"
])
]
if (this.hlsSegmentType === 'fmp4') {
hlsOptions.push('-strict -2')
var fmp4InitFilename = Path.join(this.streamPath, 'init.mp4')
hlsOptions.push(`-hls_fmp4_init_filename ${fmp4InitFilename}`)
}
this.ffmpeg.addOption(hlsOptions)
var segmentFilename = Path.join(this.streamPath, this.segmentBasename)
this.ffmpeg.addOption(`-hls_segment_filename ${segmentFilename}`)
this.ffmpeg.output(this.finalPlaylistPath)