audiobookshelf/server/objects/legacy/AudioTrack.js

116 lines
3.0 KiB
JavaScript
Raw Normal View History

var { bytesPretty } = require('../../utils/fileUtils')
2021-08-18 00:01:11 +02:00
class AudioTrack {
constructor(audioTrack = null) {
this.index = null
this.ino = null
2021-08-18 00:01:11 +02:00
this.path = null
this.fullPath = null
this.ext = null
this.filename = null
this.format = null
this.duration = null
this.size = null
this.bitRate = null
this.language = null
this.codec = null
this.timeBase = null
this.channels = null
this.channelLayout = null
if (audioTrack) {
this.construct(audioTrack)
}
}
construct(audioTrack) {
this.index = audioTrack.index
this.ino = audioTrack.ino || null
2021-08-18 00:01:11 +02:00
this.path = audioTrack.path
this.fullPath = audioTrack.fullPath
this.ext = audioTrack.ext
this.filename = audioTrack.filename
this.format = audioTrack.format
this.duration = audioTrack.duration
this.size = audioTrack.size
this.bitRate = audioTrack.bitRate
this.language = audioTrack.language
this.codec = audioTrack.codec
this.timeBase = audioTrack.timeBase
this.channels = audioTrack.channels
this.channelLayout = audioTrack.channelLayout
}
get name() {
return `${String(this.index).padStart(3, '0')}: ${this.filename} (${bytesPretty(this.size)}) [${this.duration}]`
}
toJSON() {
return {
index: this.index,
ino: this.ino,
2021-08-18 00:01:11 +02:00
path: this.path,
fullPath: this.fullPath,
ext: this.ext,
filename: this.filename,
format: this.format,
duration: this.duration,
size: this.size,
bitRate: this.bitRate,
language: this.language,
codec: this.codec,
2021-08-18 00:01:11 +02:00
timeBase: this.timeBase,
channels: this.channels,
channelLayout: this.channelLayout,
2021-08-18 00:01:11 +02:00
}
}
setData(probeData) {
this.index = probeData.index
this.ino = probeData.ino || null
2021-08-18 00:01:11 +02:00
this.path = probeData.path
this.fullPath = probeData.fullPath
this.ext = probeData.ext
this.filename = probeData.filename
this.format = probeData.format
this.duration = probeData.duration
this.size = probeData.size
this.bitRate = probeData.bitRate
2021-08-18 00:01:11 +02:00
this.language = probeData.language
this.codec = probeData.codec || null
this.timeBase = probeData.timeBase
2021-08-18 00:01:11 +02:00
this.channels = probeData.channels
this.channelLayout = probeData.channelLayout
}
2021-08-18 00:01:11 +02:00
syncMetadata(audioFile) {
var hasUpdates = false
var keysToSync = ['format', 'duration', 'size', 'bitRate', 'language', 'codec', 'timeBase', 'channels', 'channelLayout']
keysToSync.forEach((key) => {
if (audioFile[key] !== undefined && audioFile[key] !== this[key]) {
hasUpdates = true
this[key] = audioFile[key]
}
})
return hasUpdates
2021-08-18 00:01:11 +02:00
}
syncFile(newFile) {
var hasUpdates = false
var keysToSync = ['path', 'fullPath', 'ext', 'filename']
keysToSync.forEach((key) => {
if (newFile[key] !== undefined && newFile[key] !== this[key]) {
hasUpdates = true
this[key] = newFile[key]
}
})
return hasUpdates
}
2021-08-18 00:01:11 +02:00
}
module.exports = AudioTrack