mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-01-16 19:08:42 +01:00
Merge pull request #720 from jmt-gh/fix_formatting
Fix indents from 4 spaces to 2 in parseOverdriveMediaMarkers.js
This commit is contained in:
commit
987f188f00
@ -2,147 +2,147 @@ const Logger = require('../../Logger')
|
|||||||
|
|
||||||
// given a list of audio files, extract all of the Overdrive Media Markers metaTags, and return an array of them as XML
|
// given a list of audio files, extract all of the Overdrive Media Markers metaTags, and return an array of them as XML
|
||||||
function extractOverdriveMediaMarkers(includedAudioFiles) {
|
function extractOverdriveMediaMarkers(includedAudioFiles) {
|
||||||
Logger.debug('[parseOverdriveMediaMarkers] Extracting overdrive media markers')
|
Logger.debug('[parseOverdriveMediaMarkers] Extracting overdrive media markers')
|
||||||
var markers = includedAudioFiles.map((af) => af.metaTags.tagOverdriveMediaMarker).filter(notUndefined => notUndefined !== undefined).filter(elem => { return elem !== null }) || []
|
var markers = includedAudioFiles.map((af) => af.metaTags.tagOverdriveMediaMarker).filter(notUndefined => notUndefined !== undefined).filter(elem => { return elem !== null }) || []
|
||||||
|
|
||||||
return markers
|
return markers
|
||||||
}
|
}
|
||||||
|
|
||||||
// given the array of Overdrive Media Markers from generateOverdriveMediaMarkers()
|
// given the array of Overdrive Media Markers from generateOverdriveMediaMarkers()
|
||||||
// parse and clean them in to something a bit more usable
|
// parse and clean them in to something a bit more usable
|
||||||
function cleanOverdriveMediaMarkers(overdriveMediaMarkers) {
|
function cleanOverdriveMediaMarkers(overdriveMediaMarkers) {
|
||||||
Logger.debug('[parseOverdriveMediaMarkers] Cleaning up overdrive media markers')
|
Logger.debug('[parseOverdriveMediaMarkers] Cleaning up overdrive media markers')
|
||||||
/*
|
/*
|
||||||
returns an array of arrays of objects. Each inner array corresponds to an audio track, with it's objects being a chapter:
|
returns an array of arrays of objects. Each inner array corresponds to an audio track, with it's objects being a chapter:
|
||||||
|
[
|
||||||
[
|
[
|
||||||
[
|
{
|
||||||
{
|
"Name": "Chapter 1",
|
||||||
"Name": "Chapter 1",
|
"Time": "0:00.000"
|
||||||
"Time": "0:00.000"
|
},
|
||||||
},
|
{
|
||||||
{
|
"Name": "Chapter 2",
|
||||||
"Name": "Chapter 2",
|
"Time": "15:51.000"
|
||||||
"Time": "15:51.000"
|
},
|
||||||
},
|
{ etc }
|
||||||
{ etc }
|
|
||||||
]
|
|
||||||
]
|
]
|
||||||
*/
|
]
|
||||||
|
*/
|
||||||
|
|
||||||
var parseString = require('xml2js').parseString; // function to convert xml to JSON
|
var parseString = require('xml2js').parseString; // function to convert xml to JSON
|
||||||
var parsedOverdriveMediaMarkers = []
|
var parsedOverdriveMediaMarkers = []
|
||||||
|
|
||||||
overdriveMediaMarkers.forEach(function (item, index) {
|
overdriveMediaMarkers.forEach(function (item, index) {
|
||||||
var parsed_result
|
var parsed_result
|
||||||
parseString(item, function (err, result) {
|
parseString(item, function (err, result) {
|
||||||
/*
|
/*
|
||||||
result.Markers.Marker is the result of parsing the XML for the MediaMarker tags for the MP3 file (Part##.mp3)
|
result.Markers.Marker is the result of parsing the XML for the MediaMarker tags for the MP3 file (Part##.mp3)
|
||||||
it is shaped like this, and needs further cleaning below:
|
it is shaped like this, and needs further cleaning below:
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"Name": [
|
"Name": [
|
||||||
"Chapter 1: "
|
"Chapter 1: "
|
||||||
],
|
],
|
||||||
"Time": [
|
"Time": [
|
||||||
"0:00.000"
|
"0:00.000"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ANOTHER CHAPTER
|
ANOTHER CHAPTER
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// The values for Name and Time in results.Markers.Marker are returned as Arrays from parseString and should be strings
|
// The values for Name and Time in results.Markers.Marker are returned as Arrays from parseString and should be strings
|
||||||
parsed_result = objectValuesArrayToString(result.Markers.Marker)
|
parsed_result = objectValuesArrayToString(result.Markers.Marker)
|
||||||
})
|
|
||||||
|
|
||||||
parsedOverdriveMediaMarkers.push(parsed_result)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return removeExtraChapters(parsedOverdriveMediaMarkers)
|
parsedOverdriveMediaMarkers.push(parsed_result)
|
||||||
|
})
|
||||||
|
|
||||||
|
return removeExtraChapters(parsedOverdriveMediaMarkers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// given an array of objects, convert any values that are arrays to strings
|
// given an array of objects, convert any values that are arrays to strings
|
||||||
function objectValuesArrayToString(arrayOfObjects) {
|
function objectValuesArrayToString(arrayOfObjects) {
|
||||||
Logger.debug('[parseOverdriveMediaMarkers] Converting Marker object values from arrays to strings')
|
Logger.debug('[parseOverdriveMediaMarkers] Converting Marker object values from arrays to strings')
|
||||||
arrayOfObjects.forEach((item) => {
|
arrayOfObjects.forEach((item) => {
|
||||||
Object.keys(item).forEach(key => {
|
Object.keys(item).forEach(key => {
|
||||||
item[key] = item[key].toString()
|
item[key] = item[key].toString()
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
return arrayOfObjects
|
return arrayOfObjects
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overdrive sometimes has weird chapters and subchapters defined
|
// Overdrive sometimes has weird chapters and subchapters defined
|
||||||
// These aren't necessary, so lets remove them
|
// These aren't necessary, so lets remove them
|
||||||
function removeExtraChapters(parsedOverdriveMediaMarkers) {
|
function removeExtraChapters(parsedOverdriveMediaMarkers) {
|
||||||
Logger.debug('[parseOverdriveMediaMarkers] Removing any unnecessary chapters')
|
Logger.debug('[parseOverdriveMediaMarkers] Removing any unnecessary chapters')
|
||||||
const weirdChapterFilterRegex = /([(]\d|[cC]ontinued)/
|
const weirdChapterFilterRegex = /([(]\d|[cC]ontinued)/
|
||||||
var cleaned = []
|
var cleaned = []
|
||||||
parsedOverdriveMediaMarkers.forEach(function (item) {
|
parsedOverdriveMediaMarkers.forEach(function (item) {
|
||||||
cleaned.push(item.filter(chapter => !weirdChapterFilterRegex.test(chapter.Name)))
|
cleaned.push(item.filter(chapter => !weirdChapterFilterRegex.test(chapter.Name)))
|
||||||
})
|
})
|
||||||
|
|
||||||
return cleaned
|
return cleaned
|
||||||
}
|
}
|
||||||
|
|
||||||
// Given a set of chapters from generateParsedChapters, add the end time to each one
|
// Given a set of chapters from generateParsedChapters, add the end time to each one
|
||||||
function addChapterEndTimes(chapters, totalAudioDuration) {
|
function addChapterEndTimes(chapters, totalAudioDuration) {
|
||||||
Logger.debug('[parseOverdriveMediaMarkers] Adding chapter end times')
|
Logger.debug('[parseOverdriveMediaMarkers] Adding chapter end times')
|
||||||
chapters.forEach((chapter, chapter_index) => {
|
chapters.forEach((chapter, chapter_index) => {
|
||||||
if (chapter_index < chapters.length - 1) {
|
if (chapter_index < chapters.length - 1) {
|
||||||
chapter.end = chapters[chapter_index + 1].start
|
chapter.end = chapters[chapter_index + 1].start
|
||||||
} else {
|
} else {
|
||||||
chapter.end = totalAudioDuration
|
chapter.end = totalAudioDuration
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return chapters
|
return chapters
|
||||||
}
|
}
|
||||||
|
|
||||||
// The function that actually generates the Chapters object that we update ABS with
|
// The function that actually generates the Chapters object that we update ABS with
|
||||||
function generateParsedChapters(includedAudioFiles, cleanedOverdriveMediaMarkers) {
|
function generateParsedChapters(includedAudioFiles, cleanedOverdriveMediaMarkers) {
|
||||||
Logger.debug('[parseOverdriveMediaMarkers] Generating new chapters for ABS')
|
Logger.debug('[parseOverdriveMediaMarkers] Generating new chapters for ABS')
|
||||||
// logic ported over from benonymity's OverdriveChapterizer:
|
// logic ported over from benonymity's OverdriveChapterizer:
|
||||||
// https://github.com/benonymity/OverdriveChapterizer/blob/main/chapters.py
|
// https://github.com/benonymity/OverdriveChapterizer/blob/main/chapters.py
|
||||||
var parsedChapters = []
|
var parsedChapters = []
|
||||||
var length = 0.0
|
var length = 0.0
|
||||||
var index = 0
|
var index = 0
|
||||||
var time = 0.0
|
var time = 0.0
|
||||||
|
|
||||||
// cleanedOverdriveMediaMarkers is an array of array of objects, where the inner array matches to the included audio files tracks
|
// cleanedOverdriveMediaMarkers is an array of array of objects, where the inner array matches to the included audio files tracks
|
||||||
// this allows us to leverage the individual track durations when calculating the start times of chapters in tracks after the first (using length)
|
// this allows us to leverage the individual track durations when calculating the start times of chapters in tracks after the first (using length)
|
||||||
includedAudioFiles.forEach((track, track_index) => {
|
includedAudioFiles.forEach((track, track_index) => {
|
||||||
cleanedOverdriveMediaMarkers[track_index].forEach((chapter) => {
|
cleanedOverdriveMediaMarkers[track_index].forEach((chapter) => {
|
||||||
time = chapter.Time.split(":")
|
time = chapter.Time.split(":")
|
||||||
time = length + parseFloat(time[0]) * 60 + parseFloat(time[1])
|
time = length + parseFloat(time[0]) * 60 + parseFloat(time[1])
|
||||||
var newChapterData = {
|
var newChapterData = {
|
||||||
id: index++,
|
id: index++,
|
||||||
start: time,
|
start: time,
|
||||||
title: chapter.Name
|
title: chapter.Name
|
||||||
}
|
}
|
||||||
parsedChapters.push(newChapterData)
|
parsedChapters.push(newChapterData)
|
||||||
})
|
|
||||||
length += track.duration
|
|
||||||
})
|
})
|
||||||
|
length += track.duration
|
||||||
|
})
|
||||||
|
|
||||||
parsedChapters = addChapterEndTimes(parsedChapters, length) // we need all the start times sorted out before we can add the end times
|
parsedChapters = addChapterEndTimes(parsedChapters, length) // we need all the start times sorted out before we can add the end times
|
||||||
|
|
||||||
return parsedChapters
|
return parsedChapters
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.overdriveMediaMarkersExist = (includedAudioFiles) => {
|
module.exports.overdriveMediaMarkersExist = (includedAudioFiles) => {
|
||||||
return extractOverdriveMediaMarkers(includedAudioFiles).length > 1
|
return extractOverdriveMediaMarkers(includedAudioFiles).length > 1
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.parseOverdriveMediaMarkersAsChapters = (includedAudioFiles) => {
|
module.exports.parseOverdriveMediaMarkersAsChapters = (includedAudioFiles) => {
|
||||||
Logger.info('[parseOverdriveMediaMarkers] Parsing of Overdrive Media Markers started')
|
Logger.info('[parseOverdriveMediaMarkers] Parsing of Overdrive Media Markers started')
|
||||||
|
|
||||||
var overdriveMediaMarkers = extractOverdriveMediaMarkers(includedAudioFiles)
|
var overdriveMediaMarkers = extractOverdriveMediaMarkers(includedAudioFiles)
|
||||||
var cleanedOverdriveMediaMarkers = cleanOverdriveMediaMarkers(overdriveMediaMarkers)
|
var cleanedOverdriveMediaMarkers = cleanOverdriveMediaMarkers(overdriveMediaMarkers)
|
||||||
var parsedChapters = generateParsedChapters(includedAudioFiles, cleanedOverdriveMediaMarkers)
|
var parsedChapters = generateParsedChapters(includedAudioFiles, cleanedOverdriveMediaMarkers)
|
||||||
|
|
||||||
return parsedChapters
|
return parsedChapters
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user