From 3204bce8bba221a3bcdd1116c79de42aa9081a36 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Mon, 26 Jun 2023 18:04:11 +0100 Subject: [PATCH 01/12] Create Tdarr_Plugin_00td_action_transcode.js --- .../Tdarr_Plugin_00td_action_transcode.js | 481 ++++++++++++++++++ 1 file changed, 481 insertions(+) create mode 100644 Community/Tdarr_Plugin_00td_action_transcode.js diff --git a/Community/Tdarr_Plugin_00td_action_transcode.js b/Community/Tdarr_Plugin_00td_action_transcode.js new file mode 100644 index 0000000..ae186a4 --- /dev/null +++ b/Community/Tdarr_Plugin_00td_action_transcode.js @@ -0,0 +1,481 @@ +/* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */ +const details = () => ({ + id: 'Tdarr_Plugin_00td_action_transcode', + Stage: 'Pre-processing', + Name: 'Transcode a video file', + Type: 'Video', + Operation: 'Transcode', + Description: 'Transcode a video file using ffmpeg. GPU transcoding will be used if possible.', + Version: '3.1', + Tags: 'pre-processing,ffmpeg,video only,nvenc h265,configurable', + Inputs: [ + { + name: 'container', + type: 'string', + defaultValue: 'mkv', + inputUI: { + type: 'text', + }, + tooltip: `Specify output container of file. Use 'original' wihout quotes to keep original container. + \\n Ensure that all stream types you may have are supported by your chosen container. + \\n mkv is recommended. + \\nExample:\\n + mkv + + \\nExample:\\n + mp4 + + \\nExample:\\n + original`, + }, + { + name: 'bitrate_cutoff', + type: 'string', + defaultValue: '', + inputUI: { + type: 'text', + }, + tooltip: `Specify bitrate cutoff, files with a current bitrate lower then this will not be transcoded. + \\n Rate is in kbps. + \\n Leave empty to disable. + \\nExample:\\n + 6000 + + \\nExample:\\n + 4000`, + }, + { + name: 'enable_10bit', + type: 'boolean', + defaultValue: false, + inputUI: { + type: 'dropdown', + options: [ + 'false', + 'true', + ], + }, + tooltip: `Specify if output file should be 10bit. Default is false. + \\nExample:\\n + true + + \\nExample:\\n + false`, + }, + { + name: 'enable_bframes', + type: 'boolean', + defaultValue: false, + inputUI: { + type: 'dropdown', + options: [ + 'false', + 'true', + ], + }, + tooltip: `Specify if b frames should be used. + \\n Using B frames should decrease file sizes but are only supported on newer GPUs. + \\n Default is false. + \\nExample:\\n + true + + \\nExample:\\n + false`, + }, + { + name: 'force_conform', + type: 'boolean', + defaultValue: false, + inputUI: { + type: 'dropdown', + options: [ + 'false', + 'true', + ], + }, + tooltip: `Make the file conform to output containers requirements. + \\n Drop hdmv_pgs_subtitle/eia_608/subrip/timed_id3 for MP4. + \\n Drop data streams/mov_text/eia_608/timed_id3 for MKV. + \\n Default is false. + \\nExample:\\n + true + + \\nExample:\\n + false`, + }, + + { + name: 'encoder', + type: 'string', + defaultValue: 'hevc', + inputUI: { + type: 'dropdown', + options: [ + 'hevc', + 'vp9', + 'h264', + 'vp8', + ], + }, + tooltip: 'Specify the codec to use', + }, + { + name: 'use_gpu', + type: 'boolean', + defaultValue: true, + inputUI: { + type: 'dropdown', + options: [ + 'false', + 'true', + ], + }, + tooltip: 'If enabled then will use GPU if possible.', + }, + ], +}); + +const hasEncoder = async ({ + ffmpegPath, + encoder, +}) => { + const { exec } = require('child_process'); + let res = false; + try { + res = await new Promise((resolve) => { + exec(`${ffmpegPath} -f lavfi -i color=c=black:s=256x256:d=1:r=30 -c:v ${encoder} -f null /dev/null`, ( + error, + // stdout, + // stderr + ) => { + if (error) { + resolve(false); + return; + } + resolve(true); + }); + }); + } catch (err) { + // eslint-disable-next-line no-console + console.log(err); + } + + return res; +}; + +const getEncoder = async ({ + inputs, + otherArguments, +}) => { + let { encoder } = inputs; + if (inputs.use_gpu + && (inputs.encoder === 'hevc' || inputs.encoder === 'h264')) { + const gpuEncoders = [ + { + encoder: 'hevc_nvenc', + enabled: false, + }, + { + encoder: 'hevc_amf', + enabled: false, + }, + { + encoder: 'hevc_qsv', + enabled: false, + }, + { + encoder: 'hevc_videotoolbox', + enabled: false, + }, + + { + encoder: 'h264_nvenc', + enabled: false, + }, + { + encoder: 'h264_amf', + enabled: false, + }, + { + encoder: 'h264_qsv', + enabled: false, + }, + { + encoder: 'h264_videotoolbox', + enabled: false, + }, + ]; + + const filteredGpuEncoders = gpuEncoders.filter((device) => device.encoder.includes(inputs.encoder)); + + // eslint-disable-next-line no-restricted-syntax + for (const device of filteredGpuEncoders) { + // eslint-disable-next-line no-await-in-loop + device.enabled = await hasEncoder({ + ffmpegPath: otherArguments.ffmpegPath, + encoder: device.encoder, + }); + } + + const enabledDevices = gpuEncoders.filter((device) => device.enabled === true); + + if (enabledDevices.length > 0) { + encoder = enabledDevices[0].encoder; + } + } + + return encoder; +}; + +// eslint-disable-next-line no-unused-vars +const plugin = async (file, librarySettings, inputs, otherArguments) => { + const lib = require('../methods/lib')(); + // eslint-disable-next-line no-unused-vars,no-param-reassign + inputs = lib.loadDefaultValues(inputs, details); + const response = { + processFile: false, + preset: '', + handBrakeMode: false, + FFmpegMode: true, + reQueueAfter: true, + infoLog: '', + }; + + const encoder = await getEncoder({ + inputs, + otherArguments, + }); + + let duration = ''; + + // Check if inputs.container has been configured. If it hasn't then exit plugin. + if (inputs.container === '') { + response.infoLog += 'Plugin has not been configured, please configure required options. Skipping this plugin. \n'; + response.processFile = false; + return response; + } + + if (inputs.container === 'original') { + // eslint-disable-next-line no-param-reassign + inputs.container = `${file.container}`; + response.container = `.${file.container}`; + } else { + response.container = `.${inputs.container}`; + } + + // Check if file is a video. If it isn't then exit plugin. + if (file.fileMedium !== 'video') { + response.processFile = false; + response.infoLog += 'File is not a video. \n'; + return response; + } + + // Check if duration info is filled, if so times it by 0.0166667 to get time in minutes. + // If not filled then get duration of stream 0 and do the same. + if (parseFloat(file.ffProbeData?.format?.duration) > 0) { + duration = parseFloat(file.ffProbeData?.format?.duration) * 0.0166667; + } else if (typeof file.meta.Duration !== 'undefined') { + duration = file.meta.Duration * 0.0166667; + } else { + duration = file.ffProbeData.streams[0].duration * 0.0166667; + } + + // Set up required variables. + let videoIdx = 0; + let CPU10 = false; + let extraArguments = ''; + let genpts = ''; + let bitrateSettings = ''; + // Work out currentBitrate using "Bitrate = file size / (number of minutes * .0075)" + // Used from here https://blog.frame.io/2017/03/06/calculate-video-bitrates/ + // eslint-disable-next-line no-bitwise + const currentBitrate = ~~(file.file_size / (duration * 0.0075)); + // Use the same calculation used for currentBitrate but divide it in half to get targetBitrate. + // Logic of h265 can be half the bitrate as h264 without losing quality. + // eslint-disable-next-line no-bitwise + const targetBitrate = ~~(file.file_size / (duration * 0.0075) / 2); + // Allow some leeway under and over the targetBitrate. + // eslint-disable-next-line no-bitwise + const minimumBitrate = ~~(targetBitrate * 0.7); + // eslint-disable-next-line no-bitwise + const maximumBitrate = ~~(targetBitrate * 1.3); + + // If Container .ts or .avi set genpts to fix unknown timestamp + if (inputs.container.toLowerCase() === 'ts' || inputs.container.toLowerCase() === 'avi') { + genpts = '-fflags +genpts'; + } + + // If targetBitrate comes out as 0 then something has gone wrong and bitrates could not be calculated. + // Cancel plugin completely. + if (targetBitrate === 0) { + response.processFile = false; + response.infoLog += 'Target bitrate could not be calculated. Skipping this plugin. \n'; + return response; + } + + // Check if inputs.bitrate cutoff has something entered. + // (Entered means user actually wants something to happen, empty would disable this). + if (inputs.bitrate_cutoff !== '') { + // Checks if currentBitrate is below inputs.bitrate_cutoff. + // If so then cancel plugin without touching original files. + if (currentBitrate <= inputs.bitrate_cutoff) { + response.processFile = false; + response.infoLog += `Current bitrate is below set cutoff of ${inputs.bitrate_cutoff}. Cancelling plugin. \n`; + return response; + } + } + + // Check if force_conform option is checked. + // If so then check streams and add any extra parameters required to make file conform with output format. + if (inputs.force_conform === true) { + if (inputs.container.toLowerCase() === 'mkv') { + extraArguments += '-map -0:d '; + for (let i = 0; i < file.ffProbeData.streams.length; i++) { + try { + if ( + file.ffProbeData.streams[i].codec_name + .toLowerCase() === 'mov_text' + || file.ffProbeData.streams[i].codec_name + .toLowerCase() === 'eia_608' + || file.ffProbeData.streams[i].codec_name + .toLowerCase() === 'timed_id3' + ) { + extraArguments += `-map -0:${i} `; + } + } catch (err) { + // Error + } + } + } + if (inputs.container.toLowerCase() === 'mp4') { + for (let i = 0; i < file.ffProbeData.streams.length; i++) { + try { + if ( + file.ffProbeData.streams[i].codec_name + .toLowerCase() === 'hdmv_pgs_subtitle' + || file.ffProbeData.streams[i].codec_name + .toLowerCase() === 'eia_608' + || file.ffProbeData.streams[i].codec_name + .toLowerCase() === 'subrip' + || file.ffProbeData.streams[i].codec_name + .toLowerCase() === 'timed_id3' + ) { + extraArguments += `-map -0:${i} `; + } + } catch (err) { + // Error + } + } + } + } + + // Check if 10bit variable is true. + if (inputs.enable_10bit === true) { + // If set to true then add 10bit argument + extraArguments += '-pix_fmt p010le '; + } + + // Check if b frame variable is true. + if (encoder === 'hevc_nvenc' && inputs.enable_bframes === true) { + // If set to true then add b frames argument + extraArguments += '-bf 5 '; + } + + // Go through each stream in the file. + for (let i = 0; i < file.ffProbeData.streams.length; i++) { + // Check if stream is a video. + let codec_type = ''; + try { + codec_type = file.ffProbeData.streams[i].codec_type.toLowerCase(); + } catch (err) { + // err + } + if (codec_type === 'video') { + // Check if codec of stream is mjpeg/png, if so then remove this "video" stream. + // mjpeg/png are usually embedded pictures that can cause havoc with plugins. + if (file.ffProbeData.streams[i].codec_name === 'mjpeg' || file.ffProbeData.streams[i].codec_name === 'png') { + extraArguments += `-map -v:${videoIdx} `; + } + // Check if codec of stream is hevc or vp9 AND check if file.container matches inputs.container. + // If so nothing for plugin to do. + if ( + ( + inputs.encoder === file.ffProbeData.streams[i].codec_name + ) + && file.container === inputs.container + ) { + response.processFile = false; + response.infoLog += `File is already ${inputs.encoder} & in ${inputs.container}. \n`; + return response; + } + // Check if codec of stream is hevc or vp9 + // AND check if file.container does NOT match inputs.container. + // If so remux file. + if ( + ( + inputs.encoder === file.ffProbeData.streams[i].codec_name + ) + && file.container !== inputs.container + ) { + response.infoLog += `File is hevc or vp9 but is not in ${inputs.container} container. Remuxing. \n`; + response.preset = `, -map 0 -c copy ${extraArguments}`; + response.processFile = true; + return response; + } + + // Check if video stream is HDR or 10bit + if ( + inputs.encoder === 'hevc' + && (file.ffProbeData.streams[i].profile === 'High 10' + || file.ffProbeData.streams[i].bits_per_raw_sample === '10') + ) { + CPU10 = true; + } + + // Increment videoIdx. + videoIdx += 1; + } + } + + // Set bitrateSettings variable using bitrate information calulcated earlier. + bitrateSettings = `-b:v ${targetBitrate}k -minrate ${minimumBitrate}k ` + + `-maxrate ${maximumBitrate}k -bufsize ${currentBitrate}k`; + // Print to infoLog information around file & bitrate settings. + response.infoLog += `Container for output selected as ${inputs.container}. \n`; + response.infoLog += `Current bitrate = ${currentBitrate} \n`; + response.infoLog += 'Bitrate settings: \n'; + response.infoLog += `Target = ${targetBitrate} \n`; + response.infoLog += `Minimum = ${minimumBitrate} \n`; + response.infoLog += `Maximum = ${maximumBitrate} \n`; + + if (encoder === 'hevc_nvenc' || encoder === 'h264_nvenc') { + // Codec will be checked so it can be transcoded correctly + if (file.video_codec_name === 'h263') { + response.preset = '-c:v h263_cuvid'; + } else if (file.video_codec_name === 'h264') { + if (CPU10 === false) { + response.preset = '-c:v h264_cuvid'; + } + } else if (file.video_codec_name === 'mjpeg') { + response.preset = '-c:v mjpeg_cuvid'; + } else if (file.video_codec_name === 'mpeg1') { + response.preset = '-c:v mpeg1_cuvid'; + } else if (file.video_codec_name === 'mpeg2') { + response.preset = '-c:v mpeg2_cuvid'; + } else if (file.video_codec_name === 'mpeg4') { + response.preset = '-c:v mpeg4_cuvid'; + } else if (file.video_codec_name === 'vc1') { + response.preset = '-c:v vc1_cuvid'; + } else if (file.video_codec_name === 'vp8') { + response.preset = '-c:v vp8_cuvid'; + } + } + + response.preset += `${genpts}, -map 0 -c:v ${encoder} -cq:v 19 ${bitrateSettings} ` + + `-spatial_aq:v 1 -rc-lookahead:v 32 -c:a copy -c:s copy -max_muxing_queue_size 9999 ${extraArguments}`; + response.processFile = true; + response.infoLog += 'File is not hevc or vp9. Transcoding. \n'; + return response; +}; +module.exports.details = details; +module.exports.plugin = plugin; From abfa9af5aa8316eaddd7ee08916169da49b2464a Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Tue, 27 Jun 2023 05:52:43 +0100 Subject: [PATCH 02/12] Adjustments based on comments --- .../Tdarr_Plugin_00td_action_transcode.js | 188 ++++++++---------- 1 file changed, 82 insertions(+), 106 deletions(-) diff --git a/Community/Tdarr_Plugin_00td_action_transcode.js b/Community/Tdarr_Plugin_00td_action_transcode.js index ae186a4..6a2e01b 100644 --- a/Community/Tdarr_Plugin_00td_action_transcode.js +++ b/Community/Tdarr_Plugin_00td_action_transcode.js @@ -9,29 +9,56 @@ const details = () => ({ Version: '3.1', Tags: 'pre-processing,ffmpeg,video only,nvenc h265,configurable', Inputs: [ + { + name: 'encoder', + type: 'string', + defaultValue: 'hevc', + inputUI: { + type: 'dropdown', + options: [ + 'hevc', + 'vp9', + 'h264', + 'vp8', + ], + }, + tooltip: 'Specify the codec to use', + }, + { + name: 'use_gpu', + type: 'boolean', + defaultValue: true, + inputUI: { + type: 'dropdown', + options: [ + 'false', + 'true', + ], + }, + tooltip: 'If enabled then will use GPU if possible.', + }, { name: 'container', type: 'string', defaultValue: 'mkv', inputUI: { - type: 'text', + type: 'dropdown', + options: [ + 'mkv', + 'mp4', + 'avi', + 'ts', + 'original', + ], }, tooltip: `Specify output container of file. Use 'original' wihout quotes to keep original container. \\n Ensure that all stream types you may have are supported by your chosen container. - \\n mkv is recommended. - \\nExample:\\n - mkv - - \\nExample:\\n - mp4 - - \\nExample:\\n - original`, + \\n mkv is recommended`, }, { name: 'bitrate_cutoff', - type: 'string', - defaultValue: '', + type: 'number', + defaultValue: 0, inputUI: { type: 'text', }, @@ -103,35 +130,6 @@ const details = () => ({ \\nExample:\\n false`, }, - - { - name: 'encoder', - type: 'string', - defaultValue: 'hevc', - inputUI: { - type: 'dropdown', - options: [ - 'hevc', - 'vp9', - 'h264', - 'vp8', - ], - }, - tooltip: 'Specify the codec to use', - }, - { - name: 'use_gpu', - type: 'boolean', - defaultValue: true, - inputUI: { - type: 'dropdown', - options: [ - 'false', - 'true', - ], - }, - tooltip: 'If enabled then will use GPU if possible.', - }, ], }); @@ -140,9 +138,9 @@ const hasEncoder = async ({ encoder, }) => { const { exec } = require('child_process'); - let res = false; + let isEnabled = false; try { - res = await new Promise((resolve) => { + isEnabled = await new Promise((resolve) => { exec(`${ffmpegPath} -f lavfi -i color=c=black:s=256x256:d=1:r=30 -c:v ${encoder} -f null /dev/null`, ( error, // stdout, @@ -160,7 +158,7 @@ const hasEncoder = async ({ console.log(err); } - return res; + return isEnabled; }; const getEncoder = async ({ @@ -168,8 +166,7 @@ const getEncoder = async ({ otherArguments, }) => { let { encoder } = inputs; - if (inputs.use_gpu - && (inputs.encoder === 'hevc' || inputs.encoder === 'h264')) { + if (inputs.use_gpu && (inputs.encoder === 'hevc' || inputs.encoder === 'h264')) { const gpuEncoders = [ { encoder: 'hevc_nvenc', @@ -209,11 +206,11 @@ const getEncoder = async ({ const filteredGpuEncoders = gpuEncoders.filter((device) => device.encoder.includes(inputs.encoder)); // eslint-disable-next-line no-restricted-syntax - for (const device of filteredGpuEncoders) { + for (const gpuEncoder of filteredGpuEncoders) { // eslint-disable-next-line no-await-in-loop - device.enabled = await hasEncoder({ + gpuEncoder.enabled = await hasEncoder({ ffmpegPath: otherArguments.ffmpegPath, - encoder: device.encoder, + encoder: gpuEncoder.encoder, }); } @@ -246,18 +243,7 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { otherArguments, }); - let duration = ''; - - // Check if inputs.container has been configured. If it hasn't then exit plugin. - if (inputs.container === '') { - response.infoLog += 'Plugin has not been configured, please configure required options. Skipping this plugin. \n'; - response.processFile = false; - return response; - } - if (inputs.container === 'original') { - // eslint-disable-next-line no-param-reassign - inputs.container = `${file.container}`; response.container = `.${file.container}`; } else { response.container = `.${inputs.container}`; @@ -265,19 +251,19 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { // Check if file is a video. If it isn't then exit plugin. if (file.fileMedium !== 'video') { - response.processFile = false; response.infoLog += 'File is not a video. \n'; return response; } - // Check if duration info is filled, if so times it by 0.0166667 to get time in minutes. - // If not filled then get duration of stream 0 and do the same. + let duration = 0; + + // Get duration in seconds if (parseFloat(file.ffProbeData?.format?.duration) > 0) { - duration = parseFloat(file.ffProbeData?.format?.duration) * 0.0166667; + duration = parseFloat(file.ffProbeData?.format?.duration); } else if (typeof file.meta.Duration !== 'undefined') { - duration = file.meta.Duration * 0.0166667; + duration = file.meta.Duration; } else { - duration = file.ffProbeData.streams[0].duration * 0.0166667; + duration = file.ffProbeData.streams[0].duration; } // Set up required variables. @@ -286,49 +272,45 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { let extraArguments = ''; let genpts = ''; let bitrateSettings = ''; - // Work out currentBitrate using "Bitrate = file size / (number of minutes * .0075)" + // Used from here https://blog.frame.io/2017/03/06/calculate-video-bitrates/ - // eslint-disable-next-line no-bitwise - const currentBitrate = ~~(file.file_size / (duration * 0.0075)); + + const currentBitrate = (file.file_size * 1024 * 1024 * 8) / duration; // Use the same calculation used for currentBitrate but divide it in half to get targetBitrate. // Logic of h265 can be half the bitrate as h264 without losing quality. - // eslint-disable-next-line no-bitwise - const targetBitrate = ~~(file.file_size / (duration * 0.0075) / 2); + + const targetBitrate = currentBitrate / 2; // Allow some leeway under and over the targetBitrate. - // eslint-disable-next-line no-bitwise - const minimumBitrate = ~~(targetBitrate * 0.7); - // eslint-disable-next-line no-bitwise - const maximumBitrate = ~~(targetBitrate * 1.3); + + const minimumBitrate = (targetBitrate * 0.7); + + const maximumBitrate = (targetBitrate * 1.3); // If Container .ts or .avi set genpts to fix unknown timestamp - if (inputs.container.toLowerCase() === 'ts' || inputs.container.toLowerCase() === 'avi') { + if (inputs.container === 'ts' || inputs.container === 'avi') { genpts = '-fflags +genpts'; } // If targetBitrate comes out as 0 then something has gone wrong and bitrates could not be calculated. // Cancel plugin completely. if (targetBitrate === 0) { - response.processFile = false; response.infoLog += 'Target bitrate could not be calculated. Skipping this plugin. \n'; return response; } // Check if inputs.bitrate cutoff has something entered. // (Entered means user actually wants something to happen, empty would disable this). - if (inputs.bitrate_cutoff !== '') { - // Checks if currentBitrate is below inputs.bitrate_cutoff. - // If so then cancel plugin without touching original files. - if (currentBitrate <= inputs.bitrate_cutoff) { - response.processFile = false; - response.infoLog += `Current bitrate is below set cutoff of ${inputs.bitrate_cutoff}. Cancelling plugin. \n`; - return response; - } + // Checks if currentBitrate is below inputs.bitrate_cutoff. + // If so then cancel plugin without touching original files. + if (currentBitrate <= inputs.bitrate_cutoff) { + response.infoLog += `Current bitrate is below set cutoff of ${inputs.bitrate_cutoff}. Cancelling plugin. \n`; + return response; } // Check if force_conform option is checked. // If so then check streams and add any extra parameters required to make file conform with output format. if (inputs.force_conform === true) { - if (inputs.container.toLowerCase() === 'mkv') { + if (inputs.container === 'mkv') { extraArguments += '-map -0:d '; for (let i = 0; i < file.ffProbeData.streams.length; i++) { try { @@ -347,7 +329,7 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { } } } - if (inputs.container.toLowerCase() === 'mp4') { + if (inputs.container === 'mp4') { for (let i = 0; i < file.ffProbeData.streams.length; i++) { try { if ( @@ -399,12 +381,9 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { // Check if codec of stream is hevc or vp9 AND check if file.container matches inputs.container. // If so nothing for plugin to do. if ( - ( - inputs.encoder === file.ffProbeData.streams[i].codec_name - ) + inputs.encoder === file.ffProbeData.streams[i].codec_name && file.container === inputs.container ) { - response.processFile = false; response.infoLog += `File is already ${inputs.encoder} & in ${inputs.container}. \n`; return response; } @@ -412,13 +391,13 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { // AND check if file.container does NOT match inputs.container. // If so remux file. if ( - ( - inputs.encoder === file.ffProbeData.streams[i].codec_name - ) + + inputs.encoder === file.ffProbeData.streams[i].codec_name + && file.container !== inputs.container ) { response.infoLog += `File is hevc or vp9 but is not in ${inputs.container} container. Remuxing. \n`; - response.preset = `, -map 0 -c copy ${extraArguments}`; + response.preset = ` -map 0 -c copy ${extraArguments}`; response.processFile = true; return response; } @@ -427,7 +406,7 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { if ( inputs.encoder === 'hevc' && (file.ffProbeData.streams[i].profile === 'High 10' - || file.ffProbeData.streams[i].bits_per_raw_sample === '10') + || file.ffProbeData.streams[i].bits_per_raw_sample === '10') ) { CPU10 = true; } @@ -438,8 +417,8 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { } // Set bitrateSettings variable using bitrate information calulcated earlier. - bitrateSettings = `-b:v ${targetBitrate}k -minrate ${minimumBitrate}k ` - + `-maxrate ${maximumBitrate}k -bufsize ${currentBitrate}k`; + bitrateSettings = `-b:v ${targetBitrate} -minrate ${minimumBitrate} ` + + `-maxrate ${maximumBitrate} -bufsize ${currentBitrate}`; // Print to infoLog information around file & bitrate settings. response.infoLog += `Container for output selected as ${inputs.container}. \n`; response.infoLog += `Current bitrate = ${currentBitrate} \n`; @@ -448,14 +427,11 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { response.infoLog += `Minimum = ${minimumBitrate} \n`; response.infoLog += `Maximum = ${maximumBitrate} \n`; - if (encoder === 'hevc_nvenc' || encoder === 'h264_nvenc') { - // Codec will be checked so it can be transcoded correctly + if (encoder.contains('nvenc')) { if (file.video_codec_name === 'h263') { response.preset = '-c:v h263_cuvid'; - } else if (file.video_codec_name === 'h264') { - if (CPU10 === false) { - response.preset = '-c:v h264_cuvid'; - } + } else if (file.video_codec_name === 'h264' && CPU10 === false) { + response.preset = '-c:v h264_cuvid'; } else if (file.video_codec_name === 'mjpeg') { response.preset = '-c:v mjpeg_cuvid'; } else if (file.video_codec_name === 'mpeg1') { @@ -471,8 +447,8 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { } } - response.preset += `${genpts}, -map 0 -c:v ${encoder} -cq:v 19 ${bitrateSettings} ` - + `-spatial_aq:v 1 -rc-lookahead:v 32 -c:a copy -c:s copy -max_muxing_queue_size 9999 ${extraArguments}`; + response.preset += `${genpts} -map 0 -c copy -c:v ${encoder} -cq:v 19 ${bitrateSettings} ` + + `-spatial_aq:v 1 -rc-lookahead:v 32 -max_muxing_queue_size 9999 ${extraArguments}`; response.processFile = true; response.infoLog += 'File is not hevc or vp9. Transcoding. \n'; return response; From d8a86dc6d562cf05ab293f7788aeeaabf56b5bf2 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Tue, 27 Jun 2023 06:07:49 +0100 Subject: [PATCH 03/12] Adjust bframes options --- .../Tdarr_Plugin_00td_action_transcode.js | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Community/Tdarr_Plugin_00td_action_transcode.js b/Community/Tdarr_Plugin_00td_action_transcode.js index 6a2e01b..5d11405 100644 --- a/Community/Tdarr_Plugin_00td_action_transcode.js +++ b/Community/Tdarr_Plugin_00td_action_transcode.js @@ -90,7 +90,7 @@ const details = () => ({ false`, }, { - name: 'enable_bframes', + name: 'bframes_enabled', type: 'boolean', defaultValue: false, inputUI: { @@ -109,6 +109,15 @@ const details = () => ({ \\nExample:\\n false`, }, + { + name: 'bframes_value', + type: 'number', + defaultValue: 5, + inputUI: { + type: 'text', + }, + tooltip: 'Specify number of bframes to use.', + }, { name: 'force_conform', type: 'boolean', @@ -133,6 +142,11 @@ const details = () => ({ ], }); +const bframeSupport = [ + 'hevc_nvenc', + 'h264_nvenc', +]; + const hasEncoder = async ({ ffmpegPath, encoder, @@ -358,9 +372,9 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { } // Check if b frame variable is true. - if (encoder === 'hevc_nvenc' && inputs.enable_bframes === true) { + if (bframeSupport.includes(encoder) && inputs.bframes_enabled === true) { // If set to true then add b frames argument - extraArguments += '-bf 5 '; + extraArguments += `-bf ${inputs.bframes_value} `; } // Go through each stream in the file. From 599ae068a4a22ca0750b5a0cfef5bca63cbe34ba Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Tue, 27 Jun 2023 06:18:34 +0100 Subject: [PATCH 04/12] Adjust gpu inputs --- .../Tdarr_Plugin_00td_action_transcode.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Community/Tdarr_Plugin_00td_action_transcode.js b/Community/Tdarr_Plugin_00td_action_transcode.js index 5d11405..4dd9e2d 100644 --- a/Community/Tdarr_Plugin_00td_action_transcode.js +++ b/Community/Tdarr_Plugin_00td_action_transcode.js @@ -25,7 +25,19 @@ const details = () => ({ tooltip: 'Specify the codec to use', }, { - name: 'use_gpu', + name: 'target_bitrate_multiplier', + type: 'number', + defaultValue: 0.5, + inputUI: { + type: 'text', + }, + tooltip: ` + Specify the multiplier to use to calculate the target bitrate. + Default of 0.5 will roughly half the size of the file. + `, + }, + { + name: 'try_use_gpu', type: 'boolean', defaultValue: true, inputUI: { @@ -180,7 +192,7 @@ const getEncoder = async ({ otherArguments, }) => { let { encoder } = inputs; - if (inputs.use_gpu && (inputs.encoder === 'hevc' || inputs.encoder === 'h264')) { + if (inputs.try_use_gpu && (inputs.encoder === 'hevc' || inputs.encoder === 'h264')) { const gpuEncoders = [ { encoder: 'hevc_nvenc', @@ -293,7 +305,7 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { // Use the same calculation used for currentBitrate but divide it in half to get targetBitrate. // Logic of h265 can be half the bitrate as h264 without losing quality. - const targetBitrate = currentBitrate / 2; + const targetBitrate = currentBitrate * inputs.target_bitrate_multiplier; // Allow some leeway under and over the targetBitrate. const minimumBitrate = (targetBitrate * 0.7); From ffe427e2858da165267188821754a1c4003390cd Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Tue, 27 Jun 2023 07:02:46 +0100 Subject: [PATCH 05/12] Adjust inputs --- .../Tdarr_Plugin_00td_action_transcode.js | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/Community/Tdarr_Plugin_00td_action_transcode.js b/Community/Tdarr_Plugin_00td_action_transcode.js index 4dd9e2d..0d019bb 100644 --- a/Community/Tdarr_Plugin_00td_action_transcode.js +++ b/Community/Tdarr_Plugin_00td_action_transcode.js @@ -10,16 +10,16 @@ const details = () => ({ Tags: 'pre-processing,ffmpeg,video only,nvenc h265,configurable', Inputs: [ { - name: 'encoder', + name: 'target_codec', type: 'string', defaultValue: 'hevc', inputUI: { type: 'dropdown', options: [ 'hevc', - 'vp9', + // 'vp9', 'h264', - 'vp8', + // 'vp8', ], }, tooltip: 'Specify the codec to use', @@ -191,8 +191,8 @@ const getEncoder = async ({ inputs, otherArguments, }) => { - let { encoder } = inputs; - if (inputs.try_use_gpu && (inputs.encoder === 'hevc' || inputs.encoder === 'h264')) { + if (otherArguments.workerType.includes('gpu') + && inputs.try_use_gpu && (inputs.target_codec === 'hevc' || inputs.target_codec === 'h264')) { const gpuEncoders = [ { encoder: 'hevc_nvenc', @@ -229,7 +229,7 @@ const getEncoder = async ({ }, ]; - const filteredGpuEncoders = gpuEncoders.filter((device) => device.encoder.includes(inputs.encoder)); + const filteredGpuEncoders = gpuEncoders.filter((device) => device.encoder.includes(inputs.target_codec)); // eslint-disable-next-line no-restricted-syntax for (const gpuEncoder of filteredGpuEncoders) { @@ -243,11 +243,17 @@ const getEncoder = async ({ const enabledDevices = gpuEncoders.filter((device) => device.enabled === true); if (enabledDevices.length > 0) { - encoder = enabledDevices[0].encoder; + return enabledDevices[0].encoder; } } - return encoder; + if (inputs.target_codec === 'hevc') { + return 'libx265'; + } if (inputs.target_codec === 'h264') { + return 'libx264'; + } + + return ''; }; // eslint-disable-next-line no-unused-vars @@ -407,10 +413,10 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { // Check if codec of stream is hevc or vp9 AND check if file.container matches inputs.container. // If so nothing for plugin to do. if ( - inputs.encoder === file.ffProbeData.streams[i].codec_name + inputs.target_codec === file.ffProbeData.streams[i].codec_name && file.container === inputs.container ) { - response.infoLog += `File is already ${inputs.encoder} & in ${inputs.container}. \n`; + response.infoLog += `File is already ${inputs.target_codec} and in ${inputs.container}. \n`; return response; } // Check if codec of stream is hevc or vp9 @@ -418,11 +424,12 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { // If so remux file. if ( - inputs.encoder === file.ffProbeData.streams[i].codec_name + inputs.target_codec === file.ffProbeData.streams[i].codec_name && file.container !== inputs.container ) { - response.infoLog += `File is hevc or vp9 but is not in ${inputs.container} container. Remuxing. \n`; + response.infoLog += `File is in ${inputs.target_codec} but ` + + `is not in ${inputs.container} container. Remuxing. \n`; response.preset = ` -map 0 -c copy ${extraArguments}`; response.processFile = true; return response; @@ -430,7 +437,7 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { // Check if video stream is HDR or 10bit if ( - inputs.encoder === 'hevc' + inputs.target_codec === 'hevc' && (file.ffProbeData.streams[i].profile === 'High 10' || file.ffProbeData.streams[i].bits_per_raw_sample === '10') ) { @@ -453,7 +460,7 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { response.infoLog += `Minimum = ${minimumBitrate} \n`; response.infoLog += `Maximum = ${maximumBitrate} \n`; - if (encoder.contains('nvenc')) { + if (encoder.includes('nvenc')) { if (file.video_codec_name === 'h263') { response.preset = '-c:v h263_cuvid'; } else if (file.video_codec_name === 'h264' && CPU10 === false) { @@ -476,7 +483,7 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { response.preset += `${genpts} -map 0 -c copy -c:v ${encoder} -cq:v 19 ${bitrateSettings} ` + `-spatial_aq:v 1 -rc-lookahead:v 32 -max_muxing_queue_size 9999 ${extraArguments}`; response.processFile = true; - response.infoLog += 'File is not hevc or vp9. Transcoding. \n'; + response.infoLog += `File is not in ${inputs.target_codec}. Transcoding. \n`; return response; }; module.exports.details = details; From 89fe92a8cca78b652f1ac375c5e94a02c7fc6caf Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Tue, 27 Jun 2023 07:02:58 +0100 Subject: [PATCH 06/12] Add tests --- .../Tdarr_Plugin_00td_action_transcode.js | 142 ++++++++++++++++++ tests/helpers/run.js | 4 +- 2 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 tests/Community/Tdarr_Plugin_00td_action_transcode.js diff --git a/tests/Community/Tdarr_Plugin_00td_action_transcode.js b/tests/Community/Tdarr_Plugin_00td_action_transcode.js new file mode 100644 index 0000000..a9776cb --- /dev/null +++ b/tests/Community/Tdarr_Plugin_00td_action_transcode.js @@ -0,0 +1,142 @@ +/* eslint max-len: 0 */ +const _ = require('lodash'); +const run = require('../helpers/run'); + +const tests = [ + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH264_1.json')), + librarySettings: {}, + inputs: {}, + otherArguments: {}, + }, + output: { + processFile: true, + preset: ' -map 0 -c copy -c:v libx265 -cq:v 19 -b:v 795571.5361445782 -minrate 556900.0753012047 -maxrate 1034242.9969879518 -bufsize 1591143.0722891565 -spatial_aq:v 1 -rc-lookahead:v 32 -max_muxing_queue_size 9999 ', + handBrakeMode: false, + FFmpegMode: true, + reQueueAfter: true, + infoLog: 'Container for output selected as mkv. \n' + + 'Current bitrate = 1591143.0722891565 \n' + + 'Bitrate settings: \n' + + 'Target = 795571.5361445782 \n' + + 'Minimum = 556900.0753012047 \n' + + 'Maximum = 1034242.9969879518 \n' + + 'File is not in hevc. Transcoding. \n', + container: '.mkv', + }, + }, + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH265_1.json')), + librarySettings: {}, + inputs: {}, + otherArguments: {}, + }, + output: { + processFile: false, + preset: '', + handBrakeMode: false, + FFmpegMode: true, + reQueueAfter: true, + infoLog: 'File is already hevc and in mkv. \n', + container: '.mkv', + }, + }, + + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH264_1.json')), + librarySettings: {}, + inputs: { + target_codec: 'hevc', + target_bitrate_multiplier: 0.75, + try_use_gpu: true, + container: 'mkv', + bitrate_cutoff: 0, + + enable_10bit: false, + bframes_enabled: false, + bframes_value: 5, + force_conform: false, + + }, + otherArguments: {}, + }, + output: { + processFile: true, + preset: ' -map 0 -c copy -c:v libx265 -cq:v 19 -b:v 1193357.3042168673 -minrate 835350.1129518071 -maxrate 1551364.4954819276 -bufsize 1591143.0722891565 -spatial_aq:v 1 -rc-lookahead:v 32 -max_muxing_queue_size 9999 ', + handBrakeMode: false, + FFmpegMode: true, + reQueueAfter: true, + infoLog: 'Container for output selected as mkv. \n' + + 'Current bitrate = 1591143.0722891565 \n' + + 'Bitrate settings: \n' + + 'Target = 1193357.3042168673 \n' + + 'Minimum = 835350.1129518071 \n' + + 'Maximum = 1551364.4954819276 \n' + + 'File is not in hevc. Transcoding. \n', + container: '.mkv', + }, + }, + + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH264_1.json')), + librarySettings: {}, + inputs: { + target_codec: 'h264', + target_bitrate_multiplier: 0.75, + try_use_gpu: true, + container: 'mkv', + bitrate_cutoff: 0, + enable_10bit: false, + bframes_enabled: false, + bframes_value: 5, + force_conform: false, + }, + otherArguments: {}, + }, + output: { + processFile: true, + preset: ' -map 0 -c copy ', + handBrakeMode: false, + FFmpegMode: true, + reQueueAfter: true, + infoLog: 'File is in h264 but is not in mkv container. Remuxing. \n', + container: '.mkv', + }, + }, + + { + input: { + file: _.cloneDeep(require('../sampleData/media/sampleH264_1.json')), + librarySettings: {}, + inputs: { + target_codec: 'hevc', + target_bitrate_multiplier: 0.75, + try_use_gpu: true, + container: 'mkv', + bitrate_cutoff: 10000000, + + enable_10bit: false, + bframes_enabled: false, + bframes_value: 5, + force_conform: false, + + }, + otherArguments: {}, + }, + output: { + processFile: false, + preset: '', + handBrakeMode: false, + FFmpegMode: true, + reQueueAfter: true, + infoLog: 'Current bitrate is below set cutoff of 10000000. Cancelling plugin. \n', + container: '.mkv', + }, + }, +]; + +run(tests); diff --git a/tests/helpers/run.js b/tests/helpers/run.js index ff1dabf..012fcfb 100644 --- a/tests/helpers/run.js +++ b/tests/helpers/run.js @@ -46,7 +46,7 @@ const run = async (tests) => { testOutput = test.outputModify(testOutput); } - if (test.error && test.error.shouldThrow) { + if (test?.error?.shouldThrow) { if (errorEncountered !== false) { // eslint-disable-next-line no-console console.log(errorEncountered); @@ -54,6 +54,8 @@ const run = async (tests) => { } else { throw new Error('Expected plugin error but none was thrown!'); } + } else if (!test?.error?.shouldThrow && errorEncountered !== false) { + throw new Error(`Unexpected plugin error!${errorEncountered}`); } else { chai.assert.deepEqual(testOutput, expectedOutput); } From fe8864fcc0da5888e90d32b739799be76c5d309f Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Tue, 27 Jun 2023 07:09:13 +0100 Subject: [PATCH 07/12] Add other args check --- Community/Tdarr_Plugin_00td_action_transcode.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Community/Tdarr_Plugin_00td_action_transcode.js b/Community/Tdarr_Plugin_00td_action_transcode.js index 0d019bb..a831b40 100644 --- a/Community/Tdarr_Plugin_00td_action_transcode.js +++ b/Community/Tdarr_Plugin_00td_action_transcode.js @@ -191,7 +191,9 @@ const getEncoder = async ({ inputs, otherArguments, }) => { - if (otherArguments.workerType.includes('gpu') + if ( + otherArguments.workerType + && otherArguments.workerType.includes('gpu') && inputs.try_use_gpu && (inputs.target_codec === 'hevc' || inputs.target_codec === 'h264')) { const gpuEncoders = [ { From 911a388d217ebe2520a38d25258acf90070620a3 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Tue, 4 Jul 2023 18:24:56 +0100 Subject: [PATCH 08/12] Auto detect nvenc gpu with least utilisation --- .../Tdarr_Plugin_00td_action_transcode.js | 125 ++++++++++++++++-- 1 file changed, 115 insertions(+), 10 deletions(-) diff --git a/Community/Tdarr_Plugin_00td_action_transcode.js b/Community/Tdarr_Plugin_00td_action_transcode.js index a831b40..b07f90a 100644 --- a/Community/Tdarr_Plugin_00td_action_transcode.js +++ b/Community/Tdarr_Plugin_00td_action_transcode.js @@ -151,6 +151,25 @@ const details = () => ({ \\nExample:\\n false`, }, + { + name: 'exclude_gpus', + type: 'string', + defaultValue: '', + inputUI: { + type: 'text', + }, + tooltip: `Specify the id(s) of any GPUs that needs to be excluded from assigning transcoding tasks. + \\n Seperate with a comma (,). Leave empty to disable. + \\n Get GPU numbers in the node by running 'nvidia-smi' + \\nExample:\\n + 0,1,3,8 + + \\nExample:\\n + 3 + + \\nExample:\\n + 0`, + }, ], }); @@ -162,15 +181,18 @@ const bframeSupport = [ const hasEncoder = async ({ ffmpegPath, encoder, + inputArgs, }) => { const { exec } = require('child_process'); let isEnabled = false; try { isEnabled = await new Promise((resolve) => { - exec(`${ffmpegPath} -f lavfi -i color=c=black:s=256x256:d=1:r=30 -c:v ${encoder} -f null /dev/null`, ( + const command = `${ffmpegPath} ${inputArgs || ''} -f lavfi -i color=c=black:s=256x256:d=1:r=30` + + ` -c:v ${encoder} -f null /dev/null`; + exec(command, ( error, // stdout, - // stderr + // stderr, ) => { if (error) { resolve(false); @@ -187,7 +209,66 @@ const hasEncoder = async ({ return isEnabled; }; +// credit to UNCode101 for this +const getBestNvencDevice = async ({ + response, + inputs, + nvencDevice, +}) => { + const { execSync } = require('child_process'); + let gpu_num = -1; + let gpu_util = 100000; + let result_util = 0; + let gpu_count = -1; + let gpu_names = ''; + const gpus_to_exclude = inputs.exclude_gpus === '' ? [] : inputs.exclude_gpus.split(',').map(Number); + try { + gpu_names = execSync('nvidia-smi --query-gpu=name --format=csv,noheader'); + gpu_names = gpu_names.toString().trim(); + gpu_names = gpu_names.split(/\r?\n/); + /* When nvidia-smi returns an error it contains 'nvidia-smi' in the error + Example: Linux: nvidia-smi: command not found + Windows: 'nvidia-smi' is not recognized as an internal or external command, + operable program or batch file. */ + if (!gpu_names[0].includes('nvidia-smi')) { + gpu_count = gpu_names.length; + } + } catch (error) { + response.infoLog += 'Error in reading nvidia-smi output! \n'; + // response.infoLog += error.message; + } + if (gpu_count > 0) { + for (let gpui = 0; gpui < gpu_count; gpui++) { + // Check if GPU # is in GPUs to exclude + if (gpus_to_exclude.includes(gpui)) { + response.infoLog += `GPU ${gpui}: ${gpu_names[gpui]} is in exclusion list, will not be used!\n`; + } else { + try { + const cmd_gpu = `nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader,nounits -i ${gpui}`; + result_util = parseInt(execSync(cmd_gpu), 10); + if (!Number.isNaN(result_util)) { // != "No devices were found") { + response.infoLog += `GPU ${gpui} : Utilization ${result_util}%\n`; + if (result_util < gpu_util) { + gpu_num = gpui; + gpu_util = result_util; + } + } + } catch (error) { + response.infoLog += `Error in reading GPU ${gpui} Utilization\nError: ${error}\n`; + } + } + } + } + if (gpu_num >= 0) { + // eslint-disable-next-line no-param-reassign + nvencDevice.inputArgs = `-hwaccel_device ${gpu_num}`; + } + + return nvencDevice; +}; + const getEncoder = async ({ + response, inputs, otherArguments, }) => { @@ -208,6 +289,11 @@ const getEncoder = async ({ encoder: 'hevc_qsv', enabled: false, }, + { + encoder: 'hevc_vaapi', + inputArgs: '-hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi', + enabled: false, + }, { encoder: 'hevc_videotoolbox', enabled: false, @@ -239,23 +325,40 @@ const getEncoder = async ({ gpuEncoder.enabled = await hasEncoder({ ffmpegPath: otherArguments.ffmpegPath, encoder: gpuEncoder.encoder, + inputArgs: gpuEncoder.inputArgs, }); } const enabledDevices = gpuEncoders.filter((device) => device.enabled === true); if (enabledDevices.length > 0) { - return enabledDevices[0].encoder; + if (enabledDevices[0].encoder.includes('nvenc')) { + return getBestNvencDevice({ + response, + inputs, + nvencDevice: enabledDevices[0], + }); + } + return enabledDevices[0]; } } if (inputs.target_codec === 'hevc') { - return 'libx265'; + return { + encoder: 'libx265', + inputArgs: '', + }; } if (inputs.target_codec === 'h264') { - return 'libx264'; + return { + encoder: 'libx264', + inputArgs: '', + }; } - return ''; + return { + encoder: '', + inputArgs: '', + }; }; // eslint-disable-next-line no-unused-vars @@ -272,7 +375,8 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { infoLog: '', }; - const encoder = await getEncoder({ + const encoderProperties = await getEncoder({ + response, inputs, otherArguments, }); @@ -392,7 +496,7 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { } // Check if b frame variable is true. - if (bframeSupport.includes(encoder) && inputs.bframes_enabled === true) { + if (bframeSupport.includes(encoderProperties.encoder) && inputs.bframes_enabled === true) { // If set to true then add b frames argument extraArguments += `-bf ${inputs.bframes_value} `; } @@ -462,7 +566,7 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { response.infoLog += `Minimum = ${minimumBitrate} \n`; response.infoLog += `Maximum = ${maximumBitrate} \n`; - if (encoder.includes('nvenc')) { + if (encoderProperties.encoder.includes('nvenc')) { if (file.video_codec_name === 'h263') { response.preset = '-c:v h263_cuvid'; } else if (file.video_codec_name === 'h264' && CPU10 === false) { @@ -482,7 +586,8 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { } } - response.preset += `${genpts} -map 0 -c copy -c:v ${encoder} -cq:v 19 ${bitrateSettings} ` + response.preset += ` ${encoderProperties.inputArgs ? encoderProperties.inputArgs : ''} ${genpts}` + + ` -map 0 -c copy -c:v ${encoderProperties.encoder} -cq:v 19 ${bitrateSettings} ` + `-spatial_aq:v 1 -rc-lookahead:v 32 -max_muxing_queue_size 9999 ${extraArguments}`; response.processFile = true; response.infoLog += `File is not in ${inputs.target_codec}. Transcoding. \n`; From 40d25c56f9a25b504ed3b34bc9729440735499ea Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Tue, 4 Jul 2023 18:44:45 +0100 Subject: [PATCH 09/12] Update tests --- .../Tdarr_Plugin_00td_action_transcode.js | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/Community/Tdarr_Plugin_00td_action_transcode.js b/tests/Community/Tdarr_Plugin_00td_action_transcode.js index a9776cb..728a454 100644 --- a/tests/Community/Tdarr_Plugin_00td_action_transcode.js +++ b/tests/Community/Tdarr_Plugin_00td_action_transcode.js @@ -12,18 +12,18 @@ const tests = [ }, output: { processFile: true, - preset: ' -map 0 -c copy -c:v libx265 -cq:v 19 -b:v 795571.5361445782 -minrate 556900.0753012047 -maxrate 1034242.9969879518 -bufsize 1591143.0722891565 -spatial_aq:v 1 -rc-lookahead:v 32 -max_muxing_queue_size 9999 ', + preset: ' -map 0 -c copy -c:v libx265 -cq:v 19 -b:v 795571.5361445782 -minrate 556900.0753012047 -maxrate 1034242.9969879518 -bufsize 1591143.0722891565 -spatial_aq:v 1 -rc-lookahead:v 32 -max_muxing_queue_size 9999 ', handBrakeMode: false, FFmpegMode: true, reQueueAfter: true, - infoLog: 'Container for output selected as mkv. \n' - + 'Current bitrate = 1591143.0722891565 \n' - + 'Bitrate settings: \n' - + 'Target = 795571.5361445782 \n' - + 'Minimum = 556900.0753012047 \n' - + 'Maximum = 1034242.9969879518 \n' - + 'File is not in hevc. Transcoding. \n', - container: '.mkv', + infoLog: 'Container for output selected as mkv. \n' + + 'Current bitrate = 1591143.0722891565 \n' + + 'Bitrate settings: \n' + + 'Target = 795571.5361445782 \n' + + 'Minimum = 556900.0753012047 \n' + + 'Maximum = 1034242.9969879518 \n' + + 'File is not in hevc. Transcoding. \n', + container: '.mkv' }, }, { @@ -65,18 +65,18 @@ const tests = [ }, output: { processFile: true, - preset: ' -map 0 -c copy -c:v libx265 -cq:v 19 -b:v 1193357.3042168673 -minrate 835350.1129518071 -maxrate 1551364.4954819276 -bufsize 1591143.0722891565 -spatial_aq:v 1 -rc-lookahead:v 32 -max_muxing_queue_size 9999 ', + preset: ' -map 0 -c copy -c:v libx265 -cq:v 19 -b:v 1193357.3042168673 -minrate 835350.1129518071 -maxrate 1551364.4954819276 -bufsize 1591143.0722891565 -spatial_aq:v 1 -rc-lookahead:v 32 -max_muxing_queue_size 9999 ', handBrakeMode: false, FFmpegMode: true, reQueueAfter: true, - infoLog: 'Container for output selected as mkv. \n' - + 'Current bitrate = 1591143.0722891565 \n' - + 'Bitrate settings: \n' - + 'Target = 1193357.3042168673 \n' - + 'Minimum = 835350.1129518071 \n' - + 'Maximum = 1551364.4954819276 \n' - + 'File is not in hevc. Transcoding. \n', - container: '.mkv', + infoLog: 'Container for output selected as mkv. \n' + + 'Current bitrate = 1591143.0722891565 \n' + + 'Bitrate settings: \n' + + 'Target = 1193357.3042168673 \n' + + 'Minimum = 835350.1129518071 \n' + + 'Maximum = 1551364.4954819276 \n' + + 'File is not in hevc. Transcoding. \n', + container: '.mkv' }, }, From 0181b627e1c769d6de760ee24364e698a8e0bb72 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Tue, 4 Jul 2023 18:59:10 +0100 Subject: [PATCH 10/12] Use node 18 --- .github/workflows/lint_and_test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint_and_test.yml b/.github/workflows/lint_and_test.yml index c85eb73..3d80d30 100644 --- a/.github/workflows/lint_and_test.yml +++ b/.github/workflows/lint_and_test.yml @@ -8,7 +8,7 @@ jobs: build: strategy: matrix: - node-version: [16.x] + node-version: [18.x] os: [ ["ubuntu-20.04"], From ad7eb13005b83e5a23b6d6eb96232979eb2eca90 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Tue, 4 Jul 2023 18:59:17 +0100 Subject: [PATCH 11/12] Fix lint issues --- .../Tdarr_Plugin_00td_action_transcode.js | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/tests/Community/Tdarr_Plugin_00td_action_transcode.js b/tests/Community/Tdarr_Plugin_00td_action_transcode.js index 728a454..bb2ded6 100644 --- a/tests/Community/Tdarr_Plugin_00td_action_transcode.js +++ b/tests/Community/Tdarr_Plugin_00td_action_transcode.js @@ -16,14 +16,14 @@ const tests = [ handBrakeMode: false, FFmpegMode: true, reQueueAfter: true, - infoLog: 'Container for output selected as mkv. \n' + - 'Current bitrate = 1591143.0722891565 \n' + - 'Bitrate settings: \n' + - 'Target = 795571.5361445782 \n' + - 'Minimum = 556900.0753012047 \n' + - 'Maximum = 1034242.9969879518 \n' + - 'File is not in hevc. Transcoding. \n', - container: '.mkv' + infoLog: 'Container for output selected as mkv. \n' + + 'Current bitrate = 1591143.0722891565 \n' + + 'Bitrate settings: \n' + + 'Target = 795571.5361445782 \n' + + 'Minimum = 556900.0753012047 \n' + + 'Maximum = 1034242.9969879518 \n' + + 'File is not in hevc. Transcoding. \n', + container: '.mkv', }, }, { @@ -69,14 +69,14 @@ const tests = [ handBrakeMode: false, FFmpegMode: true, reQueueAfter: true, - infoLog: 'Container for output selected as mkv. \n' + - 'Current bitrate = 1591143.0722891565 \n' + - 'Bitrate settings: \n' + - 'Target = 1193357.3042168673 \n' + - 'Minimum = 835350.1129518071 \n' + - 'Maximum = 1551364.4954819276 \n' + - 'File is not in hevc. Transcoding. \n', - container: '.mkv' + infoLog: 'Container for output selected as mkv. \n' + + 'Current bitrate = 1591143.0722891565 \n' + + 'Bitrate settings: \n' + + 'Target = 1193357.3042168673 \n' + + 'Minimum = 835350.1129518071 \n' + + 'Maximum = 1551364.4954819276 \n' + + 'File is not in hevc. Transcoding. \n', + container: '.mkv', }, }, From 735947ee045dbedd7c5195457ddd88782ca553b3 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Tue, 8 Aug 2023 07:18:24 +0100 Subject: [PATCH 12/12] Set gpu device --- .../Tdarr_Plugin_00td_action_transcode.js | 18 +++++++++++++----- .../Tdarr_Plugin_00td_action_transcode.js | 4 ++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Community/Tdarr_Plugin_00td_action_transcode.js b/Community/Tdarr_Plugin_00td_action_transcode.js index b07f90a..77b5230 100644 --- a/Community/Tdarr_Plugin_00td_action_transcode.js +++ b/Community/Tdarr_Plugin_00td_action_transcode.js @@ -217,7 +217,7 @@ const getBestNvencDevice = async ({ }) => { const { execSync } = require('child_process'); let gpu_num = -1; - let gpu_util = 100000; + let lowest_gpu_util = 100000; let result_util = 0; let gpu_count = -1; let gpu_names = ''; @@ -237,6 +237,7 @@ const getBestNvencDevice = async ({ response.infoLog += 'Error in reading nvidia-smi output! \n'; // response.infoLog += error.message; } + if (gpu_count > 0) { for (let gpui = 0; gpui < gpu_count; gpui++) { // Check if GPU # is in GPUs to exclude @@ -248,9 +249,10 @@ const getBestNvencDevice = async ({ result_util = parseInt(execSync(cmd_gpu), 10); if (!Number.isNaN(result_util)) { // != "No devices were found") { response.infoLog += `GPU ${gpui} : Utilization ${result_util}%\n`; - if (result_util < gpu_util) { + + if (result_util < lowest_gpu_util) { gpu_num = gpui; - gpu_util = result_util; + lowest_gpu_util = result_util; } } } catch (error) { @@ -262,6 +264,8 @@ const getBestNvencDevice = async ({ if (gpu_num >= 0) { // eslint-disable-next-line no-param-reassign nvencDevice.inputArgs = `-hwaccel_device ${gpu_num}`; + // eslint-disable-next-line no-param-reassign + nvencDevice.outputArgs = `-gpu ${gpu_num}`; } return nvencDevice; @@ -586,9 +590,13 @@ const plugin = async (file, librarySettings, inputs, otherArguments) => { } } + const vEncode = `-cq:v 19 ${bitrateSettings}`; + response.preset += ` ${encoderProperties.inputArgs ? encoderProperties.inputArgs : ''} ${genpts}` - + ` -map 0 -c copy -c:v ${encoderProperties.encoder} -cq:v 19 ${bitrateSettings} ` - + `-spatial_aq:v 1 -rc-lookahead:v 32 -max_muxing_queue_size 9999 ${extraArguments}`; + + ` -map 0 -c copy -c:v ${encoderProperties.encoder}` + + ` ${encoderProperties.outputArgs ? encoderProperties.outputArgs : ''}` + + ` ${vEncode}` + + ` -spatial_aq:v 1 -rc-lookahead:v 32 -max_muxing_queue_size 9999 ${extraArguments}`; response.processFile = true; response.infoLog += `File is not in ${inputs.target_codec}. Transcoding. \n`; return response; diff --git a/tests/Community/Tdarr_Plugin_00td_action_transcode.js b/tests/Community/Tdarr_Plugin_00td_action_transcode.js index bb2ded6..5055986 100644 --- a/tests/Community/Tdarr_Plugin_00td_action_transcode.js +++ b/tests/Community/Tdarr_Plugin_00td_action_transcode.js @@ -12,7 +12,7 @@ const tests = [ }, output: { processFile: true, - preset: ' -map 0 -c copy -c:v libx265 -cq:v 19 -b:v 795571.5361445782 -minrate 556900.0753012047 -maxrate 1034242.9969879518 -bufsize 1591143.0722891565 -spatial_aq:v 1 -rc-lookahead:v 32 -max_muxing_queue_size 9999 ', + preset: ' -map 0 -c copy -c:v libx265 -cq:v 19 -b:v 795571.5361445782 -minrate 556900.0753012047 -maxrate 1034242.9969879518 -bufsize 1591143.0722891565 -spatial_aq:v 1 -rc-lookahead:v 32 -max_muxing_queue_size 9999 ', handBrakeMode: false, FFmpegMode: true, reQueueAfter: true, @@ -65,7 +65,7 @@ const tests = [ }, output: { processFile: true, - preset: ' -map 0 -c copy -c:v libx265 -cq:v 19 -b:v 1193357.3042168673 -minrate 835350.1129518071 -maxrate 1551364.4954819276 -bufsize 1591143.0722891565 -spatial_aq:v 1 -rc-lookahead:v 32 -max_muxing_queue_size 9999 ', + preset: ' -map 0 -c copy -c:v libx265 -cq:v 19 -b:v 1193357.3042168673 -minrate 835350.1129518071 -maxrate 1551364.4954819276 -bufsize 1591143.0722891565 -spatial_aq:v 1 -rc-lookahead:v 32 -max_muxing_queue_size 9999 ', handBrakeMode: false, FFmpegMode: true, reQueueAfter: true,