2022-05-02 01:33:46 +02:00
|
|
|
const Path = require('path')
|
|
|
|
const workerThreads = require('worker_threads')
|
2022-11-24 22:53:58 +01:00
|
|
|
|
|
|
|
const SocketAuthority = require('../SocketAuthority')
|
2022-05-02 01:33:46 +02:00
|
|
|
const Logger = require('../Logger')
|
2022-11-24 22:53:58 +01:00
|
|
|
|
|
|
|
const fs = require('../libs/fsExtra')
|
|
|
|
|
2022-05-02 01:33:46 +02:00
|
|
|
const filePerms = require('../utils/filePerms')
|
|
|
|
const { secondsToTimestamp } = require('../utils/index')
|
|
|
|
const { writeMetadataFile } = require('../utils/ffmpegHelpers')
|
2022-09-25 22:56:06 +02:00
|
|
|
const toneHelpers = require('../utils/toneHelpers')
|
2022-05-02 01:33:46 +02:00
|
|
|
|
|
|
|
class AudioMetadataMangaer {
|
2022-11-24 22:53:58 +01:00
|
|
|
constructor(db, taskManager) {
|
2022-05-02 01:33:46 +02:00
|
|
|
this.db = db
|
2022-10-02 21:16:17 +02:00
|
|
|
this.taskManager = taskManager
|
2022-05-02 01:33:46 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 19:13:52 +01:00
|
|
|
updateMetadataForItem(user, libraryItem, useTone, forceEmbedChapters) {
|
2022-09-25 22:56:06 +02:00
|
|
|
if (useTone) {
|
2022-11-05 19:13:52 +01:00
|
|
|
this.updateMetadataForItemWithTone(user, libraryItem, forceEmbedChapters)
|
2022-09-25 22:56:06 +02:00
|
|
|
} else {
|
|
|
|
this.updateMetadataForItemWithFfmpeg(user, libraryItem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// TONE
|
|
|
|
//
|
|
|
|
getToneMetadataObjectForApi(libraryItem) {
|
|
|
|
return toneHelpers.getToneMetadataObject(libraryItem)
|
|
|
|
}
|
|
|
|
|
2022-11-05 19:13:52 +01:00
|
|
|
async updateMetadataForItemWithTone(user, libraryItem, forceEmbedChapters) {
|
2022-09-25 22:56:06 +02:00
|
|
|
var audioFiles = libraryItem.media.includedAudioFiles
|
|
|
|
|
|
|
|
const itemAudioMetadataPayload = {
|
|
|
|
userId: user.id,
|
|
|
|
libraryItemId: libraryItem.id,
|
|
|
|
startedAt: Date.now(),
|
|
|
|
audioFiles: audioFiles.map(af => ({ index: af.index, ino: af.ino, filename: af.metadata.filename }))
|
|
|
|
}
|
|
|
|
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('audio_metadata_started', itemAudioMetadataPayload)
|
2022-09-25 22:56:06 +02:00
|
|
|
|
|
|
|
// Write chapters file
|
2022-11-03 02:40:50 +01:00
|
|
|
var toneJsonPath = null
|
2022-09-27 01:07:31 +02:00
|
|
|
const itemCacheDir = Path.join(global.MetadataPath, `cache/items/${libraryItem.id}`)
|
2022-09-25 22:56:06 +02:00
|
|
|
await fs.ensureDir(itemCacheDir)
|
|
|
|
|
2022-11-03 02:40:50 +01:00
|
|
|
try {
|
|
|
|
toneJsonPath = Path.join(itemCacheDir, 'metadata.json')
|
2022-11-05 19:13:52 +01:00
|
|
|
const chapters = (audioFiles.length == 1 || forceEmbedChapters) ? libraryItem.media.chapters : null
|
|
|
|
await toneHelpers.writeToneMetadataJsonFile(libraryItem, chapters, toneJsonPath, audioFiles.length)
|
2022-11-03 02:40:50 +01:00
|
|
|
} catch (error) {
|
|
|
|
Logger.error(`[AudioMetadataManager] Write metadata.json failed`, error)
|
|
|
|
toneJsonPath = null
|
2022-09-25 22:56:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const results = []
|
|
|
|
for (const af of audioFiles) {
|
2022-11-03 16:32:50 +01:00
|
|
|
const result = await this.updateAudioFileMetadataWithTone(libraryItem.id, af, toneJsonPath, itemCacheDir)
|
2022-09-25 22:56:06 +02:00
|
|
|
results.push(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
const elapsed = Date.now() - itemAudioMetadataPayload.startedAt
|
|
|
|
Logger.debug(`[AudioMetadataManager] Elapsed ${secondsToTimestamp(elapsed)}`)
|
|
|
|
itemAudioMetadataPayload.results = results
|
|
|
|
itemAudioMetadataPayload.elapsed = elapsed
|
|
|
|
itemAudioMetadataPayload.finishedAt = Date.now()
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('audio_metadata_finished', itemAudioMetadataPayload)
|
2022-09-25 22:56:06 +02:00
|
|
|
}
|
|
|
|
|
2022-11-03 02:40:50 +01:00
|
|
|
async updateAudioFileMetadataWithTone(libraryItemId, audioFile, toneJsonPath, itemCacheDir) {
|
2022-09-25 22:56:06 +02:00
|
|
|
const resultPayload = {
|
|
|
|
libraryItemId,
|
|
|
|
index: audioFile.index,
|
|
|
|
ino: audioFile.ino,
|
|
|
|
filename: audioFile.metadata.filename
|
|
|
|
}
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('audiofile_metadata_started', resultPayload)
|
2022-09-25 22:56:06 +02:00
|
|
|
|
|
|
|
// Backup audio file
|
|
|
|
try {
|
|
|
|
const backupFilePath = Path.join(itemCacheDir, audioFile.metadata.filename)
|
|
|
|
await fs.copy(audioFile.metadata.path, backupFilePath)
|
|
|
|
Logger.debug(`[AudioMetadataManager] Backed up audio file at "${backupFilePath}"`)
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`[AudioMetadataManager] Failed to backup audio file "${audioFile.metadata.path}"`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const _toneMetadataObject = {
|
2022-11-03 02:40:50 +01:00
|
|
|
'ToneJsonFile': toneJsonPath,
|
|
|
|
'TrackNumber': audioFile.index,
|
2022-09-25 22:56:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resultPayload.success = await toneHelpers.tagAudioFile(audioFile.metadata.path, _toneMetadataObject)
|
|
|
|
if (resultPayload.success) {
|
|
|
|
Logger.info(`[AudioMetadataManager] Successfully tagged audio file "${audioFile.metadata.path}"`)
|
|
|
|
}
|
|
|
|
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('audiofile_metadata_finished', resultPayload)
|
2022-09-25 22:56:06 +02:00
|
|
|
return resultPayload
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// FFMPEG
|
|
|
|
//
|
|
|
|
async updateMetadataForItemWithFfmpeg(user, libraryItem) {
|
2022-05-02 01:33:46 +02:00
|
|
|
var audioFiles = libraryItem.media.audioFiles
|
|
|
|
|
|
|
|
const itemAudioMetadataPayload = {
|
|
|
|
userId: user.id,
|
|
|
|
libraryItemId: libraryItem.id,
|
|
|
|
startedAt: Date.now(),
|
|
|
|
audioFiles: audioFiles.map(af => ({ index: af.index, ino: af.ino, filename: af.metadata.filename }))
|
|
|
|
}
|
|
|
|
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('audio_metadata_started', itemAudioMetadataPayload)
|
2022-05-02 01:33:46 +02:00
|
|
|
|
|
|
|
var downloadsPath = Path.join(global.MetadataPath, 'downloads')
|
|
|
|
var outputDir = Path.join(downloadsPath, libraryItem.id)
|
|
|
|
await fs.ensureDir(outputDir)
|
|
|
|
|
|
|
|
var metadataFilePath = Path.join(outputDir, 'metadata.txt')
|
|
|
|
await writeMetadataFile(libraryItem, metadataFilePath)
|
|
|
|
|
2022-06-05 19:36:42 +02:00
|
|
|
if (libraryItem.media.coverPath != null) {
|
|
|
|
var coverPath = libraryItem.media.coverPath.replace(/\\/g, '/')
|
|
|
|
}
|
2022-06-05 03:56:55 +02:00
|
|
|
|
2022-05-02 01:33:46 +02:00
|
|
|
const proms = audioFiles.map(af => {
|
2022-09-25 22:56:06 +02:00
|
|
|
return this.updateAudioFileMetadataWithFfmpeg(libraryItem.id, af, outputDir, metadataFilePath, coverPath)
|
2022-05-02 01:33:46 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
const results = await Promise.all(proms)
|
|
|
|
|
2022-05-03 01:48:00 +02:00
|
|
|
Logger.debug(`[AudioMetadataManager] Finished`)
|
2022-05-02 01:33:46 +02:00
|
|
|
|
|
|
|
await fs.remove(outputDir)
|
|
|
|
|
|
|
|
const elapsed = Date.now() - itemAudioMetadataPayload.startedAt
|
|
|
|
Logger.debug(`[AudioMetadataManager] Elapsed ${secondsToTimestamp(elapsed)}`)
|
|
|
|
itemAudioMetadataPayload.results = results
|
|
|
|
itemAudioMetadataPayload.elapsed = elapsed
|
|
|
|
itemAudioMetadataPayload.finishedAt = Date.now()
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('audio_metadata_finished', itemAudioMetadataPayload)
|
2022-05-02 01:33:46 +02:00
|
|
|
}
|
|
|
|
|
2022-09-25 22:56:06 +02:00
|
|
|
updateAudioFileMetadataWithFfmpeg(libraryItemId, audioFile, outputDir, metadataFilePath, coverPath = '') {
|
2022-05-02 01:33:46 +02:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
const resultPayload = {
|
|
|
|
libraryItemId,
|
|
|
|
index: audioFile.index,
|
|
|
|
ino: audioFile.ino,
|
|
|
|
filename: audioFile.metadata.filename
|
|
|
|
}
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('audiofile_metadata_started', resultPayload)
|
2022-05-02 01:33:46 +02:00
|
|
|
|
|
|
|
Logger.debug(`[AudioFileMetadataManager] Starting audio file metadata encode for "${audioFile.metadata.filename}"`)
|
|
|
|
|
|
|
|
var outputPath = Path.join(outputDir, audioFile.metadata.filename)
|
|
|
|
var inputPath = audioFile.metadata.path
|
|
|
|
const isM4b = audioFile.metadata.format === 'm4b'
|
|
|
|
const ffmpegInputs = [
|
|
|
|
{
|
|
|
|
input: inputPath,
|
|
|
|
options: isM4b ? ['-f mp4'] : []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: metadataFilePath
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
/*
|
|
|
|
Mp4 doesnt support writing custom tags by default. Supported tags are itunes tags: https://git.videolan.org/?p=ffmpeg.git;a=blob;f=libavformat/movenc.c;h=b6821d447c92183101086cb67099b2f4804293de;hb=HEAD#l2905
|
|
|
|
|
|
|
|
Workaround -movflags use_metadata_tags found here: https://superuser.com/a/1208277
|
|
|
|
|
|
|
|
Ffmpeg premapped id3 tags: https://wiki.multimedia.cx/index.php/FFmpeg_Metadata
|
|
|
|
*/
|
|
|
|
|
2022-06-05 19:36:42 +02:00
|
|
|
const ffmpegOptions = ['-c copy', '-map_chapters 1', '-map_metadata 1', `-metadata track=${audioFile.index}`, '-write_id3v2 1', '-movflags use_metadata_tags']
|
|
|
|
|
|
|
|
if (coverPath != '') {
|
|
|
|
var ffmpegCoverPathInput = {
|
|
|
|
input: coverPath,
|
|
|
|
options: ['-f image2pipe']
|
|
|
|
}
|
|
|
|
var ffmpegCoverPathOptions = [
|
|
|
|
'-c:v copy',
|
|
|
|
'-map 2:v',
|
|
|
|
'-map 0:a'
|
|
|
|
]
|
|
|
|
|
|
|
|
ffmpegInputs.push(ffmpegCoverPathInput)
|
|
|
|
Logger.debug(`[AudioFileMetaDataManager] Cover found for "${audioFile.metadata.filename}". Cover will be merged to metadata`)
|
|
|
|
} else {
|
|
|
|
// remove the video stream to account for the user getting rid an existing cover in abs
|
|
|
|
var ffmpegCoverPathOptions = [
|
|
|
|
'-map 0',
|
|
|
|
'-map -0:v'
|
|
|
|
]
|
|
|
|
|
|
|
|
Logger.debug(`[AudioFileMetaDataManager] No cover found for "${audioFile.metadata.filename}". Cover will be skipped or removed from metadata`)
|
|
|
|
}
|
2022-07-06 02:53:01 +02:00
|
|
|
|
2022-06-05 19:36:42 +02:00
|
|
|
ffmpegOptions.push(...ffmpegCoverPathOptions)
|
|
|
|
|
2022-05-02 01:33:46 +02:00
|
|
|
var workerData = {
|
|
|
|
inputs: ffmpegInputs,
|
|
|
|
options: ffmpegOptions,
|
|
|
|
outputOptions: isM4b ? ['-f mp4'] : [],
|
|
|
|
output: outputPath,
|
|
|
|
}
|
|
|
|
var workerPath = Path.join(global.appRoot, 'server/utils/downloadWorker.js')
|
|
|
|
var worker = new workerThreads.Worker(workerPath, { workerData })
|
|
|
|
|
|
|
|
worker.on('message', async (message) => {
|
|
|
|
if (message != null && typeof message === 'object') {
|
|
|
|
if (message.type === 'RESULT') {
|
|
|
|
Logger.debug(message)
|
|
|
|
|
|
|
|
if (message.success) {
|
|
|
|
Logger.debug(`[AudioFileMetadataManager] Metadata encode SUCCESS for "${audioFile.metadata.filename}"`)
|
|
|
|
|
|
|
|
await filePerms.setDefault(outputPath, true)
|
|
|
|
|
|
|
|
fs.move(outputPath, inputPath, { overwrite: true }).then(() => {
|
|
|
|
Logger.debug(`[AudioFileMetadataManager] Audio file replaced successfully "${inputPath}"`)
|
|
|
|
|
|
|
|
resultPayload.success = true
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('audiofile_metadata_finished', resultPayload)
|
2022-05-02 01:33:46 +02:00
|
|
|
resolve(resultPayload)
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[AudioFileMetadataManager] Audio file failed to move "${inputPath}"`, error)
|
|
|
|
resultPayload.success = false
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('audiofile_metadata_finished', resultPayload)
|
2022-05-02 01:33:46 +02:00
|
|
|
resolve(resultPayload)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Logger.debug(`[AudioFileMetadataManager] Metadata encode FAILED for "${audioFile.metadata.filename}"`)
|
|
|
|
|
|
|
|
resultPayload.success = false
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('audiofile_metadata_finished', resultPayload)
|
2022-05-02 01:33:46 +02:00
|
|
|
resolve(resultPayload)
|
|
|
|
}
|
|
|
|
} else if (message.type === 'FFMPEG') {
|
2022-05-03 01:48:00 +02:00
|
|
|
if (message.level === 'debug' && process.env.NODE_ENV === 'production') {
|
|
|
|
// stderr is not necessary in production
|
|
|
|
} else if (Logger[message.level]) {
|
2022-05-02 01:33:46 +02:00
|
|
|
Logger[message.level](message.log)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Logger.error('Invalid worker message', message)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-06-04 19:17:42 +02:00
|
|
|
module.exports = AudioMetadataMangaer
|