Fixes + add progress to m4b and embed tools

This commit is contained in:
mikiher 2024-07-20 12:28:06 +03:00
parent b6875a44cf
commit 10f5f331d7
11 changed files with 530 additions and 211 deletions

View File

@ -10,7 +10,8 @@
<button v-else class="abs-btn outline-none rounded-md shadow-md relative border border-gray-600" :disabled="disabled || loading" :type="type" :class="classList" @mousedown.prevent @click="click">
<slot />
<div v-if="loading" class="text-white absolute top-0 left-0 w-full h-full flex items-center justify-center text-opacity-100">
<svg class="animate-spin" style="width: 24px; height: 24px" viewBox="0 0 24 24">
<span v-if="progress">{{ progress }}</span>
<svg v-else class="animate-spin" style="width: 24px; height: 24px" viewBox="0 0 24 24">
<path fill="currentColor" d="M12,4V2A10,10 0 0,0 2,12H4A8,8 0 0,1 12,4Z" />
</svg>
</div>
@ -33,7 +34,8 @@ export default {
paddingY: Number,
small: Boolean,
loading: Boolean,
disabled: Boolean
disabled: Boolean,
progress: String
},
data() {
return {}

View File

@ -212,6 +212,16 @@ export default {
this.libraryItemAdded(ab)
})
},
trackStarted(data) {
this.$store.commit('tasks/updateAudioFilesEncoding', { libraryItemId: data.libraryItemId, ino: data.ino, progress: '0%' })
},
trackProgress(data) {
this.$store.commit('tasks/updateAudioFilesEncoding', { libraryItemId: data.libraryItemId, ino: data.ino, progress: `${Math.round(data.progress)}%` })
},
trackFinished(data) {
this.$store.commit('tasks/updateAudioFilesEncoding', { libraryItemId: data.libraryItemId, ino: data.ino, progress: '100%' })
this.$store.commit('tasks/updateAudioFilesFinished', { libraryItemId: data.libraryItemId, ino: data.ino, finished: true })
},
taskStarted(task) {
console.log('Task started', task)
this.$store.commit('tasks/addUpdateTask', task)
@ -220,6 +230,9 @@ export default {
console.log('Task finished', task)
this.$store.commit('tasks/addUpdateTask', task)
},
taskProgress(data) {
this.$store.commit('tasks/updateTaskProgress', { libraryItemId: data.libraryItemId, progress: `${Math.round(data.progress)}%` })
},
metadataEmbedQueueUpdate(data) {
if (data.queued) {
this.$store.commit('tasks/addQueuedEmbedLId', data.libraryItemId)
@ -406,6 +419,10 @@ export default {
this.socket.on('task_started', this.taskStarted)
this.socket.on('task_finished', this.taskFinished)
this.socket.on('metadata_embed_queue_update', this.metadataEmbedQueueUpdate)
this.socket.on('track_started', this.trackStarted)
this.socket.on('track_finished', this.trackFinished)
this.socket.on('track_progress', this.trackProgress)
this.socket.on('task_progress', this.taskProgress)
// EReader Device Listeners
this.socket.on('ereader-devices-updated', this.ereaderDevicesUpdated)

View File

@ -71,7 +71,8 @@
<div class="flex-grow" />
<ui-btn v-if="!isTaskFinished" color="primary" :loading="processing" @click.stop="embedClick">{{ $strings.ButtonStartMetadataEmbed }}</ui-btn>
<ui-btn v-if="!isTaskFinished" color="primary" :loading="processing" :progress="progress" @click.stop="embedClick">{{ $strings.ButtonStartMetadataEmbed }}</ui-btn>
<p v-else-if="taskFailed" class="text-error text-lg font-semibold">{{ $strings.MessageEmbedFailed }} {{ taskError }}</p>
<p v-else class="text-success text-lg font-semibold">{{ $strings.MessageEmbedFinished }}</p>
</div>
<!-- m4b embed action buttons -->
@ -83,7 +84,7 @@
<div class="flex-grow" />
<ui-btn v-if="!isTaskFinished && processing" color="error" :loading="isCancelingEncode" class="mr-2" @click.stop="cancelEncodeClick">{{ $strings.ButtonCancelEncode }}</ui-btn>
<ui-btn v-if="!isTaskFinished" color="primary" :loading="processing" @click.stop="encodeM4bClick">{{ $strings.ButtonStartM4BEncode }}</ui-btn>
<ui-btn v-if="!isTaskFinished" color="primary" :loading="processing" :progress="progress" @click.stop="encodeM4bClick">{{ $strings.ButtonStartM4BEncode }}</ui-btn>
<p v-else-if="taskFailed" class="text-error text-lg font-semibold">{{ $strings.MessageM4BFailed }} {{ taskError }}</p>
<p v-else class="text-success text-lg font-semibold">{{ $strings.MessageM4BFinished }}</p>
</div>
@ -159,9 +160,9 @@
</div>
<div class="w-24">
<div class="flex justify-center">
<span v-if="audiofilesFinished[file.ino]" class="material-symbols text-xl text-success leading-none">check_circle</span>
<div v-else-if="audiofilesEncoding[file.ino]">
<widgets-loading-spinner />
<span v-if="audioFilesFinished[file.ino]" class="material-symbols text-xl text-success leading-none">check_circle</span>
<div v-else-if="audioFilesEncoding[file.ino]">
<span class="font-mono text-success leading-none">{{ audioFilesEncoding[file.ino] }}</span>
</div>
</div>
</div>
@ -205,8 +206,6 @@ export default {
data() {
return {
processing: false,
audiofilesEncoding: {},
audiofilesFinished: {},
metadataObject: null,
selectedTool: 'embed',
isCancelingEncode: false,
@ -229,6 +228,15 @@ export default {
}
},
computed: {
audioFilesEncoding() {
return this.$store.getters['tasks/getAudioFilesEncoding'](this.libraryItemId) || {}
},
audioFilesFinished() {
return this.$store.getters['tasks/getAudioFilesFinished'](this.libraryItemId) || {}
},
progress() {
return this.$store.getters['tasks/getTaskProgress'](this.libraryItemId) || '0%'
},
isEmbedTool() {
return this.selectedTool === 'embed'
},
@ -372,15 +380,6 @@ export default {
this.processing = false
})
},
audiofileMetadataStarted(data) {
if (data.libraryItemId !== this.libraryItemId) return
this.$set(this.audiofilesEncoding, data.ino, true)
},
audiofileMetadataFinished(data) {
if (data.libraryItemId !== this.libraryItemId) return
this.$set(this.audiofilesEncoding, data.ino, false)
this.$set(this.audiofilesFinished, data.ino, true)
},
selectedToolUpdated() {
let newurl = window.location.protocol + '//' + window.location.host + window.location.pathname + `?tool=${this.selectedTool}`
window.history.replaceState({ path: newurl }, '', newurl)
@ -416,12 +415,6 @@ export default {
},
mounted() {
this.init()
this.$root.socket.on('audiofile_metadata_started', this.audiofileMetadataStarted)
this.$root.socket.on('audiofile_metadata_finished', this.audiofileMetadataFinished)
},
beforeDestroy() {
this.$root.socket.off('audiofile_metadata_started', this.audiofileMetadataStarted)
this.$root.socket.off('audiofile_metadata_finished', this.audiofileMetadataFinished)
}
}
</script>

View File

@ -1,34 +1,60 @@
import Vue from 'vue'
export const state = () => ({
tasks: [],
queuedEmbedLIds: []
queuedEmbedLIds: [],
audioFilesEncoding: {},
audioFilesFinished: {},
taskProgress: {}
})
export const getters = {
getTasksByLibraryItemId: (state) => (libraryItemId) => {
return state.tasks.filter(t => t.data?.libraryItemId === libraryItemId)
return state.tasks.filter((t) => t.data?.libraryItemId === libraryItemId)
},
getRunningLibraryScanTask: (state) => (libraryId) => {
const libraryScanActions = ['library-scan', 'library-match-all']
return state.tasks.find(t => libraryScanActions.includes(t.action) && t.data?.libraryId === libraryId && !t.isFinished)
return state.tasks.find((t) => libraryScanActions.includes(t.action) && t.data?.libraryId === libraryId && !t.isFinished)
},
getAudioFilesEncoding: (state) => (libraryItemId) => {
return state.audioFilesEncoding[libraryItemId]
},
getAudioFilesFinished: (state) => (libraryItemId) => {
return state.audioFilesFinished[libraryItemId]
},
getTaskProgress: (state) => (libraryItemId) => {
return state.taskProgress[libraryItemId]
}
}
export const actions = {
}
export const actions = {}
export const mutations = {
updateAudioFilesEncoding(state, payload) {
if (!state.audioFilesEncoding[payload.libraryItemId]) {
Vue.set(state.audioFilesEncoding, payload.libraryItemId, {})
}
Vue.set(state.audioFilesEncoding[payload.libraryItemId], payload.ino, payload.progress)
},
updateAudioFilesFinished(state, payload) {
if (!state.audioFilesFinished[payload.libraryItemId]) {
Vue.set(state.audioFilesFinished, payload.libraryItemId, {})
}
Vue.set(state.audioFilesFinished[payload.libraryItemId], payload.ino, payload.finished)
},
updateTaskProgress(state, payload) {
Vue.set(state.taskProgress, payload.libraryItemId, payload.progress)
},
setTasks(state, tasks) {
state.tasks = tasks
},
addUpdateTask(state, task) {
const index = state.tasks.findIndex(d => d.id === task.id)
const index = state.tasks.findIndex((d) => d.id === task.id)
if (index >= 0) {
state.tasks.splice(index, 1, task)
} else {
// Remove duplicate (only have one library item per action)
state.tasks = state.tasks.filter(_task => {
state.tasks = state.tasks.filter((_task) => {
if (!_task.data?.libraryItemId || _task.action !== task.action) return true
return _task.data.libraryItemId !== task.data.libraryItemId
})
@ -37,17 +63,17 @@ export const mutations = {
}
},
removeTask(state, task) {
state.tasks = state.tasks.filter(d => d.id !== task.id)
state.tasks = state.tasks.filter((d) => d.id !== task.id)
},
setQueuedEmbedLIds(state, libraryItemIds) {
state.queuedEmbedLIds = libraryItemIds
},
addQueuedEmbedLId(state, libraryItemId) {
if (!state.queuedEmbedLIds.some(lid => lid === libraryItemId)) {
if (!state.queuedEmbedLIds.some((lid) => lid === libraryItemId)) {
state.queuedEmbedLIds.push(libraryItemId)
}
},
removeQueuedEmbedLId(state, libraryItemId) {
state.queuedEmbedLIds = state.queuedEmbedLIds.filter(lid => lid !== libraryItemId)
state.queuedEmbedLIds = state.queuedEmbedLIds.filter((lid) => lid !== libraryItemId)
}
}

View File

@ -659,6 +659,7 @@
"MessageDownloadingEpisode": "Downloading episode",
"MessageDragFilesIntoTrackOrder": "Drag files into correct track order",
"MessageEmbedFinished": "Embed Finished!",
"MessageEmbedFailed": "Embed Failed!",
"MessageEpisodesQueuedForDownload": "{0} Episode(s) queued for download",
"MessageEreaderDevices": "To ensure delivery of ebooks, you may need to add the above email address as a valid sender for each device listed below.",
"MessageFeedURLWillBe": "Feed URL will be {0}",

View File

@ -1,12 +1,14 @@
const Path = require('path')
const fs = require('../libs/fsExtra')
const workerThreads = require('worker_threads')
const Logger = require('../Logger')
const TaskManager = require('./TaskManager')
const Task = require('../objects/Task')
const { writeConcatFile } = require('../utils/ffmpegHelpers')
const ffmpegHelpers = require('../utils/ffmpegHelpers')
const Ffmpeg = require('../libs/fluentFfmpeg')
const SocketAuthority = require('../SocketAuthority')
const fileUtils = require('../utils/fileUtils')
const TrackProgressMonitor = require('../objects/TrackProgressMonitor')
class AbMergeManager {
constructor() {
@ -20,6 +22,7 @@ class AbMergeManager {
}
cancelEncode(task) {
task.setFailed('Task canceled by user')
return this.removeTask(task, true)
}
@ -36,6 +39,7 @@ class AbMergeManager {
libraryItemPath: libraryItem.path,
userId: user.id,
originalTrackPaths: libraryItem.media.tracks.map((t) => t.metadata.path),
inos: libraryItem.media.includedAudioFiles.map((f) => f.ino),
tempFilepath,
targetFilename,
targetFilepath: Path.join(libraryItem.path, targetFilename),
@ -43,7 +47,8 @@ class AbMergeManager {
ffmetadataObject: ffmpegHelpers.getFFMetadataObject(libraryItem, 1),
chapters: libraryItem.media.chapters?.map((c) => ({ ...c })),
coverPath: libraryItem.media.coverPath,
ffmetadataPath
ffmetadataPath,
duration: libraryItem.media.duration
}
const taskDescription = `Encoding audiobook "${libraryItem.media.metadata.title}" into a single m4b file.`
task.setData('encode-m4b', 'Encoding M4b', taskDescription, false, taskData)
@ -58,119 +63,78 @@ class AbMergeManager {
}
async runAudiobookMerge(libraryItem, task, encodingOptions) {
// Make sure the target directory is writable
if (!(await fileUtils.isWritable(libraryItem.path))) {
Logger.error(`[AbMergeManager] Target directory is not writable: ${libraryItem.path}`)
task.setFailed('Target directory is not writable')
this.removeTask(task, true)
return
}
// Create ffmetadata file
const success = await ffmpegHelpers.writeFFMetadataFile(task.data.metadataObject, task.data.chapters, task.data.ffmetadataPath)
if (!success) {
if (!(await ffmpegHelpers.writeFFMetadataFile(task.data.ffmetadataObject, task.data.chapters, task.data.ffmetadataPath))) {
Logger.error(`[AudioMetadataManager] Failed to write ffmetadata file for audiobook "${task.data.libraryItemId}"`)
task.setFailed('Failed to write metadata file.')
this.removeTask(task, true)
return
}
const audioBitrate = encodingOptions.bitrate || '128k'
const audioCodec = encodingOptions.codec || 'aac'
const audioChannels = encodingOptions.channels || 2
// If changing audio file type then encoding is needed
const audioTracks = libraryItem.media.tracks
// TODO: Updated in 2.2.11 to always encode even if merging multiple m4b. This is because just using the file extension as was being done before is not enough. This can be an option or do more to check if a concat is possible.
// const audioRequiresEncode = audioTracks[0].metadata.ext !== '.m4b'
const audioRequiresEncode = true
const firstTrackIsM4b = audioTracks[0].metadata.ext.toLowerCase() === '.m4b'
const isOneTrack = audioTracks.length === 1
const ffmpegInputs = []
if (!isOneTrack) {
const concatFilePath = Path.join(task.data.itemCachePath, 'files.txt')
await writeConcatFile(audioTracks, concatFilePath)
ffmpegInputs.push({
input: concatFilePath,
options: ['-safe 0', '-f concat']
})
} else {
ffmpegInputs.push({
input: audioTracks[0].metadata.path,
options: firstTrackIsM4b ? ['-f mp4'] : []
})
}
const logLevel = process.env.NODE_ENV === 'production' ? 'error' : 'warning'
let ffmpegOptions = [`-loglevel ${logLevel}`]
const ffmpegOutputOptions = ['-f mp4']
if (audioRequiresEncode) {
ffmpegOptions = ffmpegOptions.concat(['-map 0:a', `-acodec ${audioCodec}`, `-ac ${audioChannels}`, `-b:a ${audioBitrate}`])
} else {
ffmpegOptions.push('-max_muxing_queue_size 1000')
if (isOneTrack && firstTrackIsM4b) {
ffmpegOptions.push('-c copy')
} else {
ffmpegOptions.push('-c:a copy')
}
}
const workerData = {
inputs: ffmpegInputs,
options: ffmpegOptions,
outputOptions: ffmpegOutputOptions,
output: task.data.tempFilepath
}
let worker = null
try {
const workerPath = Path.join(global.appRoot, 'server/utils/downloadWorker.js')
worker = new workerThreads.Worker(workerPath, { workerData })
} catch (error) {
Logger.error(`[AbMergeManager] Start worker thread failed`, error)
task.setFailed('Failed to start worker thread')
this.removeTask(task, true)
return
}
worker.on('message', (message) => {
if (message != null && typeof message === 'object') {
if (message.type === 'RESULT') {
this.sendResult(task, message)
} else if (message.type === 'FFMPEG') {
if (Logger[message.level]) {
Logger[message.level](message.log)
}
}
}
})
this.pendingTasks.push({
id: task.id,
task,
worker
task
})
}
async sendResult(task, result) {
// Remove pending task
this.pendingTasks = this.pendingTasks.filter((d) => d.id !== task.id)
if (result.isKilled) {
task.setFailed('Ffmpeg task killed')
this.removeTask(task, true)
return
}
if (!result.success) {
task.setFailed('Encoding failed')
this.removeTask(task, true)
const encodeFraction = 0.95
const embedFraction = 1 - encodeFraction
try {
const trackProgressMonitor = new TrackProgressMonitor(
libraryItem.media.tracks.map((t) => t.duration),
(trackIndex) => SocketAuthority.adminEmitter('track_started', { libraryItemId: libraryItem.id, ino: task.data.inos[trackIndex] }),
(trackIndex, progressInTrack, taskProgress) => {
SocketAuthority.adminEmitter('track_progress', { libraryItemId: libraryItem.id, ino: task.data.inos[trackIndex], progress: progressInTrack })
SocketAuthority.adminEmitter('task_progress', { libraryItemId: libraryItem.id, progress: taskProgress * encodeFraction })
},
(trackIndex) => SocketAuthority.adminEmitter('track_finished', { libraryItemId: libraryItem.id, ino: task.data.inos[trackIndex] })
)
task.data.ffmpeg = new Ffmpeg()
await ffmpegHelpers.mergeAudioFiles(libraryItem.media.tracks, task.data.duration, task.data.itemCachePath, task.data.tempFilepath, encodingOptions, (progress) => trackProgressMonitor.update(progress), task.data.ffmpeg)
delete task.data.ffmpeg
trackProgressMonitor.finish()
} catch (error) {
if (error.message === 'FFMPEG_CANCELED') {
Logger.info(`[AbMergeManager] Task cancelled ${task.id}`)
} else {
Logger.error(`[AbMergeManager] mergeAudioFiles failed`, error)
task.setFailed('Failed to merge audio files')
this.removeTask(task, true)
}
return
}
// Write metadata to merged file
const success = await ffmpegHelpers.addCoverAndMetadataToFile(task.data.tempFilepath, task.data.coverPath, task.data.ffmetadataPath, 1, 'audio/mp4')
if (!success) {
Logger.error(`[AbMergeManager] Failed to write metadata to file "${task.data.tempFilepath}"`)
task.setFailed('Failed to write metadata to m4b file')
this.removeTask(task, true)
try {
task.data.ffmpeg = new Ffmpeg()
await ffmpegHelpers.addCoverAndMetadataToFile(
task.data.tempFilepath,
task.data.coverPath,
task.data.ffmetadataPath,
1,
'audio/mp4',
(progress) => {
Logger.debug(`[AbMergeManager] Embedding metadata progress: ${100 * encodeFraction + progress * embedFraction}`)
SocketAuthority.adminEmitter('task_progress', { libraryItemId: libraryItem.id, progress: 100 * encodeFraction + progress * embedFraction })
},
task.data.ffmpeg
)
delete task.data.ffmpeg
} catch (error) {
if (error.message === 'FFMPEG_CANCELED') {
Logger.info(`[AbMergeManager] Task cancelled ${task.id}`)
} else {
Logger.error(`[AbMergeManager] Failed to write metadata to file "${task.data.tempFilepath}"`)
task.setFailed('Failed to write metadata to m4b file')
this.removeTask(task, true)
}
return
}
@ -199,19 +163,14 @@ class AbMergeManager {
async removeTask(task, removeTempFilepath = false) {
Logger.info('[AbMergeManager] Removing task ' + task.id)
const pendingDl = this.pendingTasks.find((d) => d.id === task.id)
if (pendingDl) {
const pendingTask = this.pendingTasks.find((d) => d.id === task.id)
if (pendingTask) {
this.pendingTasks = this.pendingTasks.filter((d) => d.id !== task.id)
if (pendingDl.worker) {
Logger.warn(`[AbMergeManager] Removing download in progress - stopping worker`)
try {
pendingDl.worker.postMessage('STOP')
return
} catch (error) {
Logger.error('[AbMergeManager] Error posting stop message to worker', error)
}
} else {
Logger.debug(`[AbMergeManager] Removing download in progress - no worker`)
if (task.data.ffmpeg) {
Logger.warn(`[AbMergeManager] Killing ffmpeg process for task ${task.id}`)
task.data.ffmpeg.kill()
// wait for ffmpeg to exit, so that the output file is unlocked
await new Promise((resolve) => setTimeout(resolve, 500))
}
}

View File

@ -1,15 +1,11 @@
const Path = require('path')
const SocketAuthority = require('../SocketAuthority')
const Logger = require('../Logger')
const fs = require('../libs/fsExtra')
const ffmpegHelpers = require('../utils/ffmpegHelpers')
const TaskManager = require('./TaskManager')
const Task = require('../objects/Task')
const fileUtils = require('../utils/fileUtils')
class AudioMetadataMangaer {
constructor() {
@ -68,7 +64,8 @@ class AudioMetadataMangaer {
ino: af.ino,
filename: af.metadata.filename,
path: af.metadata.path,
cachePath: Path.join(itemCachePath, af.metadata.filename)
cachePath: Path.join(itemCachePath, af.metadata.filename),
duration: af.duration
})),
coverPath: libraryItem.media.coverPath,
metadataObject: ffmpegHelpers.getFFMetadataObject(libraryItem, audioFiles.length),
@ -78,7 +75,8 @@ class AudioMetadataMangaer {
options: {
forceEmbedChapters,
backupFiles
}
},
duration: libraryItem.media.duration
}
const taskDescription = `Embedding metadata in audiobook "${libraryItem.media.metadata.title}".`
task.setData('embed-metadata', 'Embedding Metadata', taskDescription, false, taskData)
@ -101,11 +99,40 @@ class AudioMetadataMangaer {
Logger.info(`[AudioMetadataManager] Starting metadata embed task`, task.description)
// Ensure target directory is writable
const targetDirWritable = await fileUtils.isWritable(task.data.libraryItemPath)
Logger.debug(`[AudioMetadataManager] Target directory ${task.data.libraryItemPath} writable: ${targetDirWritable}`)
if (!targetDirWritable) {
Logger.error(`[AudioMetadataManager] Target directory is not writable: ${task.data.libraryItemPath}`)
task.setFailed('Target directory is not writable')
this.handleTaskFinished(task)
return
}
// Ensure target audio files are writable
for (const af of task.data.audioFiles) {
try {
await fs.access(af.path, fs.constants.W_OK)
} catch (err) {
Logger.error(`[AudioMetadataManager] Audio file is not writable: ${af.path}`)
task.setFailed(`Audio file "${Path.basename(af.path)}" is not writable`)
this.handleTaskFinished(task)
return
}
}
// Ensure item cache dir exists
let cacheDirCreated = false
if (!(await fs.pathExists(task.data.itemCachePath))) {
await fs.mkdir(task.data.itemCachePath)
cacheDirCreated = true
try {
await fs.mkdir(task.data.itemCachePath)
cacheDirCreated = true
} catch (err) {
Logger.error(`[AudioMetadataManager] Failed to create cache directory ${task.data.itemCachePath}`, err)
task.setFailed('Failed to create cache directory')
this.handleTaskFinished(task)
return
}
}
// Create ffmetadata file
@ -119,8 +146,10 @@ class AudioMetadataMangaer {
}
// Tag audio files
let cummulativeProgress = 0
for (const af of task.data.audioFiles) {
SocketAuthority.adminEmitter('audiofile_metadata_started', {
const audioFileRelativeDuration = af.duration / task.data.duration
SocketAuthority.adminEmitter('track_started', {
libraryItemId: task.data.libraryItemId,
ino: af.ino
})
@ -133,18 +162,31 @@ class AudioMetadataMangaer {
Logger.debug(`[AudioMetadataManager] Backed up audio file at "${backupFilePath}"`)
} catch (err) {
Logger.error(`[AudioMetadataManager] Failed to backup audio file "${af.path}"`, err)
task.setFailed(`Failed to backup audio file "${Path.basename(af.path)}"`)
this.handleTaskFinished(task)
return
}
}
const success = await ffmpegHelpers.addCoverAndMetadataToFile(af.path, task.data.coverPath, ffmetadataPath, af.index, task.data.mimeType)
if (success) {
try {
await ffmpegHelpers.addCoverAndMetadataToFile(af.path, task.data.coverPath, ffmetadataPath, af.index, task.data.mimeType, (progress) => {
SocketAuthority.adminEmitter('task_progress', { libraryItemId: task.data.libraryItemId, progress: cummulativeProgress + progress * audioFileRelativeDuration })
SocketAuthority.adminEmitter('track_progress', { libraryItemId: task.data.libraryItemId, ino: af.ino, progress })
})
Logger.info(`[AudioMetadataManager] Successfully tagged audio file "${af.path}"`)
} catch (err) {
Logger.error(`[AudioMetadataManager] Failed to tag audio file "${af.path}"`, err)
task.setFailed(`Failed to tag audio file "${Path.basename(af.path)}"`)
this.handleTaskFinished(task)
return
}
SocketAuthority.adminEmitter('audiofile_metadata_finished', {
SocketAuthority.adminEmitter('track_finished', {
libraryItemId: task.data.libraryItemId,
ino: af.ino
})
cummulativeProgress += audioFileRelativeDuration * 100
}
// Remove temp cache file/folder if not backing up

View File

@ -0,0 +1,88 @@
class TrackProgressMonitor {
/**
* @callback TrackStartedCallback
* @param {number} trackIndex - The index of the track that started.
*/
/**
* @callback ProgressCallback
* @param {number} trackIndex - The index of the track that is being updated.
* @param {number} progressInTrack - The progress of the track in percent.
* @param {number} totalProgress - The total progress in percent.
*/
/**
* @callback TrackFinishedCallback
* @param {number} trackIndex - The index of the track that finished.
*/
/**
* Creates a new TrackProgressMonitor.
* @constructor
* @param {number[]} trackDurations - The durations of the tracks in seconds.
* @param {TrackStartedCallback} trackStartedCallback - The callback to call when a track starts.
* @param {ProgressCallback} progressCallback - The callback to call when progress is updated.
* @param {TrackFinishedCallback} trackFinishedCallback - The callback to call when a track finishes.
*/
constructor(trackDurations, trackStartedCallback, progressCallback, trackFinishedCallback) {
this.trackDurations = trackDurations
this.totalDuration = trackDurations.reduce((total, duration) => total + duration, 0)
this.trackStartedCallback = trackStartedCallback
this.progressCallback = progressCallback
this.trackFinishedCallback = trackFinishedCallback
this.currentTrackIndex = -1
this.cummulativeProgress = 0
this.currentTrackPercentage = 0
this.numTracks = this.trackDurations.length
this.allTracksFinished = false
this.#moveToNextTrack()
}
#outsideCurrentTrack(progress) {
this.currentTrackProgress = progress - this.cummulativeProgress
return this.currentTrackProgress >= this.currentTrackPercentage
}
#moveToNextTrack() {
if (this.currentTrackIndex >= 0) this.#trackFinished()
this.currentTrackIndex++
this.cummulativeProgress += this.currentTrackPercentage
if (this.currentTrackIndex >= this.numTracks) {
this.allTracksFinished = true
return
}
this.currentTrackPercentage = (this.trackDurations[this.currentTrackIndex] / this.totalDuration) * 100
this.#trackStarted()
}
#trackStarted() {
this.trackStartedCallback(this.currentTrackIndex)
}
#progressUpdated(progress) {
const progressInTrack = (this.currentTrackProgress / this.currentTrackPercentage) * 100
this.progressCallback(this.currentTrackIndex, progressInTrack, progress)
}
#trackFinished() {
this.trackFinishedCallback(this.currentTrackIndex)
}
/**
* Updates the progress of the track.
* @param {number} progress - The progress of the track in percent.
*/
update(progress) {
while (this.#outsideCurrentTrack(progress) && !this.allTracksFinished) this.#moveToNextTrack()
if (!this.allTracksFinished) this.#progressUpdated(progress)
}
/**
* Finish the track progress monitoring.
* Forces all remaining tracks to finish.
*/
finish() {
this.update(101)
}
}
module.exports = TrackProgressMonitor

View File

@ -1,7 +1,7 @@
const axios = require('axios')
const Ffmpeg = require('../libs/fluentFfmpeg')
const ffmpgegUtils = require('../libs/fluentFfmpeg/utils')
const fs = require('../libs/fsExtra')
const os = require('os')
const Path = require('path')
const Logger = require('../Logger')
const { filePathToPOSIX } = require('./fileUtils')
@ -251,9 +251,10 @@ module.exports.writeFFMetadataFile = writeFFMetadataFile
* @param {number} track - The track number to embed in the audio file.
* @param {string} mimeType - The MIME type of the audio file.
* @param {Ffmpeg} ffmpeg - The Ffmpeg instance to use (optional). Used for dependency injection in tests.
* @returns {Promise<boolean>} A promise that resolves to true if the operation is successful, false otherwise.
* @param {function(number): void|null} progressCB - A callback function to report progress.
* @returns {Promise<void>} A promise that resolves if the operation is successful, rejects otherwise.
*/
async function addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType, ffmpeg = Ffmpeg()) {
async function addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType, progressCB = null, ffmpeg = Ffmpeg()) {
const isMp4 = mimeType === 'audio/mp4'
const isMp3 = mimeType === 'audio/mpeg'
@ -262,7 +263,7 @@ async function addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataF
const audioFileBaseName = Path.basename(audioFilePath, audioFileExt)
const tempFilePath = filePathToPOSIX(Path.join(audioFileDir, `${audioFileBaseName}.tmp${audioFileExt}`))
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
ffmpeg.input(audioFilePath).input(metadataFilePath).outputOptions([
'-map 0:a', // map audio stream from input file
'-map_metadata 1', // map metadata tags from metadata file first
@ -302,21 +303,36 @@ async function addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataF
ffmpeg
.output(tempFilePath)
.on('start', function (commandLine) {
.on('start', (commandLine) => {
Logger.debug('[ffmpegHelpers] Spawned Ffmpeg with command: ' + commandLine)
})
.on('end', (stdout, stderr) => {
.on('progress', (progress) => {
if (!progressCB || !progress.percent) return
Logger.debug(`[ffmpegHelpers] Progress: ${progress.percent}%`)
progressCB(progress.percent)
})
.on('end', async (stdout, stderr) => {
Logger.debug('[ffmpegHelpers] ffmpeg stdout:', stdout)
Logger.debug('[ffmpegHelpers] ffmpeg stderr:', stderr)
fs.copyFileSync(tempFilePath, audioFilePath)
fs.unlinkSync(tempFilePath)
resolve(true)
Logger.debug('[ffmpegHelpers] Moving temp file to audio file path:', `"${tempFilePath}"`, '->', `"${audioFilePath}"`)
try {
await fs.move(tempFilePath, audioFilePath, { overwrite: true })
resolve()
} catch (error) {
Logger.error(`[ffmpegHelpers] Failed to move temp file to audio file path: "${tempFilePath}" -> "${audioFilePath}"`, error)
reject(error)
}
})
.on('error', (err, stdout, stderr) => {
Logger.error('Error adding cover image and metadata:', err)
Logger.error('ffmpeg stdout:', stdout)
Logger.error('ffmpeg stderr:', stderr)
resolve(false)
if (err.message && err.message.includes('SIGKILL')) {
Logger.info(`[ffmpegHelpers] addCoverAndMetadataToFile Killed by User`)
reject(new Error('FFMPEG_CANCELED'))
} else {
Logger.error('Error adding cover image and metadata:', err)
Logger.error('ffmpeg stdout:', stdout)
Logger.error('ffmpeg stderr:', stderr)
reject(err)
}
})
ffmpeg.run()
@ -366,3 +382,92 @@ function getFFMetadataObject(libraryItem, audioFilesLength) {
}
module.exports.getFFMetadataObject = getFFMetadataObject
/**
* Merges audio files into a single output file using FFmpeg.
*
* @param {Array} audioTracks - The audio tracks to merge.
* @param {number} duration - The total duration of the audio tracks.
* @param {string} itemCachePath - The path to the item cache.
* @param {string} outputFilePath - The path to the output file.
* @param {Object} encodingOptions - The options for encoding the audio.
* @param {Function} [progressCB=null] - The callback function to track the progress of the merge.
* @param {Object} [ffmpeg=Ffmpeg()] - The FFmpeg instance to use for merging.
* @returns {Promise<void>} A promise that resolves when the audio files are merged successfully.
*/
async function mergeAudioFiles(audioTracks, duration, itemCachePath, outputFilePath, encodingOptions, progressCB = null, ffmpeg = Ffmpeg()) {
const audioBitrate = encodingOptions.bitrate || '128k'
const audioCodec = encodingOptions.codec || 'aac'
const audioChannels = encodingOptions.channels || 2
// TODO: Updated in 2.2.11 to always encode even if merging multiple m4b. This is because just using the file extension as was being done before is not enough. This can be an option or do more to check if a concat is possible.
// const audioRequiresEncode = audioTracks[0].metadata.ext !== '.m4b'
const audioRequiresEncode = true
const firstTrackIsM4b = audioTracks[0].metadata.ext.toLowerCase() === '.m4b'
const isOneTrack = audioTracks.length === 1
let concatFilePath = null
if (!isOneTrack) {
concatFilePath = Path.join(itemCachePath, 'files.txt')
if ((await writeConcatFile(audioTracks, concatFilePath)) == null) {
throw new Error('Failed to write concat file')
}
ffmpeg.input(concatFilePath).inputOptions(['-safe 0', '-f concat'])
} else {
ffmpeg.input(audioTracks[0].metadata.path).inputOptions(firstTrackIsM4b ? ['-f mp4'] : [])
}
//const logLevel = process.env.NODE_ENV === 'production' ? 'error' : 'warning'
ffmpeg.outputOptions(['-f mp4'])
if (audioRequiresEncode) {
ffmpeg.outputOptions(['-map 0:a', `-acodec ${audioCodec}`, `-ac ${audioChannels}`, `-b:a ${audioBitrate}`])
} else {
ffmpeg.outputOptions(['-max_muxing_queue_size 1000'])
if (isOneTrack && firstTrackIsM4b) {
ffmpeg.outputOptions(['-c copy'])
} else {
ffmpeg.outputOptions(['-c:a copy'])
}
}
ffmpeg.output(outputFilePath)
return new Promise((resolve, reject) => {
ffmpeg
.on('start', (cmd) => {
Logger.debug(`[ffmpegHelpers] Merge Audio Files ffmpeg command: ${cmd}`)
})
.on('progress', (progress) => {
if (!progressCB || !progress.timemark || !duration) return
// Cannot rely on progress.percent as it is not accurate for concat
const percent = (ffmpgegUtils.timemarkToSeconds(progress.timemark) / duration) * 100
progressCB(percent)
})
.on('end', async (stdout, stderr) => {
if (concatFilePath) await fs.remove(concatFilePath)
Logger.debug('[ffmpegHelpers] ffmpeg stdout:', stdout)
Logger.debug('[ffmpegHelpers] ffmpeg stderr:', stderr)
Logger.debug(`[ffmpegHelpers] Audio Files Merged Successfully`)
resolve()
})
.on('error', async (err, stdout, stderr) => {
if (concatFilePath) await fs.remove(concatFilePath)
if (err.message && err.message.includes('SIGKILL')) {
Logger.info(`[ffmpegHelpers] Merge Audio Files Killed by User`)
reject(new Error('FFMPEG_CANCELED'))
} else {
Logger.error(`[ffmpegHelpers] Merge Audio Files Error ${err}`)
Logger.error('ffmpeg stdout:', stdout)
Logger.error('ffmpeg stderr:', stderr)
reject(err)
}
})
ffmpeg.run()
})
}
module.exports.mergeAudioFiles = mergeAudioFiles

View File

@ -0,0 +1,95 @@
const chai = require('chai')
const sinon = require('sinon')
const TrackProgressMonitor = require('../../../server/objects/TrackProgressMonitor')
const expect = chai.expect
describe('TrackProgressMonitor', () => {
let trackDurations
let trackStartedCallback
let progressCallback
let trackFinishedCallback
let monitor
beforeEach(() => {
trackDurations = [10, 40, 50]
trackStartedCallback = sinon.spy()
progressCallback = sinon.spy()
trackFinishedCallback = sinon.spy()
})
it('should initialize correctly', () => {
monitor = new TrackProgressMonitor(trackDurations, trackStartedCallback, progressCallback, trackFinishedCallback)
expect(monitor.trackDurations).to.deep.equal(trackDurations)
expect(monitor.totalDuration).to.equal(100)
expect(monitor.trackStartedCallback).to.equal(trackStartedCallback)
expect(monitor.progressCallback).to.equal(progressCallback)
expect(monitor.trackFinishedCallback).to.equal(trackFinishedCallback)
expect(monitor.currentTrackIndex).to.equal(0)
expect(monitor.cummulativeProgress).to.equal(0)
expect(monitor.currentTrackPercentage).to.equal(10)
expect(monitor.numTracks).to.equal(trackDurations.length)
expect(monitor.allTracksFinished).to.be.false
})
it('should update the progress', () => {
monitor = new TrackProgressMonitor(trackDurations, trackStartedCallback, progressCallback, trackFinishedCallback)
monitor.update(5)
expect(monitor.currentTrackIndex).to.equal(0)
expect(monitor.cummulativeProgress).to.equal(0)
expect(monitor.currentTrackPercentage).to.equal(10)
expect(trackStartedCallback.calledOnceWithExactly(0)).to.be.true
expect(progressCallback.calledOnceWithExactly(0, 50, 5)).to.be.true
expect(trackFinishedCallback.notCalled).to.be.true
})
it('should update the progress multiple times on the same track', () => {
monitor = new TrackProgressMonitor(trackDurations, trackStartedCallback, progressCallback, trackFinishedCallback)
monitor.update(5)
monitor.update(7)
expect(monitor.currentTrackIndex).to.equal(0)
expect(monitor.cummulativeProgress).to.equal(0)
expect(monitor.currentTrackPercentage).to.equal(10)
expect(trackStartedCallback.calledOnceWithExactly(0)).to.be.true
expect(progressCallback.calledTwice).to.be.true
expect(progressCallback.calledWithExactly(0, 50, 5)).to.be.true
expect(progressCallback.calledWithExactly(0, 70, 7)).to.be.true
expect(trackFinishedCallback.notCalled).to.be.true
})
it('should update the progress multiple times on different tracks', () => {
monitor = new TrackProgressMonitor(trackDurations, trackStartedCallback, progressCallback, trackFinishedCallback)
monitor.update(5)
monitor.update(20)
expect(monitor.currentTrackIndex).to.equal(1)
expect(monitor.cummulativeProgress).to.equal(10)
expect(monitor.currentTrackPercentage).to.equal(40)
expect(trackStartedCallback.calledTwice).to.be.true
expect(trackStartedCallback.calledWithExactly(0)).to.be.true
expect(trackStartedCallback.calledWithExactly(1)).to.be.true
expect(progressCallback.calledTwice).to.be.true
expect(progressCallback.calledWithExactly(0, 50, 5)).to.be.true
expect(progressCallback.calledWithExactly(1, 25, 20)).to.be.true
expect(trackFinishedCallback.calledOnceWithExactly(0)).to.be.true
})
it('should finish all tracks', () => {
monitor = new TrackProgressMonitor(trackDurations, trackStartedCallback, progressCallback, trackFinishedCallback)
monitor.finish()
expect(monitor.allTracksFinished).to.be.true
expect(trackStartedCallback.calledThrice).to.be.true
expect(trackFinishedCallback.calledThrice).to.be.true
expect(progressCallback.notCalled).to.be.true
expect(trackStartedCallback.calledWithExactly(0)).to.be.true
expect(trackFinishedCallback.calledWithExactly(0)).to.be.true
expect(trackStartedCallback.calledWithExactly(1)).to.be.true
expect(trackFinishedCallback.calledWithExactly(1)).to.be.true
expect(trackStartedCallback.calledWithExactly(2)).to.be.true
expect(trackFinishedCallback.calledWithExactly(2)).to.be.true
})
})

View File

@ -81,10 +81,9 @@ describe('addCoverAndMetadataToFile', () => {
ffmpegStub.run = sinon.stub().callsFake(() => {
ffmpegStub.emit('end')
})
const fsCopyFileSyncStub = sinon.stub(fs, 'copyFileSync')
const fsUnlinkSyncStub = sinon.stub(fs, 'unlinkSync')
const fsMove = sinon.stub(fs, 'move').resolves()
return { audioFilePath, coverFilePath, metadataFilePath, track, mimeType, ffmpegStub, fsCopyFileSyncStub, fsUnlinkSyncStub }
return { audioFilePath, coverFilePath, metadataFilePath, track, mimeType, ffmpegStub, fsMove }
}
let audioFilePath = null
@ -93,8 +92,7 @@ describe('addCoverAndMetadataToFile', () => {
let track = null
let mimeType = null
let ffmpegStub = null
let fsCopyFileSyncStub = null
let fsUnlinkSyncStub = null
let fsMove = null
beforeEach(() => {
const input = createTestSetup()
audioFilePath = input.audioFilePath
@ -103,16 +101,14 @@ describe('addCoverAndMetadataToFile', () => {
track = input.track
mimeType = input.mimeType
ffmpegStub = input.ffmpegStub
fsCopyFileSyncStub = input.fsCopyFileSyncStub
fsUnlinkSyncStub = input.fsUnlinkSyncStub
fsMove = input.fsMove
})
it('should add cover image and metadata to audio file', async () => {
// Act
const result = await addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType, ffmpegStub)
await addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType, null, ffmpegStub)
// Assert
expect(result).to.be.true
expect(ffmpegStub.input.calledThrice).to.be.true
expect(ffmpegStub.input.getCall(0).args[0]).to.equal(audioFilePath)
expect(ffmpegStub.input.getCall(1).args[0]).to.equal(metadataFilePath)
@ -129,12 +125,10 @@ describe('addCoverAndMetadataToFile', () => {
expect(ffmpegStub.run.calledOnce).to.be.true
expect(fsCopyFileSyncStub.calledOnce).to.be.true
expect(fsCopyFileSyncStub.firstCall.args[0]).to.equal('/path/to/audio/file.tmp.mp3')
expect(fsCopyFileSyncStub.firstCall.args[1]).to.equal('/path/to/audio/file.mp3')
expect(fsUnlinkSyncStub.calledOnce).to.be.true
expect(fsUnlinkSyncStub.firstCall.args[0]).to.equal('/path/to/audio/file.tmp.mp3')
expect(fsMove.calledOnce).to.be.true
expect(fsMove.firstCall.args[0]).to.equal('/path/to/audio/file.tmp.mp3')
expect(fsMove.firstCall.args[1]).to.equal('/path/to/audio/file.mp3')
expect(fsMove.firstCall.args[2]).to.deep.equal({ overwrite: true })
// Restore the stub
sinon.restore()
@ -145,10 +139,9 @@ describe('addCoverAndMetadataToFile', () => {
coverFilePath = null
// Act
const result = await addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType, ffmpegStub)
await addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType, null, ffmpegStub)
// Assert
expect(result).to.be.true
expect(ffmpegStub.input.calledTwice).to.be.true
expect(ffmpegStub.input.getCall(0).args[0]).to.equal(audioFilePath)
expect(ffmpegStub.input.getCall(1).args[0]).to.equal(metadataFilePath)
@ -164,12 +157,10 @@ describe('addCoverAndMetadataToFile', () => {
expect(ffmpegStub.run.calledOnce).to.be.true
expect(fsCopyFileSyncStub.calledOnce).to.be.true
expect(fsCopyFileSyncStub.firstCall.args[0]).to.equal('/path/to/audio/file.tmp.mp3')
expect(fsCopyFileSyncStub.firstCall.args[1]).to.equal('/path/to/audio/file.mp3')
expect(fsUnlinkSyncStub.calledOnce).to.be.true
expect(fsUnlinkSyncStub.firstCall.args[0]).to.equal('/path/to/audio/file.tmp.mp3')
expect(fsMove.calledOnce).to.be.true
expect(fsMove.firstCall.args[0]).to.equal('/path/to/audio/file.tmp.mp3')
expect(fsMove.firstCall.args[1]).to.equal('/path/to/audio/file.mp3')
expect(fsMove.firstCall.args[2]).to.deep.equal({ overwrite: true })
// Restore the stub
sinon.restore()
@ -182,10 +173,15 @@ describe('addCoverAndMetadataToFile', () => {
})
// Act
const result = await addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType, ffmpegStub)
try {
await addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType, null, ffmpegStub)
expect.fail('Expected an error to be thrown')
} catch (error) {
// Assert
expect(error.message).to.equal('FFmpeg error')
}
// Assert
expect(result).to.be.false
expect(ffmpegStub.input.calledThrice).to.be.true
expect(ffmpegStub.input.getCall(0).args[0]).to.equal(audioFilePath)
expect(ffmpegStub.input.getCall(1).args[0]).to.equal(metadataFilePath)
@ -202,9 +198,7 @@ describe('addCoverAndMetadataToFile', () => {
expect(ffmpegStub.run.calledOnce).to.be.true
expect(fsCopyFileSyncStub.called).to.be.false
expect(fsUnlinkSyncStub.called).to.be.false
expect(fsMove.called).to.be.false
// Restore the stub
sinon.restore()
@ -216,10 +210,9 @@ describe('addCoverAndMetadataToFile', () => {
audioFilePath = '/path/to/audio/file.m4b'
// Act
const result = await addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType, ffmpegStub)
await addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType, null, ffmpegStub)
// Assert
expect(result).to.be.true
expect(ffmpegStub.input.calledThrice).to.be.true
expect(ffmpegStub.input.getCall(0).args[0]).to.equal(audioFilePath)
expect(ffmpegStub.input.getCall(1).args[0]).to.equal(metadataFilePath)
@ -236,12 +229,10 @@ describe('addCoverAndMetadataToFile', () => {
expect(ffmpegStub.run.calledOnce).to.be.true
expect(fsCopyFileSyncStub.calledOnce).to.be.true
expect(fsCopyFileSyncStub.firstCall.args[0]).to.equal('/path/to/audio/file.tmp.m4b')
expect(fsCopyFileSyncStub.firstCall.args[1]).to.equal('/path/to/audio/file.m4b')
expect(fsUnlinkSyncStub.calledOnce).to.be.true
expect(fsUnlinkSyncStub.firstCall.args[0]).to.equal('/path/to/audio/file.tmp.m4b')
expect(fsMove.calledOnce).to.be.true
expect(fsMove.firstCall.args[0]).to.equal('/path/to/audio/file.tmp.m4b')
expect(fsMove.firstCall.args[1]).to.equal('/path/to/audio/file.m4b')
expect(fsMove.firstCall.args[2]).to.deep.equal({ overwrite: true })
// Restore the stub
sinon.restore()