Adding chapters and downloading m4b file

This commit is contained in:
Mark Cooper 2021-09-08 09:15:54 -05:00
parent 26d922d3dc
commit 315de87bfc
24 changed files with 311 additions and 69 deletions

View file

@ -33,7 +33,8 @@ async function scan(path) {
language: audioStream.language,
channel_layout: audioStream.channel_layout,
channels: audioStream.channels,
sample_rate: audioStream.sample_rate
sample_rate: audioStream.sample_rate,
chapters: probeData.chapters || []
}
for (const key in probeData) {

View file

@ -13,9 +13,11 @@ Logger.info('[DownloadWorker] Starting Worker...')
const ffmpegCommand = Ffmpeg()
const startTime = Date.now()
ffmpegCommand.input(workerData.input)
if (workerData.inputFormat) ffmpegCommand.inputFormat(workerData.inputFormat)
if (workerData.inputOption) ffmpegCommand.inputOption(workerData.inputOption)
workerData.inputs.forEach((inputData) => {
ffmpegCommand.input(inputData.input)
if (inputData.options) ffmpegCommand.inputOption(inputData.options)
})
if (workerData.options) ffmpegCommand.addOption(workerData.options)
ffmpegCommand.output(workerData.output)

View file

@ -1,4 +1,5 @@
const fs = require('fs-extra')
const package = require('../../package.json')
function escapeSingleQuotes(path) {
// return path.replace(/'/g, '\'\\\'\'')
@ -34,4 +35,33 @@ async function writeConcatFile(tracks, outputPath, startTime = 0) {
return firstTrackStartTime
}
module.exports.writeConcatFile = writeConcatFile
module.exports.writeConcatFile = writeConcatFile
async function writeMetadataFile(audiobook, outputPath) {
var inputstrs = [
';FFMETADATA1',
`title=${audiobook.title}`,
`artist=${audiobook.author}`,
`date=${audiobook.book.publishYear || ''}`,
`comment=AudioBookshelf v${package.version}`,
'genre=Audiobook'
]
if (audiobook.chapters) {
audiobook.chapters.forEach((chap) => {
const chapterstrs = [
'[CHAPTER]',
'TIMEBASE=1/1000',
`START=${Math.round(chap.start * 1000)}`,
`END=${Math.round(chap.end * 1000)}`,
`title=${chap.title}`
]
inputstrs = inputstrs.concat(chapterstrs)
})
}
await fs.writeFile(outputPath, inputstrs.join('\n'))
return inputstrs
}
module.exports.writeMetadataFile = writeMetadataFile

View file

@ -110,9 +110,23 @@ function parseMediaStreamInfo(stream, all_streams, total_bit_rate) {
return info
}
function parseChapters(chapters) {
if (!chapters) return []
return chapters.map(chap => {
var title = chap['TAG:title'] || chap.title
var timebase = chap.time_base && chap.time_base.includes('/') ? Number(chap.time_base.split('/')[1]) : 1
return {
id: chap.id,
start: !isNaN(chap.start_time) ? chap.start_time : (chap.start / timebase),
end: chap.end_time || (chap.end / timebase),
title
}
})
}
function parseProbeData(data) {
try {
var { format, streams } = data
var { format, streams, chapters } = data
var { format_long_name, duration, size, bit_rate } = format
var sizeBytes = !isNaN(size) ? Number(size) : null
@ -146,6 +160,8 @@ function parseProbeData(data) {
}
}
cleanedData.chapters = parseChapters(chapters)
return cleanedData
} catch (error) {
console.error('Parse failed', error)
@ -155,7 +171,7 @@ function parseProbeData(data) {
function probe(filepath) {
return new Promise((resolve) => {
Ffmpeg.ffprobe(filepath, (err, raw) => {
Ffmpeg.ffprobe(filepath, ['-show_chapters'], (err, raw) => {
if (err) {
console.error(err)
resolve(null)