mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-08-01 08:44:40 +02:00
Fixes + add progress to m4b and embed tools
This commit is contained in:
parent
b6875a44cf
commit
10f5f331d7
11 changed files with 530 additions and 211 deletions
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
88
server/objects/TrackProgressMonitor.js
Normal file
88
server/objects/TrackProgressMonitor.js
Normal 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
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue