This commit is contained in:
HaveAGitGat 2019-12-04 17:31:02 +00:00
parent 32b8d73e0f
commit cea3c377bf
3 changed files with 488 additions and 21 deletions

View file

@ -10,7 +10,7 @@ module.exports = function transcodeAddAudioStream(file, audioEncoder, langTag, c
// note
try {
var audioCodec = audioEncoder
langTag = langTag.toLowerCase()
@ -22,8 +22,6 @@ module.exports = function transcodeAddAudioStream(file, audioEncoder, langTag, c
audioCodec = 'mp3'
}
//Step 1: Check if the file already has the required stream codec/langtag/channel count
var hasStreamAlready = file.ffProbeData.streams.filter(stream => {
@ -52,9 +50,6 @@ module.exports = function transcodeAddAudioStream(file, audioEncoder, langTag, c
var streamsWithLangTag = file.ffProbeData.streams.filter(stream => {
// console.log(stream)
console.log(stream.tags.language)
try {
if (stream.codec_type == 'audio' && stream.tags.language.toLowerCase().includes(langTag)) {
return true
@ -69,24 +64,20 @@ module.exports = function transcodeAddAudioStream(file, audioEncoder, langTag, c
if (streamsWithLangTag.length != 0) {
return attemptMakeStream(langTag)
return attemptMakeStreamLang(langTag)
} else {
return attemptMakeStream('und')
return attemptMakeStreamUnd('und')
}
function attemptMakeStream(langTag) {
function attemptMakeStreamLang(langTag) {
console.log(langTag)
var streamsWithLangTag = file.ffProbeData.streams.filter(stream => {
try {
console.log(langTag)
console.log(stream.tags.language.toLowerCase())
console.log(stream.tags.language.toLowerCase().includes(langTag))
if (stream.codec_type == 'audio' && stream.tags.language.toLowerCase().includes(langTag)) {
return true
@ -98,13 +89,6 @@ module.exports = function transcodeAddAudioStream(file, audioEncoder, langTag, c
})
if (streamsWithLangTag.length == 0) {
return {
preset: ``,
processFile: false,
note: `Unable to add audio stream in ${langTag}/und with ${channelCount} channels \n`
}
}
var highestChannelCount = streamsWithLangTag.reduce(getHighest)
@ -195,6 +179,123 @@ module.exports = function transcodeAddAudioStream(file, audioEncoder, langTag, c
}
function attemptMakeStreamUnd(langTag) {
console.log('No tracks with specified lang tag exist. Checking undefined tracks.')
console.log(langTag)
var streamsWithLangTag = file.ffProbeData.streams.filter(stream => {
try {
if (stream.codec_type == 'audio' && (stream.tags == undefined || stream.tags.language == undefined || stream.tags.language.toLowerCase().includes(langTag))) {
return true
}
} catch (err) { }
return false
})
if (streamsWithLangTag.length == 0) {
return {
preset: ``,
processFile: false,
note: `Unable to add audio stream in ${langTag}/und with ${channelCount} channels \n`
}
}
var highestChannelCount = streamsWithLangTag.reduce(getHighest)
function getHighest(first, second) {
if (first.channels > second.channels && first) {
return first
} else {
return second
}
}
if (parseInt(highestChannelCount.channels) >= parseInt(channelCount)) {
var hasStreamAlready = file.ffProbeData.streams.filter(stream => {
try {
if (stream.codec_type == 'audio' && stream.codec_name === audioCodec && (stream.tags == undefined || stream.tags.language == undefined || stream.tags.language.toLowerCase().includes(langTag)) && stream.channels == channelCount) {
return true
}
} catch (err) { }
return false
})
if (hasStreamAlready.length > 0) {
return {
preset: '',
processFile: false,
note: `File already has ${langTag} stream in ${audioEncoder}, ${channelCount} channels \n`
}
} else {
return {
preset: `,-map 0:v -map 0:${highestChannelCount.index} -map 0:a -map 0:s? -map 0:d? -c copy -c:a:0 ${audioEncoder} -ac ${channelCount}`,
processFile: true,
note: `The required channel count ${channelCount} is lower than the highest available channel count (${highestChannelCount.channels}). Adding! \n`
}
}
} else {
var hasStreamAlready = file.ffProbeData.streams.filter(stream => {
try {
if (stream.codec_type == 'audio' && stream.codec_name === audioCodec && (stream.tags == undefined || stream.tags.language == undefined || stream.tags.language.toLowerCase().includes(langTag)) && stream.channels == highestChannelCount.channels) {
return true
}
} catch (err) { }
return false
})
if (hasStreamAlready.length > 0) {
return {
preset: '',
processFile: false,
note: `File already has ${langTag} stream in ${audioEncoder}, ${highestChannelCount.channels} channels (Highest available) \n`
}
} else {
return {
preset: `,-map 0:v -map 0:${highestChannelCount.index} -map 0:a -map 0:s? -map 0:d? -c copy -c:a:0 ${audioEncoder} -ac ${highestChannelCount.channels}`,
processFile: true,
note: `The required channel count (${channelCount}) is higher than the highest channel available in specified lang tag (${highestChannelCount.channels}). Adding lower channel track. \n`
}
}
}
}