Add:M4b tool configurable options bitrate/channels/codec #1029 #1257

This commit is contained in:
advplyr 2022-12-19 17:13:04 -06:00
parent bc771a3a44
commit 616ffb8f79
3 changed files with 80 additions and 26 deletions

View File

@ -62,19 +62,38 @@
<div class="w-full h-px bg-white bg-opacity-10 my-8" /> <div class="w-full h-px bg-white bg-opacity-10 my-8" />
<div class="w-full max-w-4xl mx-auto"> <div class="w-full max-w-4xl mx-auto">
<div v-if="selectedTool === 'embed'" class="w-full flex justify-end items-center mb-4"> <div v-if="isEmbedTool" class="w-full flex justify-end items-center mb-4">
<ui-btn v-if="!isFinished" color="primary" :loading="processing" @click.stop="embedClick">{{ $strings.ButtonStartMetadataEmbed }}</ui-btn> <ui-btn v-if="!isFinished" color="primary" :loading="processing" @click.stop="embedClick">{{ $strings.ButtonStartMetadataEmbed }}</ui-btn>
<p v-else class="text-success text-lg font-semibold">{{ $strings.MessageEmbedFinished }}</p> <p v-else class="text-success text-lg font-semibold">{{ $strings.MessageEmbedFinished }}</p>
</div> </div>
<div v-else class="w-full flex justify-end items-center mb-4"> <div v-else class="w-full flex items-center mb-4">
<button :disabled="processing" class="text-sm uppercase text-gray-200 flex items-center pt-px pl-1 pr-2 hover:bg-white/5 rounded-md" @click="showEncodeOptions = !showEncodeOptions">
<span class="material-icons text-xl">{{ showEncodeOptions ? 'check_box' : 'check_box_outline_blank' }}</span> <span class="pl-1">Use Advanced Options</span>
</button>
<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 && 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" @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-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> <p v-else class="text-success text-lg font-semibold">{{ $strings.MessageM4BFinished }}</p>
</div> </div>
<div v-if="isM4BTool" class="overflow-hidden">
<transition name="slide">
<div v-if="showEncodeOptions" class="mb-4 pb-4 border-b border-white/10">
<div class="flex flex-wrap -mx-2">
<ui-text-input-with-label ref="bitrateInput" v-model="encodingOptions.bitrate" :disabled="processing || isTaskFinished" :label="'Audio Bitrate (e.g. 64k)'" class="m-2 max-w-40" />
<ui-text-input-with-label ref="channelsInput" v-model="encodingOptions.channels" :disabled="processing || isTaskFinished" :label="'Audio Channels (1 or 2)'" class="m-2 max-w-40" />
<ui-text-input-with-label ref="codecInput" v-model="encodingOptions.codec" :disabled="processing || isTaskFinished" :label="'Audio Codec'" class="m-2 max-w-40" />
</div>
<p class="text-sm text-warning">Warning: Do not update these settings unless you are familiar with ffmpeg encoding options.</p>
</div>
</transition>
</div>
<div class="mb-4"> <div class="mb-4">
<div v-if="selectedTool === 'embed'" class="flex items-start mb-2"> <div v-if="isEmbedTool" class="flex items-start mb-2">
<span class="material-icons text-base text-warning pt-1">star</span> <span class="material-icons text-base text-warning pt-1">star</span>
<p class="text-gray-200 ml-2">Metadata will be embedded in the audio tracks inside your audiobook folder.</p> <p class="text-gray-200 ml-2">Metadata will be embedded in the audio tracks inside your audiobook folder.</p>
</div> </div>
@ -91,15 +110,15 @@
A backup of your original audio files will be stored in <span class="rounded-md bg-neutral-600 text-sm text-white py-0.5 px-1 font-mono">/metadata/cache/items/{{ libraryItemId }}/</span>. Make sure to periodically purge items cache. A backup of your original audio files will be stored in <span class="rounded-md bg-neutral-600 text-sm text-white py-0.5 px-1 font-mono">/metadata/cache/items/{{ libraryItemId }}/</span>. Make sure to periodically purge items cache.
</p> </p>
</div> </div>
<div v-if="selectedTool === 'embed' && audioFiles.length > 1" class="flex items-start mb-2"> <div v-if="isEmbedTool && audioFiles.length > 1" class="flex items-start mb-2">
<span class="material-icons text-base text-warning pt-1">star</span> <span class="material-icons text-base text-warning pt-1">star</span>
<p class="text-gray-200 ml-2">Chapters are not embedded in multi-track audiobooks.</p> <p class="text-gray-200 ml-2">Chapters are not embedded in multi-track audiobooks.</p>
</div> </div>
<div v-if="selectedTool === 'm4b'" class="flex items-start mb-2"> <div v-if="isM4BTool" class="flex items-start mb-2">
<span class="material-icons text-base text-warning pt-1">star</span> <span class="material-icons text-base text-warning pt-1">star</span>
<p class="text-gray-200 ml-2">Encoding can take up to 30 minutes.</p> <p class="text-gray-200 ml-2">Encoding can take up to 30 minutes.</p>
</div> </div>
<div v-if="selectedTool === 'm4b'" class="flex items-start mb-2"> <div v-if="isM4BTool" class="flex items-start mb-2">
<span class="material-icons text-base text-warning pt-1">star</span> <span class="material-icons text-base text-warning pt-1">star</span>
<p class="text-gray-200 ml-2">If you have the watcher disabled you will need to re-scan this audiobook afterwards.</p> <p class="text-gray-200 ml-2">If you have the watcher disabled you will need to re-scan this audiobook afterwards.</p>
</div> </div>
@ -180,7 +199,13 @@ export default {
isFinished: false, isFinished: false,
toneObject: null, toneObject: null,
selectedTool: 'embed', selectedTool: 'embed',
isCancelingEncode: false isCancelingEncode: false,
showEncodeOptions: false,
encodingOptions: {
bitrate: '64k',
channels: '2',
codec: 'aac'
}
} }
}, },
watch: { watch: {
@ -193,6 +218,12 @@ export default {
} }
}, },
computed: { computed: {
isEmbedTool() {
return this.selectedTool === 'embed'
},
isM4BTool() {
return this.selectedTool === 'm4b'
},
libraryItemId() { libraryItemId() {
return this.libraryItem.id return this.libraryItem.id
}, },
@ -260,9 +291,23 @@ export default {
}) })
}, },
encodeM4bClick() { encodeM4bClick() {
if (this.$refs.bitrateInput) this.$refs.bitrateInput.blur()
if (this.$refs.channelsInput) this.$refs.channelsInput.blur()
if (this.$refs.codecInput) this.$refs.codecInput.blur()
let queryStr = ''
if (this.showEncodeOptions) {
const options = []
if (this.encodingOptions.bitrate) options.push(`bitrate=${this.encodingOptions.bitrate}`)
if (this.encodingOptions.channels) options.push(`channels=${this.encodingOptions.channels}`)
if (this.encodingOptions.codec) options.push(`codec=${this.encodingOptions.codec}`)
if (options.length) {
queryStr = `?${options.join('&')}`
}
}
this.processing = true this.processing = true
this.$axios this.$axios
.$post(`/api/tools/item/${this.libraryItemId}/encode-m4b`) .$post(`/api/tools/item/${this.libraryItemId}/encode-m4b${queryStr}`)
.then(() => { .then(() => {
console.log('Ab m4b merge started') console.log('Ab m4b merge started')
}) })

View File

@ -26,7 +26,8 @@ class ToolsController {
return res.status(500).send('Invalid audiobook: no audio tracks') return res.status(500).send('Invalid audiobook: no audio tracks')
} }
this.abMergeManager.startAudiobookMerge(req.user, req.libraryItem) const options = req.query || {}
this.abMergeManager.startAudiobookMerge(req.user, req.libraryItem, options)
res.sendStatus(200) res.sendStatus(200)
} }

