2023-03-30 01:05:53 +02:00
|
|
|
const axios = require('axios')
|
2022-07-07 00:38:19 +02:00
|
|
|
const Ffmpeg = require('../libs/fluentFfmpeg')
|
2022-07-06 02:53:01 +02:00
|
|
|
const fs = require('../libs/fsExtra')
|
2024-06-29 19:04:23 +02:00
|
|
|
const os = require('os')
|
2021-09-30 03:43:36 +02:00
|
|
|
const Path = require('path')
|
|
|
|
const Logger = require('../Logger')
|
2023-01-06 00:45:27 +01:00
|
|
|
const { filePathToPOSIX } = require('./fileUtils')
|
2021-09-04 21:17:26 +02:00
|
|
|
|
|
|
|
function escapeSingleQuotes(path) {
|
|
|
|
// return path.replace(/'/g, '\'\\\'\'')
|
2024-06-25 00:14:20 +02:00
|
|
|
return filePathToPOSIX(path).replace(/ /g, '\\ ').replace(/'/g, "\\'")
|
2021-09-04 21:17:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns first track start time
|
|
|
|
// startTime is for streams starting an encode part-way through an audiobook
|
|
|
|
async function writeConcatFile(tracks, outputPath, startTime = 0) {
|
|
|
|
var trackToStartWithIndex = 0
|
|
|
|
var firstTrackStartTime = 0
|
|
|
|
|
|
|
|
// Find first track greater than startTime
|
|
|
|
if (startTime > 0) {
|
|
|
|
var currTrackEnd = 0
|
2024-06-25 00:14:20 +02:00
|
|
|
var startingTrack = tracks.find((t) => {
|
2021-09-04 21:17:26 +02:00
|
|
|
currTrackEnd += t.duration
|
|
|
|
return startTime < currTrackEnd
|
|
|
|
})
|
|
|
|
if (startingTrack) {
|
|
|
|
firstTrackStartTime = currTrackEnd - startingTrack.duration
|
|
|
|
trackToStartWithIndex = startingTrack.index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-25 00:14:20 +02:00
|
|
|
var tracksToInclude = tracks.filter((t) => t.index >= trackToStartWithIndex)
|
|
|
|
var trackPaths = tracksToInclude.map((t) => {
|
2022-03-13 02:59:35 +01:00
|
|
|
var line = 'file ' + escapeSingleQuotes(t.metadata.path) + '\n' + `duration ${t.duration}`
|
2021-09-04 21:17:26 +02:00
|
|
|
return line
|
|
|
|
})
|
|
|
|
var inputstr = trackPaths.join('\n\n')
|
|
|
|
|
2024-04-27 23:41:57 +02:00
|
|
|
try {
|
|
|
|
await fs.writeFile(outputPath, inputstr)
|
|
|
|
return firstTrackStartTime
|
|
|
|
} catch (error) {
|
|
|
|
Logger.error(`[ffmpegHelpers] Failed to write stream concat file at "${outputPath}"`, error)
|
|
|
|
return null
|
|
|
|
}
|
2021-09-04 21:17:26 +02:00
|
|
|
}
|
2021-09-08 16:15:54 +02:00
|
|
|
module.exports.writeConcatFile = writeConcatFile
|
|
|
|
|
2021-09-30 03:43:36 +02:00
|
|
|
async function extractCoverArt(filepath, outputpath) {
|
|
|
|
var dirname = Path.dirname(outputpath)
|
|
|
|
await fs.ensureDir(dirname)
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
var ffmpeg = Ffmpeg(filepath)
|
2021-10-01 01:52:32 +02:00
|
|
|
ffmpeg.addOption(['-map 0:v', '-frames:v 1'])
|
2021-09-30 03:43:36 +02:00
|
|
|
ffmpeg.output(outputpath)
|
|
|
|
|
|
|
|
ffmpeg.on('start', (cmd) => {
|
|
|
|
Logger.debug(`[FfmpegHelpers] Extract Cover Cmd: ${cmd}`)
|
|
|
|
})
|
|
|
|
ffmpeg.on('error', (err, stdout, stderr) => {
|
|
|
|
Logger.error(`[FfmpegHelpers] Extract Cover Error ${err}`)
|
|
|
|
resolve(false)
|
|
|
|
})
|
|
|
|
ffmpeg.on('end', () => {
|
|
|
|
Logger.debug(`[FfmpegHelpers] Cover Art Extracted Successfully`)
|
|
|
|
resolve(outputpath)
|
|
|
|
})
|
|
|
|
ffmpeg.run()
|
|
|
|
})
|
|
|
|
}
|
2021-12-13 20:29:31 +01:00
|
|
|
module.exports.extractCoverArt = extractCoverArt
|
|
|
|
|
|
|
|
//This should convert based on the output file extension as well
|
|
|
|
async function resizeImage(filePath, outputPath, width, height) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
var ffmpeg = Ffmpeg(filePath)
|
|
|
|
ffmpeg.addOption(['-vf', `scale=${width || -1}:${height || -1}`])
|
|
|
|
ffmpeg.addOutput(outputPath)
|
|
|
|
ffmpeg.on('start', (cmd) => {
|
|
|
|
Logger.debug(`[FfmpegHelpers] Resize Image Cmd: ${cmd}`)
|
|
|
|
})
|
|
|
|
ffmpeg.on('error', (err, stdout, stderr) => {
|
2021-12-14 22:19:23 +01:00
|
|
|
Logger.error(`[FfmpegHelpers] Resize Image Error ${err} ${stdout} ${stderr}`)
|
2021-12-13 20:29:31 +01:00
|
|
|
resolve(false)
|
|
|
|
})
|
|
|
|
ffmpeg.on('end', () => {
|
|
|
|
Logger.debug(`[FfmpegHelpers] Image resized Successfully`)
|
|
|
|
resolve(outputPath)
|
|
|
|
})
|
|
|
|
ffmpeg.run()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
module.exports.resizeImage = resizeImage
|
2023-03-30 01:05:53 +02:00
|
|
|
|
|
|
|
module.exports.downloadPodcastEpisode = (podcastEpisodeDownload) => {
|
|
|
|
return new Promise(async (resolve) => {
|
|
|
|
const response = await axios({
|
|
|
|
url: podcastEpisodeDownload.url,
|
|
|
|
method: 'GET',
|
|
|
|
responseType: 'stream',
|
2024-06-25 00:14:20 +02:00
|
|
|
headers: {
|
|
|
|
'User-Agent': 'audiobookshelf (+https://audiobookshelf.org; like iTMS)'
|
|
|
|
},
|
2023-03-30 01:05:53 +02:00
|
|
|
timeout: 30000
|
2023-05-27 22:24:08 +02:00
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[ffmpegHelpers] Failed to download podcast episode with url "${podcastEpisodeDownload.url}"`, error)
|
|
|
|
return null
|
2023-03-30 01:05:53 +02:00
|
|
|
})
|
2023-05-27 22:24:08 +02:00
|
|
|
if (!response) return resolve(false)
|
|
|
|
|
2023-03-30 01:05:53 +02:00
|
|
|
const ffmpeg = Ffmpeg(response.data)
|
2024-02-16 23:05:02 +01:00
|
|
|
ffmpeg.addOption('-loglevel debug') // Debug logs printed on error
|
2024-06-25 00:14:20 +02:00
|
|
|
ffmpeg.outputOptions('-c:a', 'copy', '-map', '0:a', '-metadata', 'podcast=1')
|
2023-04-01 23:31:04 +02:00
|
|
|
|
|
|
|
const podcastMetadata = podcastEpisodeDownload.libraryItem.media.metadata
|
|
|
|
const podcastEpisode = podcastEpisodeDownload.podcastEpisode
|
2024-02-16 23:05:02 +01:00
|
|
|
const finalSizeInBytes = Number(podcastEpisode.enclosure?.length || 0)
|
2023-04-01 23:31:04 +02:00
|
|
|
|
|
|
|
const taggings = {
|
2024-06-25 00:14:20 +02:00
|
|
|
album: podcastMetadata.title,
|
2023-04-01 23:31:04 +02:00
|
|
|
'album-sort': podcastMetadata.title,
|
2024-06-25 00:14:20 +02:00
|
|
|
artist: podcastMetadata.author,
|
2023-04-01 23:31:04 +02:00
|
|
|
'artist-sort': podcastMetadata.author,
|
2024-06-25 00:14:20 +02:00
|
|
|
comment: podcastEpisode.description,
|
|
|
|
subtitle: podcastEpisode.subtitle,
|
|
|
|
disc: podcastEpisode.season,
|
|
|
|
genre: podcastMetadata.genres.length ? podcastMetadata.genres.join(';') : null,
|
|
|
|
language: podcastMetadata.language,
|
|
|
|
MVNM: podcastMetadata.title,
|
|
|
|
MVIN: podcastEpisode.episode,
|
|
|
|
track: podcastEpisode.episode,
|
2023-04-01 23:31:04 +02:00
|
|
|
'series-part': podcastEpisode.episode,
|
2024-06-25 00:14:20 +02:00
|
|
|
title: podcastEpisode.title,
|
2023-04-01 23:31:04 +02:00
|
|
|
'title-sort': podcastEpisode.title,
|
2024-06-25 00:14:20 +02:00
|
|
|
year: podcastEpisode.pubYear,
|
|
|
|
date: podcastEpisode.pubDate,
|
|
|
|
releasedate: podcastEpisode.pubDate,
|
2023-04-01 23:31:04 +02:00
|
|
|
'itunes-id': podcastMetadata.itunesId,
|
|
|
|
'podcast-type': podcastMetadata.type,
|
|
|
|
'episode-type': podcastMetadata.episodeType
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const tag in taggings) {
|
|
|
|
if (taggings[tag]) {
|
2023-07-09 22:50:40 +02:00
|
|
|
if (taggings[tag].length > 10000) {
|
|
|
|
Logger.warn(`[ffmpegHelpers] Episode download tag "${tag}" is too long (${taggings[tag].length} characters) - trimming it down`)
|
|
|
|
taggings[tag] = taggings[tag].slice(0, 10000)
|
|
|
|
}
|
2023-04-01 23:31:04 +02:00
|
|
|
ffmpeg.addOption('-metadata', `${tag}=${taggings[tag]}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 01:05:53 +02:00
|
|
|
ffmpeg.addOutput(podcastEpisodeDownload.targetPath)
|
|
|
|
|
2024-02-16 23:05:02 +01:00
|
|
|
const stderrLines = []
|
|
|
|
ffmpeg.on('stderr', (stderrLine) => {
|
|
|
|
if (typeof stderrLine === 'string') {
|
|
|
|
stderrLines.push(stderrLine)
|
|
|
|
}
|
|
|
|
})
|
2023-03-30 01:05:53 +02:00
|
|
|
ffmpeg.on('start', (cmd) => {
|
|
|
|
Logger.debug(`[FfmpegHelpers] downloadPodcastEpisode: Cmd: ${cmd}`)
|
|
|
|
})
|
2024-02-16 23:05:02 +01:00
|
|
|
ffmpeg.on('error', (err) => {
|
|
|
|
Logger.error(`[FfmpegHelpers] downloadPodcastEpisode: Error ${err}`)
|
|
|
|
if (stderrLines.length) {
|
|
|
|
Logger.error(`Full stderr dump for episode url "${podcastEpisodeDownload.url}": ${stderrLines.join('\n')}`)
|
|
|
|
}
|
2023-03-30 01:05:53 +02:00
|
|
|
resolve(false)
|
|
|
|
})
|
2024-02-16 23:05:02 +01:00
|
|
|
ffmpeg.on('progress', (progress) => {
|
|
|
|
let progressPercent = 0
|
|
|
|
if (finalSizeInBytes && progress.targetSize && !isNaN(progress.targetSize)) {
|
|
|
|
const finalSizeInKb = Math.floor(finalSizeInBytes / 1000)
|
|
|
|
progressPercent = Math.min(1, progress.targetSize / finalSizeInKb) * 100
|
|
|
|
}
|
|
|
|
Logger.debug(`[FfmpegHelpers] downloadPodcastEpisode: Progress estimate ${progressPercent.toFixed(0)}% (${progress?.targetSize || 'N/A'} KB) for "${podcastEpisodeDownload.url}"`)
|
|
|
|
})
|
2023-03-30 01:05:53 +02:00
|
|
|
ffmpeg.on('end', () => {
|
|
|
|
Logger.debug(`[FfmpegHelpers] downloadPodcastEpisode: Complete`)
|
|
|
|
resolve(podcastEpisodeDownload.targetPath)
|
|
|
|
})
|
|
|
|
ffmpeg.run()
|
|
|
|
})
|
2023-04-01 23:31:04 +02:00
|
|
|
}
|
2024-06-29 19:04:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates ffmetadata file content from the provided metadata object and chapters array.
|
|
|
|
* @param {Object} metadata - The input metadata object.
|
2024-06-30 14:45:25 +02:00
|
|
|
* @param {Array|null} chapters - An array of chapter objects.
|
2024-06-29 19:04:23 +02:00
|
|
|
* @returns {string} - The ffmetadata file content.
|
|
|
|
*/
|
|
|
|
function generateFFMetadata(metadata, chapters) {
|
|
|
|
let ffmetadataContent = ';FFMETADATA1\n'
|
|
|
|
|
|
|
|
// Add global metadata
|
|
|
|
for (const key in metadata) {
|
|
|
|
if (metadata[key]) {
|
|
|
|
ffmetadataContent += `${key}=${escapeFFMetadataValue(metadata[key])}\n`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add chapters
|
2024-06-30 14:45:25 +02:00
|
|
|
if (chapters) {
|
|
|
|
chapters.forEach((chapter) => {
|
|
|
|
ffmetadataContent += '\n[CHAPTER]\n'
|
|
|
|
ffmetadataContent += `TIMEBASE=1/1000\n`
|
|
|
|
ffmetadataContent += `START=${Math.floor(chapter.start * 1000)}\n`
|
|
|
|
ffmetadataContent += `END=${Math.floor(chapter.end * 1000)}\n`
|
|
|
|
if (chapter.title) {
|
|
|
|
ffmetadataContent += `title=${escapeFFMetadataValue(chapter.title)}\n`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2024-06-29 19:04:23 +02:00
|
|
|
|
|
|
|
return ffmetadataContent
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.generateFFMetadata = generateFFMetadata
|
|
|
|
|
2024-07-02 17:25:04 +02:00
|
|
|
async function writeFFMetadataFile(metadata, chapters, ffmetadataPath) {
|
|
|
|
try {
|
|
|
|
await fs.writeFile(ffmetadataPath, generateFFMetadata(metadata, chapters))
|
|
|
|
Logger.debug(`[ffmpegHelpers] Wrote ${ffmetadataPath}`)
|
|
|
|
return true
|
|
|
|
} catch (error) {
|
|
|
|
Logger.error(`[ffmpegHelpers] Write ${ffmetadataPath} failed`, error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.writeFFMetadataFile = writeFFMetadataFile
|
|
|
|
|
2024-06-29 19:04:23 +02:00
|
|
|
/**
|
|
|
|
* Adds an ffmetadata and optionally a cover image to an audio file using fluent-ffmpeg.
|
|
|
|
* @param {string} audioFilePath - Path to the input audio file.
|
|
|
|
* @param {string|null} coverFilePath - Path to the cover image file.
|
|
|
|
* @param {string} metadataFilePath - Path to the ffmetadata file.
|
2024-07-01 23:57:14 +02:00
|
|
|
* @param {number} track - The track number to embed in the audio file.
|
2024-06-30 14:45:25 +02:00
|
|
|
* @param {string} mimeType - The MIME type of the audio file.
|
2024-06-29 19:04:23 +02:00
|
|
|
*/
|
2024-07-01 23:57:14 +02:00
|
|
|
async function addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType) {
|
2024-06-30 14:45:25 +02:00
|
|
|
const isMp4 = mimeType === 'audio/mp4'
|
|
|
|
const isMp3 = mimeType === 'audio/mpeg'
|
|
|
|
|
|
|
|
const audioFileDir = Path.dirname(audioFilePath)
|
|
|
|
const audioFileExt = Path.extname(audioFilePath)
|
|
|
|
const audioFileBaseName = Path.basename(audioFilePath, audioFileExt)
|
|
|
|
const tempFilePath = Path.join(audioFileDir, `${audioFileBaseName}.tmp${audioFileExt}`)
|
|
|
|
|
2024-06-29 19:04:23 +02:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
let ffmpeg = Ffmpeg()
|
|
|
|
ffmpeg.input(audioFilePath).input(metadataFilePath).outputOptions([
|
|
|
|
'-map 0:a', // map audio stream from input file
|
2024-06-30 14:45:25 +02:00
|
|
|
'-map_metadata 1', // map metadata tags from metadata file first
|
|
|
|
'-map_metadata 0', // add additional metadata tags from input file
|
2024-06-29 19:04:23 +02:00
|
|
|
'-map_chapters 1', // map chapters from metadata file
|
2024-06-30 14:45:25 +02:00
|
|
|
'-c copy' // copy streams
|
2024-06-29 19:04:23 +02:00
|
|
|
])
|
|
|
|
|
2024-07-01 23:57:14 +02:00
|
|
|
if (track && !isNaN(track)) {
|
|
|
|
ffmpeg.outputOptions(['-metadata track=' + track])
|
|
|
|
}
|
|
|
|
|
2024-06-30 14:45:25 +02:00
|
|
|
if (isMp4) {
|
|
|
|
ffmpeg.outputOptions([
|
|
|
|
'-f mp4' // force output format to mp4
|
|
|
|
])
|
|
|
|
} else if (isMp3) {
|
|
|
|
ffmpeg.outputOptions([
|
|
|
|
'-id3v2_version 3' // set ID3v2 version to 3
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2024-06-29 19:04:23 +02:00
|
|
|
if (coverFilePath) {
|
|
|
|
ffmpeg.input(coverFilePath).outputOptions([
|
|
|
|
'-map 2:v', // map video stream from cover image file
|
|
|
|
'-disposition:v:0 attached_pic', // set cover image as attached picture
|
|
|
|
'-metadata:s:v',
|
|
|
|
'title=Cover', // add title metadata to cover image stream
|
|
|
|
'-metadata:s:v',
|
|
|
|
'comment=Cover' // add comment metadata to cover image stream
|
|
|
|
])
|
|
|
|
} else {
|
|
|
|
ffmpeg.outputOptions([
|
|
|
|
'-map 0:v?' // retain video stream from input file if exists
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
ffmpeg
|
|
|
|
.output(tempFilePath)
|
|
|
|
.on('start', function (commandLine) {
|
|
|
|
Logger.debug('[ffmpegHelpers] Spawned Ffmpeg with command: ' + commandLine)
|
|
|
|
})
|
|
|
|
.on('end', (stdout, stderr) => {
|
|
|
|
Logger.debug('[ffmpegHelpers] ffmpeg stdout:', stdout)
|
|
|
|
Logger.debug('[ffmpegHelpers] ffmpeg stderr:', stderr)
|
|
|
|
fs.copyFileSync(tempFilePath, audioFilePath)
|
|
|
|
fs.unlinkSync(tempFilePath)
|
|
|
|
resolve(true)
|
|
|
|
})
|
|
|
|
.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)
|
|
|
|
})
|
|
|
|
|
|
|
|
ffmpeg.run()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.addCoverAndMetadataToFile = addCoverAndMetadataToFile
|
|
|
|
|
|
|
|
function escapeFFMetadataValue(value) {
|
|
|
|
return value.replace(/([;=\n\\#])/g, '\\$1')
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFFMetadataObject(libraryItem, audioFilesLength) {
|
|
|
|
const metadata = libraryItem.media.metadata
|
|
|
|
|
|
|
|
const ffmetadata = {
|
|
|
|
title: metadata.title,
|
2024-06-30 14:45:25 +02:00
|
|
|
artist: metadata.authorName,
|
|
|
|
album_artist: metadata.authorName,
|
2024-06-29 19:04:23 +02:00
|
|
|
album: (metadata.title || '') + (metadata.subtitle ? `: ${metadata.subtitle}` : ''),
|
2024-06-30 14:45:25 +02:00
|
|
|
TIT3: metadata.subtitle, // mp3 only
|
2024-06-29 19:04:23 +02:00
|
|
|
genre: metadata.genres?.join('; '),
|
|
|
|
date: metadata.publishedYear,
|
|
|
|
comment: metadata.description,
|
|
|
|
description: metadata.description,
|
|
|
|
composer: metadata.narratorName,
|
|
|
|
copyright: metadata.publisher,
|
2024-06-30 14:45:25 +02:00
|
|
|
publisher: metadata.publisher, // mp3 only
|
|
|
|
TRACKTOTAL: `${audioFilesLength}`, // mp3 only
|
2024-06-29 19:04:23 +02:00
|
|
|
grouping: metadata.series?.map((s) => s.name + (s.sequence ? ` #${s.sequence}` : '')).join(', ')
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(ffmetadata).forEach((key) => {
|
|
|
|
if (!ffmetadata[key]) {
|
|
|
|
delete ffmetadata[key]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return ffmetadata
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.getFFMetadataObject = getFFMetadataObject
|