Skip to content

Commit

Permalink
Merge pull request #3329 from mikiher/embed-single-file
Browse files Browse the repository at this point in the history
Fix embed and convert for single file library items
  • Loading branch information
advplyr authored Aug 24, 2024
2 parents 519a1b0 + 4d8e1b7 commit e0de59a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
13 changes: 7 additions & 6 deletions server/managers/AbMergeManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,21 @@ class AbMergeManager {
async startAudiobookMerge(userId, libraryItem, options = {}) {
const task = new Task()

const audiobookDirname = Path.basename(libraryItem.path)
const targetFilename = audiobookDirname + '.m4b'
const audiobookBaseName = libraryItem.isFile ? Path.basename(libraryItem.path, Path.extname(libraryItem.path)) : Path.basename(libraryItem.path)
const targetFilename = audiobookBaseName + '.m4b'
const itemCachePath = Path.join(this.itemsCacheDir, libraryItem.id)
const tempFilepath = Path.join(itemCachePath, targetFilename)
const ffmetadataPath = Path.join(itemCachePath, 'ffmetadata.txt')
const libraryItemDir = libraryItem.isFile ? Path.dirname(libraryItem.path) : libraryItem.path
const taskData = {
libraryItemId: libraryItem.id,
libraryItemPath: libraryItem.path,
libraryItemDir,
userId,
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),
targetFilepath: Path.join(libraryItemDir, targetFilename),
itemCachePath,
ffmetadataObject: ffmpegHelpers.getFFMetadataObject(libraryItem, 1),
chapters: libraryItem.media.chapters?.map((c) => ({ ...c })),
Expand Down Expand Up @@ -95,8 +96,8 @@ class AbMergeManager {
*/
async runAudiobookMerge(libraryItem, task, encodingOptions) {
// Make sure the target directory is writable
if (!(await isWritable(libraryItem.path))) {
Logger.error(`[AbMergeManager] Target directory is not writable: ${libraryItem.path}`)
if (!(await isWritable(task.data.libraryItemDir))) {
Logger.error(`[AbMergeManager] Target directory is not writable: ${task.data.libraryItemDir}`)
task.setFailed('Target directory is not writable')
this.removeTask(task, true)
return
Expand Down
19 changes: 13 additions & 6 deletions server/managers/AudioMetadataManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ const TaskManager = require('./TaskManager')
const Task = require('../objects/Task')
const fileUtils = require('../utils/fileUtils')

/**
* @typedef UpdateMetadataOptions
* @property {boolean} [forceEmbedChapters=false] - Whether to force embed chapters.
* @property {boolean} [backup=false] - Whether to backup the files.
*/

class AudioMetadataMangaer {
constructor() {
this.itemsCacheDir = Path.join(global.MetadataPath, 'cache/items')
Expand Down Expand Up @@ -47,8 +53,8 @@ class AudioMetadataMangaer {
/**
*
* @param {string} userId
* @param {*} libraryItem
* @param {*} options
* @param {import('../objects/LibraryItem')} libraryItem
* @param {UpdateMetadataOptions} [options={}]
*/
async updateMetadataForItem(userId, libraryItem, options = {}) {
const forceEmbedChapters = !!options.forceEmbedChapters
Expand All @@ -67,9 +73,10 @@ class AudioMetadataMangaer {
if (audioFiles.some((a) => a.mimeType !== mimeType)) mimeType = null

// Create task
const libraryItemDir = libraryItem.isFile ? Path.dirname(libraryItem.path) : libraryItem.path
const taskData = {
libraryItemId: libraryItem.id,
libraryItemPath: libraryItem.path,
libraryItemDir,
userId,
audioFiles: audioFiles.map((af) => ({
index: af.index,
Expand Down Expand Up @@ -112,10 +119,10 @@ 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}`)
const targetDirWritable = await fileUtils.isWritable(task.data.libraryItemDir)
Logger.debug(`[AudioMetadataManager] Target directory ${task.data.libraryItemDir} writable: ${targetDirWritable}`)
if (!targetDirWritable) {
Logger.error(`[AudioMetadataManager] Target directory is not writable: ${task.data.libraryItemPath}`)
Logger.error(`[AudioMetadataManager] Target directory is not writable: ${task.data.libraryItemDir}`)
task.setFailed('Target directory is not writable')
this.handleTaskFinished(task)
return
Expand Down

0 comments on commit e0de59a

Please sign in to comment.