View File

@ -45,7 +45,7 @@ class AbMergeManager {
this.downloadDirPathExist = true this.downloadDirPathExist = true
} }
async startAudiobookMerge(user, libraryItem) { async startAudiobookMerge(user, libraryItem, options = {}) {
const task = new Task() const task = new Task()
const audiobookDirname = Path.basename(libraryItem.path) const audiobookDirname = Path.basename(libraryItem.path)
@ -72,20 +72,28 @@ class AbMergeManager {
await fs.mkdir(taskData.itemCachePath) await fs.mkdir(taskData.itemCachePath)
} }
this.runAudiobookMerge(libraryItem, task) this.runAudiobookMerge(libraryItem, task, options || {})
} }
async runAudiobookMerge(libraryItem, task) { async runAudiobookMerge(libraryItem, task, encodingOptions) {
const audioBitrate = encodingOptions.bitrate || '64k'
const audioCodec = encodingOptions.codec || 'aac'
const audioChannels = encodingOptions.channels || 2
// If changing audio file type then encoding is needed // If changing audio file type then encoding is needed
var audioTracks = libraryItem.media.tracks const audioTracks = libraryItem.media.tracks
var audioRequiresEncode = audioTracks[0].metadata.ext !== '.m4b'
var firstTrackIsM4b = audioTracks[0].metadata.ext.toLowerCase() === '.m4b' // 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.
var isOneTrack = audioTracks.length === 1 // 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 = [] const ffmpegInputs = []
if (!isOneTrack) { if (!isOneTrack) {
var concatFilePath = Path.join(task.data.itemCachePath, 'files.txt') const concatFilePath = Path.join(task.data.itemCachePath, 'files.txt')
await writeConcatFile(audioTracks, concatFilePath) await writeConcatFile(audioTracks, concatFilePath)
ffmpegInputs.push({ ffmpegInputs.push({
input: concatFilePath, input: concatFilePath,
@ -99,15 +107,15 @@ class AbMergeManager {
} }
const logLevel = process.env.NODE_ENV === 'production' ? 'error' : 'warning' const logLevel = process.env.NODE_ENV === 'production' ? 'error' : 'warning'
var ffmpegOptions = [`-loglevel ${logLevel}`] let ffmpegOptions = [`-loglevel ${logLevel}`]
var ffmpegOutputOptions = ['-f mp4'] const ffmpegOutputOptions = ['-f mp4']
if (audioRequiresEncode) { if (audioRequiresEncode) {
ffmpegOptions = ffmpegOptions.concat([ ffmpegOptions = ffmpegOptions.concat([
'-map 0:a', '-map 0:a',
'-acodec aac', `-acodec ${audioCodec}`,
'-ac 2', `-ac ${audioChannels}`,
'-b:a 64k' `-b:a ${audioBitrate}`
]) ])
} else { } else {
ffmpegOptions.push('-max_muxing_queue_size 1000') ffmpegOptions.push('-max_muxing_queue_size 1000')
@ -119,7 +127,7 @@ class AbMergeManager {
} }
} }
var toneJsonPath = null let toneJsonPath = null
try { try {
toneJsonPath = Path.join(task.data.itemCachePath, 'metadata.json') toneJsonPath = Path.join(task.data.itemCachePath, 'metadata.json')
await toneHelpers.writeToneMetadataJsonFile(libraryItem, libraryItem.media.chapters, toneJsonPath, 1) await toneHelpers.writeToneMetadataJsonFile(libraryItem, libraryItem.media.chapters, toneJsonPath, 1)
@ -133,16 +141,16 @@ class AbMergeManager {
'TrackNumber': 1, 'TrackNumber': 1,
} }
var workerData = { const workerData = {
inputs: ffmpegInputs, inputs: ffmpegInputs,
options: ffmpegOptions, options: ffmpegOptions,
outputOptions: ffmpegOutputOptions, outputOptions: ffmpegOutputOptions,
output: task.data.tempFilepath output: task.data.tempFilepath
} }
var worker = null let worker = null
try { try {
var workerPath = Path.join(global.appRoot, 'server/utils/downloadWorker.js') const workerPath = Path.join(global.appRoot, 'server/utils/downloadWorker.js')
worker = new workerThreads.Worker(workerPath, { workerData }) worker = new workerThreads.Worker(workerPath, { workerData })
} catch (error) { } catch (error) {
Logger.error(`[AbMergeManager] Start worker thread failed`, error) Logger.error(`[AbMergeManager] Start worker thread failed`, error)