From cea3c377bf33ff2173c9b1102f7e4d21f529a72a Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Wed, 4 Dec 2019 17:31:02 +0000 Subject: [PATCH] New --- methods/actions.js | 3 +- .../actions/transcodeAddAudioStream.js | 133 ++++++- .../actions/transcodeKeepOneAudioStream.js | 365 ++++++++++++++++++ 3 files changed, 484 insertions(+), 17 deletions(-) create mode 100644 methods/library/actions/transcodeKeepOneAudioStream.js diff --git a/methods/actions.js b/methods/actions.js index c069fe9..ffb1668 100644 --- a/methods/actions.js +++ b/methods/actions.js @@ -10,4 +10,5 @@ const importFresh = require(rootModules+'import-fresh'); module.exports.remuxContainer = importFresh('./library/actions/remuxContainer.js') module.exports.transcodeStandardiseAudioCodecs = importFresh('./library/actions/transcodeStandardiseAudioCodecs.js') -module.exports.transcodeAddAudioStream = importFresh('./library/actions/transcodeAddAudioStream.js') \ No newline at end of file +module.exports.transcodeAddAudioStream = importFresh('./library/actions/transcodeAddAudioStream.js') +module.exports.transcodeKeepOneAudioStream = importFresh('./library/actions/transcodeKeepOneAudioStream.js') \ No newline at end of file diff --git a/methods/library/actions/transcodeAddAudioStream.js b/methods/library/actions/transcodeAddAudioStream.js index 639abd9..aeb785b 100644 --- a/methods/library/actions/transcodeAddAudioStream.js +++ b/methods/library/actions/transcodeAddAudioStream.js @@ -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,6 +89,116 @@ module.exports = function transcodeAddAudioStream(file, audioEncoder, langTag, c }) + + 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.language.toLowerCase().includes(langTag.toLowerCase()) && 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 { + + console.log('here3') + + var hasStreamAlready = file.ffProbeData.streams.filter(stream => { + + try { + if (stream.codec_type == 'audio' && stream.codec_name === audioCodec && stream.tags.language.toLowerCase().includes(langTag.toLowerCase()) && 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` + } + + } + + } + } + + + 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: ``, @@ -123,7 +224,7 @@ module.exports = function transcodeAddAudioStream(file, audioEncoder, langTag, c var hasStreamAlready = file.ffProbeData.streams.filter(stream => { try { - if (stream.codec_type == 'audio' && stream.codec_name === audioCodec && stream.tags.language.toLowerCase().includes(langTag.toLowerCase()) && stream.channels == channelCount) { + 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 } @@ -160,12 +261,12 @@ module.exports = function transcodeAddAudioStream(file, audioEncoder, langTag, c } else { - console.log('here3') + var hasStreamAlready = file.ffProbeData.streams.filter(stream => { try { - if (stream.codec_type == 'audio' && stream.codec_name === audioCodec && stream.tags.language.toLowerCase().includes(langTag.toLowerCase()) && stream.channels == highestChannelCount.channels) { + 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 } diff --git a/methods/library/actions/transcodeKeepOneAudioStream.js b/methods/library/actions/transcodeKeepOneAudioStream.js new file mode 100644 index 0000000..8e17882 --- /dev/null +++ b/methods/library/actions/transcodeKeepOneAudioStream.js @@ -0,0 +1,365 @@ + + +module.exports = function transcodeKeepOneAudioStream(file, audioEncoder, langTag, channelCount) { + + // response.preset = library.actions.transcodeKeepOneAudioStream(file, 'aac', 'en', 1).preset + + //Function required responses + // preset + // processFile + // note + + try { + + var audioCodec = audioEncoder + langTag = langTag.toLowerCase() + + if (audioEncoder == 'dca') { + audioCodec = 'dts' + } + + if (audioEncoder == 'libmp3lame') { + audioCodec = 'mp3' + } + + var reqLang = langTag + + var numberOfAudioStreams = file.ffProbeData.streams.filter(stream => stream.codec_type == 'audio').length + + + //Step 1: Check if the file already has the required stream codec/langtag/channel count + + var hasStreamAlready = file.ffProbeData.streams.filter(stream => { + + try { + if (stream.codec_type == 'audio' && stream.codec_name === audioCodec && stream.tags.language.toLowerCase().includes(langTag.toLowerCase()) && stream.channels == channelCount) { + return true + } + + } catch (err) { } + + return false + }) + + if (numberOfAudioStreams == 1 && hasStreamAlready.length == 1) { + + + + return { + preset: '', + processFile: false, + note: `File already has ${langTag} stream in ${audioEncoder}, ${channelCount} channels. It is the only track! \n` + } + + + } else if (hasStreamAlready.length >= 1) { + + var audioStreamToKeep = hasStreamAlready[0].index + var ffmpegCommandInsert = '' + for (var i = 0; i < file.ffProbeData.streams.length; i++) { + + try { + if (file.ffProbeData.streams[i].codec_type.toLowerCase() == "audio" && i !== audioStreamToKeep) { + ffmpegCommandInsert += ` -map -0:a:${i}` + + } + } catch (err) { } + + } + + return { + preset: `, -map 0 ${ffmpegCommandInsert} -c copy`, + processFile: true, + note: `File already has ${langTag} stream in ${audioEncoder}, ${channelCount} channels. It is not the only track, removing others. \n` + } + + } + + + //Step 2: Check if file has streams with specified lang tag + + var streamsWithLangTag = file.ffProbeData.streams.filter(stream => { + + try { + if (stream.codec_type == 'audio' && stream.tags.language.toLowerCase().includes(langTag)) { + return true + } + } catch (err) { } + + return false + + }) + + console.log("streamsWithLangTag:" + streamsWithLangTag) + + if (streamsWithLangTag.length != 0) { + + return attemptMakeStreamLang(langTag) + + } else { + + return attemptMakeStreamUnd('und') + + } + + + function attemptMakeStreamLang(langTag) { + + + var streamsWithLangTag = file.ffProbeData.streams.filter(stream => { + try { + if (stream.codec_type == 'audio' && stream.tags.language.toLowerCase().includes(langTag)) { + + return true + } + + } catch (err) { } + + return false + + }) + + + 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.language.toLowerCase().includes(langTag.toLowerCase()) && stream.channels == channelCount) { + return true + } + + } catch (err) { } + + return false + }) + + if (numberOfAudioStreams == 1 && hasStreamAlready.length == 1) { + + return { + preset: '', + processFile: false, + note: `The required stream already exists. It is the only audio stream. \n` + } + + } else if (hasStreamAlready.length >= 1) { + + return { + preset: `,-map 0:v -map 0:${hasStreamAlready[0].index} -map 0:s? -map 0:d? -c copy`, + processFile: true, + note: `The required stream already exists. Removing others. \n` + } + + } else { + + + return { + preset: `,-map 0:v -map 0:${highestChannelCount.index} -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 it and removing others! \n` + } + + } + + + + + } else { + + console.log('here3') + + var hasStreamAlready = file.ffProbeData.streams.filter(stream => { + + try { + if (stream.codec_type == 'audio' && stream.codec_name === audioCodec && stream.tags.language.toLowerCase().includes(langTag.toLowerCase()) && stream.channels == highestChannelCount.channels) { + return true + } + + } catch (err) { } + + return false + }) + + if (numberOfAudioStreams == 1 && hasStreamAlready.length == 1) { + + return { + preset: '', + processFile: false, + note: `The best ${reqLang} stream already exists. It is the only audio stream. \n` + } + + } else if (hasStreamAlready.length >= 1) { + + return { + preset: `,-map 0:v -map 0:${hasStreamAlready[0].index} -map 0:s? -map 0:d? -c copy`, + processFile: true, + note: `The best ${reqLang} stream already exists. Removing others. \n` + } + + } else { + + return { + preset: `,-map 0:v -map 0:${highestChannelCount.index} -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, removing others. \n` + } + + } + + } + } + + + 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 (numberOfAudioStreams == 1 && hasStreamAlready.length == 1) { + + return { + preset: '', + processFile: false, + note: `No ${reqLang} streams. The required und stream already exists. It is the only audio stream. \n` + } + + + + } else if (hasStreamAlready.length >= 1) { + + return { + preset: `,-map 0:v -map 0:${hasStreamAlready[0].index} -map 0:s? -map 0:d? -c copy`, + processFile: true, + note: `No ${reqLang} streams. The required und stream already exists. Removing others. \n` + } + + } else { + + + return { + preset: `,-map 0:v -map 0:${highestChannelCount.index} -map 0:s? -map 0:d? -c copy -c:a:0 ${audioEncoder} -ac ${channelCount}`, + processFile: true, + note: `No ${reqLang} streams. The required channel count ${channelCount} is lower than the highest available channel count (${highestChannelCount.channels}).Adding it and removing others! \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 (numberOfAudioStreams == 1 && hasStreamAlready.length == 1) { + + return { + preset: '', + processFile: false, + note: `No ${reqLang} streams. The best und stream already exists. It is the only audio stream. \n` + } + } else if (hasStreamAlready.length >= 1) { + + return { + preset: `,-map 0:v -map 0:${hasStreamAlready[0].index} -map 0:s? -map 0:d? -c copy`, + processFile: true, + note: `No ${reqLang} streams. The best stream already exists. Removing others. \n` + } + + } else { + + return { + preset: `,-map 0:v -map 0:${highestChannelCount.index} -map 0:s? -map 0:d? -c copy -c:a:0 ${audioEncoder} -ac ${highestChannelCount.channels}`, + processFile: true, + note: `No ${reqLang} streams. The required channel count (${channelCount}) is higher than the highest channel available in specified lang tag (${highestChannelCount.channels}). Adding lower channel track, removing others. \n` + } + } + } + } + + + } catch (err) { + + return { + preset: '', + processFile: false, + note: `library.actions.transcodeKeepOneAudioStream error: ${err} \n` + } + + } +}