From b3abeb20f16c2298fe6ac427936da3e1608dc5e0 Mon Sep 17 00:00:00 2001 From: Spencer Reiter Date: Sat, 11 Apr 2020 17:04:16 -0400 Subject: [PATCH] Added community plugin to set a default audio stream --- ...Tdarr_Plugin_c0r1_SetDefaultAudioStream.js | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 Community/Tdarr_Plugin_c0r1_SetDefaultAudioStream.js diff --git a/Community/Tdarr_Plugin_c0r1_SetDefaultAudioStream.js b/Community/Tdarr_Plugin_c0r1_SetDefaultAudioStream.js new file mode 100644 index 0000000..a74b89b --- /dev/null +++ b/Community/Tdarr_Plugin_c0r1_SetDefaultAudioStream.js @@ -0,0 +1,109 @@ +module.exports.details = function details() { + + return { + id: "Tdarr_Plugin_c0r1_SetDefaultAudioStream", + Stage: "Pre-processing", + Name: "Set Default Audio Stream", + Type: "Audio", + Operation: "Transcode", + Description: `This plugin will set an audio channel (2.0, 5.1, 7.1) to default and remove default from all other audio streams \n\n`, + Version: "0.1.0a", + Link: "", + Tags:"audio only", + + Inputs: [ + { + name: 'channels', + tooltip: `Desired audio channel number. + + \\nExample:\\n + + 2 + + \\nExample:\\n + + 6 + + \\nExample:\\n + + 8` + }, + ] + + } + +} + +module.exports.plugin = function plugin(file, librarySettings, inputs) { + var response = { + processFile: false, + preset: '', + container: '.' + file.container, + handBrakeMode: false, + FFmpegMode: true, + infoLog: '', + } + + var shouldProcess = false + var defaultAudioStreams = 0 + var matchingAudioStreams = 0 + var defaultSet = false + var ffmpegCommandInsert = '' + + + // Check if default audio stream matches user's channel selection + for (var i = 0; i < file.ffProbeData.streams.length; i++) { + if(file.ffProbeData.streams[i].codec_type.toLowerCase() === "audio" && + file.ffProbeData.streams[i].channels == inputs.channels) + { + matchingAudioStreams++ + if(file.ffProbeData.streams[i].disposition.default === 1){ + defaultAudioStreams++; + } + } + } + + // build command + for (var i = 0; i < file.ffProbeData.streams.length; i++) { + if(file.ffProbeData.streams[i].codec_type.toLowerCase() === "audio") + { + if(file.ffProbeData.streams[i].channels == inputs.channels) + { + if(!defaultSet){ + ffmpegCommandInsert += `-disposition:${i} default ` + defaultSet = true + } else { + ffmpegCommandInsert += `-disposition:${i} 0 ` + } + } else { + ffmpegCommandInsert += `-disposition:${i} 0 ` + } + } + } + + // Only process when there is a matching stream and + // when there is either no default or more than 1 default stream set + if(matchingAudioStreams >= 1 && defaultAudioStreams !== 1) { + shouldProcess = true; + response.infoLog += "☒ Matching audio stream is not set to default. \n" + } + + if (shouldProcess) { + response.processFile = true; + response.reQueueAfter = true; + response.preset = `,-map 0 -c copy ${ffmpegCommandInsert}`; + response.infoLog += "☒ Setting " + inputs.channels + " channel matching audio stream to default. Remove default from all other audio streams \n" + } else { + if(matchingAudioStreams < 1){ + response.infoLog += "☑ No " + inputs.channels + " channel audio stream exists. \n " + } else if (defaultAudioStreams === 1){ + response.infoLog += "☑ Default " + inputs.channels + " channel audio stream already exists. \n " + } else { + response.infoLog += "☑ Unexpected: Did not process \n " + } + + response.processFile = false; + } + return response + +} \ No newline at end of file