From a344ca8d4abb8c31b448ff1eb5eaa7a621775e43 Mon Sep 17 00:00:00 2001 From: Stefan Date: Sat, 1 Jul 2023 14:36:22 +0200 Subject: [PATCH 01/11] Renames files based on codec Renames based on codec by reading the codec from thes file. This is done for Video and audio and taken from the first video or audio stream. it also takes care of any addtional files in the folder so they wont gget deleted by Radarr or Sonarr, after the transcode cause of not matching file names. --- ...arr_Plugin_rename_based_on_codec_schadi.js | 179 ++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 Community/Tdarr_Plugin_rename_based_on_codec_schadi.js diff --git a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js new file mode 100644 index 0000000..e5b7af7 --- /dev/null +++ b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js @@ -0,0 +1,179 @@ +const loadDefaultValues = require('../methods/loadDefaultValues'); +const fs = require('fs'); + +const details = () => { + return { + id: "Tdarr_Plugin_rename_based_on_codec_schadi", + Stage: "Post-processing", + Name: "Rename based on codec Video and Audio", + Type: "Video_Audio", + Operation: "Transcode", + Description: `[Contains built-in filter] If the filename contains '264' or '265' and also Audio, this plugin renames 264 files to 265 or vice versa depending on the codec. It also takes care off addiotnal files like deffined in the input Option. \n\n`, + Version: "1.00", + Tags: "post-processing", + Inputs:[ + { + name: 'rename_audio', + type: 'boolean', + defaultValue: false, + inputUI: { + type: 'dropdown', + options: [ + 'false', + 'true', + ], + }, + tooltip: `Will Rename According to Audio Codec after x264 or x265. + \\nExample:\\n + true + \\nExample:\\n + false`, + }, + { + name: 'rename_video', + type: 'boolean', + defaultValue: false, + inputUI: { + type: 'dropdown', + options: [ + 'false', + 'true', + ], + }, + tooltip: `Will Rename According to Video Codec after x264 or x265. + \\nExample:\\n + true + \\nExample:\\n + false`, + }, + { + name: 'additional_extensions', + type: 'text', + defaultValue: '.nfo,.srt', + tooltip: `Additional file extensions to rename (comma-separated). + \\nExample:\\n + .nfo,.srt`, + }, + ], + }; +}; + +const plugin = (file, librarySettings, inputs, otherArguments) => { + inputs = loadDefaultValues(inputs, details); + const fileNameOld = file._id; + + const codecMap = { + 'aac': 'AAC', + 'ac3': 'AC3', + 'av1': 'AV1', + 'avc': 'h264', + 'dts': 'DTS', + 'eac3': 'EAC3', + 'flac': 'FLAC', + 'hevc': 'h265', + 'mp2': 'MP2', + 'mp3': 'MP3', + 'mpeg2': 'MPEG2', + 'truehd': 'TrueHD', + 'x264': 'h264', + 'x265': 'h265', + 'h264': 'h264', + 'h265': 'h265', + 'dts': 'DTS-X', + 'dts-hd ma': 'DTS-HD MA', + 'dts-es': 'DTS-HD ES', + 'dts-hd hra': 'DTS-HD HRA', + 'dts express ': 'DTS Express', + 'dts 96/24': 'DTS', + }; + + var firstVideoStreamCodec; + var firstAudioStreamCodec; + + const videoStream = file.ffProbeData.streams.find((stream) => stream.codec_type === 'video'); + + if (videoStream && inputs.rename_video) { + const videoCodec = videoStream.codec_name.toLowerCase(); + firstVideoStreamCodec = videoCodec; + + if (videoCodec in codecMap) { + const renamedCodec = codecMap[videoCodec]; + file._id = file._id.replace(/(h264|h265|x264|x265|avc|hevc|mpeg2|av1)/gi, renamedCodec); + file.file = file.file.replace(/(h264|h265|x264|x265|avc|hevc|mpeg2|av1)/gi, renamedCodec); + } + } + + const audioStream = file.ffProbeData.streams.find((stream) => stream.codec_type === 'audio'); + + if (audioStream && inputs.rename_audio) { + const audioCodec = audioStream.codec_name.toLowerCase(); + firstAudioStreamCodec = audioCodec; + + if (audioCodec in codecMap) { + const renamedCodec = codecMap[audioCodec]; + file._id = file._id.replace(/(aac|ac3|eac3|flac|mp2|mp3|truehd|dts[-. ]hd[-. ]ma|dts[-. ]hd[-. ]es|dts[-. ]hd[-. ]hra|dts[-. ]express|dts)/gi, renamedCodec); + file.file = file.file.replace(/(aac|ac3|eac3|flac|mp2|mp3|truehd|dts[-. ]hd[-. ]ma|dts[-. ]hd[-. ]es|dts[-. ]hd[-. ]hra|dts[-. ]express|dts)/gi, renamedCodec); + } + } + + var filename = fileNameOld.replace(/^.*[\\\/]/, ''); + var JustName = filename.split('.'); + JustName.pop(); + JustName = JustName.join('.').toString(); + + const fileDir = fileNameOld.split('/'); + fileDir.pop(); + const directoryPath = fileDir.join('/'); + + const additionalExtensions = inputs.additional_extensions + .split(/[,;/]/) + .map(extension => extension.trim()) + .filter(extension => extension !== ''); + + additionalExtensions.forEach(extension => { + const regex = new RegExp(`${JustName}.*${extension}`, 'i'); + const files = fs.readdirSync(directoryPath); + + files.forEach((supportFile) => { + if (regex.test(supportFile)) { + const renamedFile = supportFile.replace(regex, (match, p1, p2) => { + const additionalFileName = match; + const renamedVideoCodec = codecMap[firstVideoStreamCodec]; + const renamedAudioCodec = codecMap[firstAudioStreamCodec]; + + const renamedFileWithCodecs = additionalFileName + .replace(/(h264|h265|x264|x265|avc|hevc|mpeg2|av1)/gi, renamedVideoCodec) + .replace(/(aac|ac3|eac3|flac|mp2|mp3|truehd|dts[-. ]hd[-. ]ma|dts[-. ]hd[-. ]es|dts[-. ]hd[-. ]hra|dts[-. ]express|dts)/gi, renamedAudioCodec); + + return renamedFileWithCodecs; + }); + + fs.renameSync(`${directoryPath}/${supportFile}`, `${directoryPath}/${renamedFile}`, { + overwrite: true, + }); + } + }); + }); + + if (fileNameOld !== file._id) { + fs.renameSync(fileNameOld, file._id, { + overwrite: true, + }); + + const response = { + file, + removeFromDB: false, + updateDB: true, + }; + + return response; + } else { + return { + processFile: true, + infoLog: `Renamed file and associated files to: ${file._id}`, + }; + } +}; + +module.exports.details = details; +module.exports.plugin = plugin; From 1db09504c6bafe409a56cde457e38bd5bc6da539 Mon Sep 17 00:00:00 2001 From: Stefan Date: Sat, 1 Jul 2023 14:42:18 +0200 Subject: [PATCH 02/11] Update Tdarr_Plugin_rename_based_on_codec_schadi.js --- .../Tdarr_Plugin_rename_based_on_codec_schadi.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js index e5b7af7..6fd2922 100644 --- a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js +++ b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js @@ -90,6 +90,9 @@ const plugin = (file, librarySettings, inputs, otherArguments) => { var firstVideoStreamCodec; var firstAudioStreamCodec; + const videoCodecRegex = /(h264|h265|x264|x265|avc|hevc|mpeg2|av1)/gi; + const audioCodecRegex = /(aac|ac3|eac3|flac|mp2|mp3|truehd|dts[-. ]hd[-. ]ma|dts[-. ]hd[-. ]es|dts[-. ]hd[-. ]hra|dts[-. ]express|dts)/gi; + const videoStream = file.ffProbeData.streams.find((stream) => stream.codec_type === 'video'); if (videoStream && inputs.rename_video) { @@ -98,8 +101,8 @@ const plugin = (file, librarySettings, inputs, otherArguments) => { if (videoCodec in codecMap) { const renamedCodec = codecMap[videoCodec]; - file._id = file._id.replace(/(h264|h265|x264|x265|avc|hevc|mpeg2|av1)/gi, renamedCodec); - file.file = file.file.replace(/(h264|h265|x264|x265|avc|hevc|mpeg2|av1)/gi, renamedCodec); + file._id = file._id.replace(videoCodecRegex, renamedCodec); + file.file = file.file.replace(videoCodecRegex, renamedCodec); } } @@ -111,8 +114,8 @@ const plugin = (file, librarySettings, inputs, otherArguments) => { if (audioCodec in codecMap) { const renamedCodec = codecMap[audioCodec]; - file._id = file._id.replace(/(aac|ac3|eac3|flac|mp2|mp3|truehd|dts[-. ]hd[-. ]ma|dts[-. ]hd[-. ]es|dts[-. ]hd[-. ]hra|dts[-. ]express|dts)/gi, renamedCodec); - file.file = file.file.replace(/(aac|ac3|eac3|flac|mp2|mp3|truehd|dts[-. ]hd[-. ]ma|dts[-. ]hd[-. ]es|dts[-. ]hd[-. ]hra|dts[-. ]express|dts)/gi, renamedCodec); + file._id = file._id.replace(audioCodecRegex, renamedCodec); + file.file = file.file.replace(audioCodecRegex, renamedCodec); } } @@ -142,8 +145,8 @@ const plugin = (file, librarySettings, inputs, otherArguments) => { const renamedAudioCodec = codecMap[firstAudioStreamCodec]; const renamedFileWithCodecs = additionalFileName - .replace(/(h264|h265|x264|x265|avc|hevc|mpeg2|av1)/gi, renamedVideoCodec) - .replace(/(aac|ac3|eac3|flac|mp2|mp3|truehd|dts[-. ]hd[-. ]ma|dts[-. ]hd[-. ]es|dts[-. ]hd[-. ]hra|dts[-. ]express|dts)/gi, renamedAudioCodec); + .replace(videoCodecRegex, renamedVideoCodec) + .replace(audioCodecRegex, renamedAudioCodec); return renamedFileWithCodecs; }); From 06f4a60e983c1e3b17312a36fb96a36099353f37 Mon Sep 17 00:00:00 2001 From: Stefan Date: Sat, 1 Jul 2023 14:48:19 +0200 Subject: [PATCH 03/11] Update Tdarr_Plugin_rename_based_on_codec_schadi.js --- Community/Tdarr_Plugin_rename_based_on_codec_schadi.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js index 6fd2922..89b314c 100644 --- a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js +++ b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js @@ -8,7 +8,7 @@ const details = () => { Name: "Rename based on codec Video and Audio", Type: "Video_Audio", Operation: "Transcode", - Description: `[Contains built-in filter] If the filename contains '264' or '265' and also Audio, this plugin renames 264 files to 265 or vice versa depending on the codec. It also takes care off addiotnal files like deffined in the input Option. \n\n`, + Description: `[Contains built-in filter] If the filename contains '264' or '265' or some other Codecs and also Audio codecs, this plugin renames 264 files to 265 or vice versa depending on the codec same for Audio. It also takes care off addiotnal files like deffined in the input Option. \n\n`, Version: "1.00", Tags: "post-processing", Inputs:[ From 35710816aeae9f198b5ea8b42e00658d93abb688 Mon Sep 17 00:00:00 2001 From: Stefan Date: Sat, 1 Jul 2023 14:54:42 +0200 Subject: [PATCH 04/11] Update Tdarr_Plugin_rename_based_on_codec_schadi.js --- Community/Tdarr_Plugin_rename_based_on_codec_schadi.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js index 89b314c..2652c9f 100644 --- a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js +++ b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js @@ -8,7 +8,10 @@ const details = () => { Name: "Rename based on codec Video and Audio", Type: "Video_Audio", Operation: "Transcode", - Description: `[Contains built-in filter] If the filename contains '264' or '265' or some other Codecs and also Audio codecs, this plugin renames 264 files to 265 or vice versa depending on the codec same for Audio. It also takes care off addiotnal files like deffined in the input Option. \n\n`, + Description: ` + If the filename contains a codec information like h264, av1 or similar for video and AC3, AAC or trueHD \n\n + the plugin will read the codec info from the file and rename it accordingly. \n\n + It also takes care off addiotnal files deffined in the input Option. \n\n`, Version: "1.00", Tags: "post-processing", Inputs:[ From a0273d34176278ba06f0e2235e99c4ccbd6cff82 Mon Sep 17 00:00:00 2001 From: Stefan Date: Mon, 10 Jul 2023 21:27:54 +0200 Subject: [PATCH 05/11] Update Tdarr_Plugin_rename_based_on_codec_schadi.js fixed additonal file handling --- ...arr_Plugin_rename_based_on_codec_schadi.js | 119 ++++++++++-------- 1 file changed, 69 insertions(+), 50 deletions(-) diff --git a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js index 2652c9f..0d25dd1 100644 --- a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js +++ b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js @@ -1,5 +1,6 @@ const loadDefaultValues = require('../methods/loadDefaultValues'); const fs = require('fs'); +const path = require('path'); const details = () => { return { @@ -53,17 +54,30 @@ const details = () => { name: 'additional_extensions', type: 'text', defaultValue: '.nfo,.srt', + inputUI: { + type: 'text', + }, tooltip: `Additional file extensions to rename (comma-separated). \\nExample:\\n .nfo,.srt`, }, - ], + ], }; }; const plugin = (file, librarySettings, inputs, otherArguments) => { inputs = loadDefaultValues(inputs, details); const fileNameOld = file._id; + + + var response = { + file, + removeFromDB: false, + updateDB: true, + infoLog: "", + processFile: false, + }; + const codecMap = { 'aac': 'AAC', @@ -121,64 +135,69 @@ const plugin = (file, librarySettings, inputs, otherArguments) => { file.file = file.file.replace(audioCodecRegex, renamedCodec); } } + +let additionalFilesCount = 0; // Counter for additional files found + +if ((audioStream && inputs.rename_audio) || (videoStream && inputs.rename_video)) { + const filename = path.basename(fileNameOld); + const JustName = path.parse(filename).name; + const popJustnamen = JustName.split('.'); + popJustnamen.splice(popJustnamen.length - 5); + const modJustname = popJustnamen.join('.'); + + const fileDir = path.dirname(fileNameOld); + const directoryPath = fileDir; + + const additionalExtensions = inputs.additional_extensions.split(','); + + let fileList = []; // Array to store the file names + const files = fs.readdirSync(directoryPath); + + files.forEach((supportFile) => { + fileList.push(supportFile); // Add all files to the fileList array + }); - var filename = fileNameOld.replace(/^.*[\\\/]/, ''); - var JustName = filename.split('.'); - JustName.pop(); - JustName = JustName.join('.').toString(); - - const fileDir = fileNameOld.split('/'); - fileDir.pop(); - const directoryPath = fileDir.join('/'); - - const additionalExtensions = inputs.additional_extensions - .split(/[,;/]/) - .map(extension => extension.trim()) - .filter(extension => extension !== ''); - - additionalExtensions.forEach(extension => { - const regex = new RegExp(`${JustName}.*${extension}`, 'i'); - const files = fs.readdirSync(directoryPath); - - files.forEach((supportFile) => { - if (regex.test(supportFile)) { - const renamedFile = supportFile.replace(regex, (match, p1, p2) => { - const additionalFileName = match; - const renamedVideoCodec = codecMap[firstVideoStreamCodec]; - const renamedAudioCodec = codecMap[firstAudioStreamCodec]; - - const renamedFileWithCodecs = additionalFileName - .replace(videoCodecRegex, renamedVideoCodec) - .replace(audioCodecRegex, renamedAudioCodec); - - return renamedFileWithCodecs; - }); - - fs.renameSync(`${directoryPath}/${supportFile}`, `${directoryPath}/${renamedFile}`, { - overwrite: true, - }); - } - }); + const extensionList = additionalExtensions.map(extension => extension.trim()); // Remove leading/trailing spaces from extensions + const regex = new RegExp(`(${extensionList.join('|')})$`, 'i'); + + files.forEach((supportFile) => { + if (supportFile.startsWith(modJustname) && regex.test(supportFile)) { + const renamedFileWithVideoCodec = supportFile.replace(videoCodecRegex, codecMap[firstVideoStreamCodec]); + const renamedFileWithBothCodecs = renamedFileWithVideoCodec.replace(audioCodecRegex, codecMap[firstAudioStreamCodec]); + + fs.renameSync(`${directoryPath}/${supportFile}`, `${directoryPath}/${renamedFileWithBothCodecs}`, { + overwrite: true, + }); + + response.infoLog += `${directoryPath}/${supportFile} renamed to ${directoryPath}/${renamedFileWithBothCodecs}\n`; + additionalFilesCount++; // Increment the count for each additional file found + } }); + //const textFilePath = path.join(directoryPath, `${modJustname}.txt`); + //fs.writeFileSync(textFilePath, fileList.filter(file => file.startsWith(modJustname) && regex.test(file)).join('\n'), 'utf-8'); +} + + + if (fileNameOld !== file._id) { fs.renameSync(fileNameOld, file._id, { overwrite: true, }); - - const response = { - file, - removeFromDB: false, - updateDB: true, - }; - + response.infoLog += `Renamed file to: ${file._id}\n` + if (additionalFilesCount > 0) { + response.infoLog += `and: ${additionalFilesCount} additional Files!\n`; + } return response; - } else { - return { - processFile: true, - infoLog: `Renamed file and associated files to: ${file._id}`, - }; } + else { + response.infoLog += `Video File not renamed!\n` + if (additionalFilesCount > 0) { + response.infoLog += `But: ${additionalFilesCount} additional Files!\n`; + } + return response; + } + }; module.exports.details = details; From 73482c5a10dbaf53deb6f29079f7a5aedd1bc951 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:25:25 +0100 Subject: [PATCH 06/11] Run lint fix --- ...arr_Plugin_rename_based_on_codec_schadi.js | 400 +++++++++--------- 1 file changed, 196 insertions(+), 204 deletions(-) diff --git a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js index 0d25dd1..675150e 100644 --- a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js +++ b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js @@ -1,204 +1,196 @@ -const loadDefaultValues = require('../methods/loadDefaultValues'); -const fs = require('fs'); -const path = require('path'); - -const details = () => { - return { - id: "Tdarr_Plugin_rename_based_on_codec_schadi", - Stage: "Post-processing", - Name: "Rename based on codec Video and Audio", - Type: "Video_Audio", - Operation: "Transcode", - Description: ` - If the filename contains a codec information like h264, av1 or similar for video and AC3, AAC or trueHD \n\n - the plugin will read the codec info from the file and rename it accordingly. \n\n - It also takes care off addiotnal files deffined in the input Option. \n\n`, - Version: "1.00", - Tags: "post-processing", - Inputs:[ - { - name: 'rename_audio', - type: 'boolean', - defaultValue: false, - inputUI: { - type: 'dropdown', - options: [ - 'false', - 'true', - ], - }, - tooltip: `Will Rename According to Audio Codec after x264 or x265. - \\nExample:\\n - true - \\nExample:\\n - false`, - }, - { - name: 'rename_video', - type: 'boolean', - defaultValue: false, - inputUI: { - type: 'dropdown', - options: [ - 'false', - 'true', - ], - }, - tooltip: `Will Rename According to Video Codec after x264 or x265. - \\nExample:\\n - true - \\nExample:\\n - false`, - }, - { - name: 'additional_extensions', - type: 'text', - defaultValue: '.nfo,.srt', - inputUI: { - type: 'text', - }, - tooltip: `Additional file extensions to rename (comma-separated). - \\nExample:\\n - .nfo,.srt`, - }, - ], - }; -}; - -const plugin = (file, librarySettings, inputs, otherArguments) => { - inputs = loadDefaultValues(inputs, details); - const fileNameOld = file._id; - - - var response = { - file, - removeFromDB: false, - updateDB: true, - infoLog: "", - processFile: false, - }; - - - const codecMap = { - 'aac': 'AAC', - 'ac3': 'AC3', - 'av1': 'AV1', - 'avc': 'h264', - 'dts': 'DTS', - 'eac3': 'EAC3', - 'flac': 'FLAC', - 'hevc': 'h265', - 'mp2': 'MP2', - 'mp3': 'MP3', - 'mpeg2': 'MPEG2', - 'truehd': 'TrueHD', - 'x264': 'h264', - 'x265': 'h265', - 'h264': 'h264', - 'h265': 'h265', - 'dts': 'DTS-X', - 'dts-hd ma': 'DTS-HD MA', - 'dts-es': 'DTS-HD ES', - 'dts-hd hra': 'DTS-HD HRA', - 'dts express ': 'DTS Express', - 'dts 96/24': 'DTS', - }; - - var firstVideoStreamCodec; - var firstAudioStreamCodec; - - const videoCodecRegex = /(h264|h265|x264|x265|avc|hevc|mpeg2|av1)/gi; - const audioCodecRegex = /(aac|ac3|eac3|flac|mp2|mp3|truehd|dts[-. ]hd[-. ]ma|dts[-. ]hd[-. ]es|dts[-. ]hd[-. ]hra|dts[-. ]express|dts)/gi; - - const videoStream = file.ffProbeData.streams.find((stream) => stream.codec_type === 'video'); - - if (videoStream && inputs.rename_video) { - const videoCodec = videoStream.codec_name.toLowerCase(); - firstVideoStreamCodec = videoCodec; - - if (videoCodec in codecMap) { - const renamedCodec = codecMap[videoCodec]; - file._id = file._id.replace(videoCodecRegex, renamedCodec); - file.file = file.file.replace(videoCodecRegex, renamedCodec); - } - } - - const audioStream = file.ffProbeData.streams.find((stream) => stream.codec_type === 'audio'); - - if (audioStream && inputs.rename_audio) { - const audioCodec = audioStream.codec_name.toLowerCase(); - firstAudioStreamCodec = audioCodec; - - if (audioCodec in codecMap) { - const renamedCodec = codecMap[audioCodec]; - file._id = file._id.replace(audioCodecRegex, renamedCodec); - file.file = file.file.replace(audioCodecRegex, renamedCodec); - } - } - -let additionalFilesCount = 0; // Counter for additional files found - -if ((audioStream && inputs.rename_audio) || (videoStream && inputs.rename_video)) { - const filename = path.basename(fileNameOld); - const JustName = path.parse(filename).name; - const popJustnamen = JustName.split('.'); - popJustnamen.splice(popJustnamen.length - 5); - const modJustname = popJustnamen.join('.'); - - const fileDir = path.dirname(fileNameOld); - const directoryPath = fileDir; - - const additionalExtensions = inputs.additional_extensions.split(','); - - let fileList = []; // Array to store the file names - const files = fs.readdirSync(directoryPath); - - files.forEach((supportFile) => { - fileList.push(supportFile); // Add all files to the fileList array - }); - - const extensionList = additionalExtensions.map(extension => extension.trim()); // Remove leading/trailing spaces from extensions - const regex = new RegExp(`(${extensionList.join('|')})$`, 'i'); - - files.forEach((supportFile) => { - if (supportFile.startsWith(modJustname) && regex.test(supportFile)) { - const renamedFileWithVideoCodec = supportFile.replace(videoCodecRegex, codecMap[firstVideoStreamCodec]); - const renamedFileWithBothCodecs = renamedFileWithVideoCodec.replace(audioCodecRegex, codecMap[firstAudioStreamCodec]); - - fs.renameSync(`${directoryPath}/${supportFile}`, `${directoryPath}/${renamedFileWithBothCodecs}`, { - overwrite: true, - }); - - response.infoLog += `${directoryPath}/${supportFile} renamed to ${directoryPath}/${renamedFileWithBothCodecs}\n`; - additionalFilesCount++; // Increment the count for each additional file found - } - }); - - //const textFilePath = path.join(directoryPath, `${modJustname}.txt`); - //fs.writeFileSync(textFilePath, fileList.filter(file => file.startsWith(modJustname) && regex.test(file)).join('\n'), 'utf-8'); -} - - - - if (fileNameOld !== file._id) { - fs.renameSync(fileNameOld, file._id, { - overwrite: true, - }); - response.infoLog += `Renamed file to: ${file._id}\n` - if (additionalFilesCount > 0) { - response.infoLog += `and: ${additionalFilesCount} additional Files!\n`; - } - return response; - } - else { - response.infoLog += `Video File not renamed!\n` - if (additionalFilesCount > 0) { - response.infoLog += `But: ${additionalFilesCount} additional Files!\n`; - } - return response; - } - -}; - -module.exports.details = details; -module.exports.plugin = plugin; +const fs = require('fs'); +const path = require('path'); +const loadDefaultValues = require('../methods/loadDefaultValues'); + +const details = () => ({ + id: 'Tdarr_Plugin_rename_based_on_codec_schadi', + Stage: 'Post-processing', + Name: 'Rename based on codec Video and Audio', + Type: 'Video_Audio', + Operation: 'Transcode', + Description: ` + If the filename contains a codec information like h264, av1 or similar for video and AC3, AAC or trueHD \n\n + the plugin will read the codec info from the file and rename it accordingly. \n\n + It also takes care off addiotnal files deffined in the input Option. \n\n`, + Version: '1.00', + Tags: 'post-processing', + Inputs: [ + { + name: 'rename_audio', + type: 'boolean', + defaultValue: false, + inputUI: { + type: 'dropdown', + options: [ + 'false', + 'true', + ], + }, + tooltip: `Will Rename According to Audio Codec after x264 or x265. + \\nExample:\\n + true + \\nExample:\\n + false`, + }, + { + name: 'rename_video', + type: 'boolean', + defaultValue: false, + inputUI: { + type: 'dropdown', + options: [ + 'false', + 'true', + ], + }, + tooltip: `Will Rename According to Video Codec after x264 or x265. + \\nExample:\\n + true + \\nExample:\\n + false`, + }, + { + name: 'additional_extensions', + type: 'text', + defaultValue: '.nfo,.srt', + inputUI: { + type: 'text', + }, + tooltip: `Additional file extensions to rename (comma-separated). + \\nExample:\\n + .nfo,.srt`, + }, + ], +}); + +const plugin = (file, librarySettings, inputs, otherArguments) => { + inputs = loadDefaultValues(inputs, details); + const fileNameOld = file._id; + + const response = { + file, + removeFromDB: false, + updateDB: true, + infoLog: '', + processFile: false, + }; + + const codecMap = { + aac: 'AAC', + ac3: 'AC3', + av1: 'AV1', + avc: 'h264', + dts: 'DTS', + eac3: 'EAC3', + flac: 'FLAC', + hevc: 'h265', + mp2: 'MP2', + mp3: 'MP3', + mpeg2: 'MPEG2', + truehd: 'TrueHD', + x264: 'h264', + x265: 'h265', + h264: 'h264', + h265: 'h265', + dts: 'DTS-X', + 'dts-hd ma': 'DTS-HD MA', + 'dts-es': 'DTS-HD ES', + 'dts-hd hra': 'DTS-HD HRA', + 'dts express ': 'DTS Express', + 'dts 96/24': 'DTS', + }; + + let firstVideoStreamCodec; + let firstAudioStreamCodec; + + const videoCodecRegex = /(h264|h265|x264|x265|avc|hevc|mpeg2|av1)/gi; + const audioCodecRegex = /(aac|ac3|eac3|flac|mp2|mp3|truehd|dts[-. ]hd[-. ]ma|dts[-. ]hd[-. ]es|dts[-. ]hd[-. ]hra|dts[-. ]express|dts)/gi; + + const videoStream = file.ffProbeData.streams.find((stream) => stream.codec_type === 'video'); + + if (videoStream && inputs.rename_video) { + const videoCodec = videoStream.codec_name.toLowerCase(); + firstVideoStreamCodec = videoCodec; + + if (videoCodec in codecMap) { + const renamedCodec = codecMap[videoCodec]; + file._id = file._id.replace(videoCodecRegex, renamedCodec); + file.file = file.file.replace(videoCodecRegex, renamedCodec); + } + } + + const audioStream = file.ffProbeData.streams.find((stream) => stream.codec_type === 'audio'); + + if (audioStream && inputs.rename_audio) { + const audioCodec = audioStream.codec_name.toLowerCase(); + firstAudioStreamCodec = audioCodec; + + if (audioCodec in codecMap) { + const renamedCodec = codecMap[audioCodec]; + file._id = file._id.replace(audioCodecRegex, renamedCodec); + file.file = file.file.replace(audioCodecRegex, renamedCodec); + } + } + + let additionalFilesCount = 0; // Counter for additional files found + + if ((audioStream && inputs.rename_audio) || (videoStream && inputs.rename_video)) { + const filename = path.basename(fileNameOld); + const JustName = path.parse(filename).name; + const popJustnamen = JustName.split('.'); + popJustnamen.splice(popJustnamen.length - 5); + const modJustname = popJustnamen.join('.'); + + const fileDir = path.dirname(fileNameOld); + const directoryPath = fileDir; + + const additionalExtensions = inputs.additional_extensions.split(','); + + const fileList = []; // Array to store the file names + const files = fs.readdirSync(directoryPath); + + files.forEach((supportFile) => { + fileList.push(supportFile); // Add all files to the fileList array + }); + + const extensionList = additionalExtensions.map((extension) => extension.trim()); // Remove leading/trailing spaces from extensions + const regex = new RegExp(`(${extensionList.join('|')})$`, 'i'); + + files.forEach((supportFile) => { + if (supportFile.startsWith(modJustname) && regex.test(supportFile)) { + const renamedFileWithVideoCodec = supportFile.replace(videoCodecRegex, codecMap[firstVideoStreamCodec]); + const renamedFileWithBothCodecs = renamedFileWithVideoCodec.replace(audioCodecRegex, codecMap[firstAudioStreamCodec]); + + fs.renameSync(`${directoryPath}/${supportFile}`, `${directoryPath}/${renamedFileWithBothCodecs}`, { + overwrite: true, + }); + + response.infoLog += `${directoryPath}/${supportFile} renamed to ${directoryPath}/${renamedFileWithBothCodecs}\n`; + additionalFilesCount++; // Increment the count for each additional file found + } + }); + + // const textFilePath = path.join(directoryPath, `${modJustname}.txt`); + // fs.writeFileSync(textFilePath, fileList.filter(file => file.startsWith(modJustname) && regex.test(file)).join('\n'), 'utf-8'); + } + + if (fileNameOld !== file._id) { + fs.renameSync(fileNameOld, file._id, { + overwrite: true, + }); + response.infoLog += `Renamed file to: ${file._id}\n`; + if (additionalFilesCount > 0) { + response.infoLog += `and: ${additionalFilesCount} additional Files!\n`; + } + return response; + } + + response.infoLog += 'Video File not renamed!\n'; + if (additionalFilesCount > 0) { + response.infoLog += `But: ${additionalFilesCount} additional Files!\n`; + } + return response; +}; + +module.exports.details = details; +module.exports.plugin = plugin; From cbbae8e67a799cfadf06b1d8ce93c30629044e19 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:28:37 +0100 Subject: [PATCH 07/11] Fix lint errors --- ...arr_Plugin_rename_based_on_codec_schadi.js | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js index 675150e..9fcd412 100644 --- a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js +++ b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js @@ -1,3 +1,5 @@ +/* eslint-disable max-len */ + const fs = require('fs'); const path = require('path'); const loadDefaultValues = require('../methods/loadDefaultValues'); @@ -11,7 +13,7 @@ const details = () => ({ Description: ` If the filename contains a codec information like h264, av1 or similar for video and AC3, AAC or trueHD \n\n the plugin will read the codec info from the file and rename it accordingly. \n\n - It also takes care off addiotnal files deffined in the input Option. \n\n`, + It also takes care off addiotnal files deffined in the input Option.\n\n`, Version: '1.00', Tags: 'post-processing', Inputs: [ @@ -53,7 +55,7 @@ const details = () => ({ name: 'additional_extensions', type: 'text', defaultValue: '.nfo,.srt', - inputUI: { + inputUI: { type: 'text', }, tooltip: `Additional file extensions to rename (comma-separated). @@ -63,7 +65,9 @@ const details = () => ({ ], }); +// eslint-disable-next-line no-unused-vars const plugin = (file, librarySettings, inputs, otherArguments) => { + // eslint-disable-next-line no-param-reassign inputs = loadDefaultValues(inputs, details); const fileNameOld = file._id; @@ -71,8 +75,8 @@ const plugin = (file, librarySettings, inputs, otherArguments) => { file, removeFromDB: false, updateDB: true, - infoLog: '', - processFile: false, + infoLog: '', + processFile: false, }; const codecMap = { @@ -92,7 +96,7 @@ const plugin = (file, librarySettings, inputs, otherArguments) => { x265: 'h265', h264: 'h264', h265: 'h265', - dts: 'DTS-X', + // dts: 'DTS-X', 'dts-hd ma': 'DTS-HD MA', 'dts-es': 'DTS-HD ES', 'dts-hd hra': 'DTS-HD HRA', @@ -114,7 +118,9 @@ const plugin = (file, librarySettings, inputs, otherArguments) => { if (videoCodec in codecMap) { const renamedCodec = codecMap[videoCodec]; + // eslint-disable-next-line no-param-reassign file._id = file._id.replace(videoCodecRegex, renamedCodec); + // eslint-disable-next-line no-param-reassign file.file = file.file.replace(videoCodecRegex, renamedCodec); } } @@ -127,7 +133,9 @@ const plugin = (file, librarySettings, inputs, otherArguments) => { if (audioCodec in codecMap) { const renamedCodec = codecMap[audioCodec]; + // eslint-disable-next-line no-param-reassign file._id = file._id.replace(audioCodecRegex, renamedCodec); + // eslint-disable-next-line no-param-reassign file.file = file.file.replace(audioCodecRegex, renamedCodec); } } @@ -166,7 +174,7 @@ const plugin = (file, librarySettings, inputs, otherArguments) => { }); response.infoLog += `${directoryPath}/${supportFile} renamed to ${directoryPath}/${renamedFileWithBothCodecs}\n`; - additionalFilesCount++; // Increment the count for each additional file found + additionalFilesCount += 1; // Increment the count for each additional file found } }); From 36b3223957ec0ff6d6da56e93ab8301c3ccb9fb2 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:35:04 +0100 Subject: [PATCH 08/11] Fix checkPlugins issues --- .../Tdarr_Plugin_075a_FFMPEG_HEVC_Generic.js | 56 ------------------- ...arr_Plugin_rename_based_on_codec_schadi.js | 17 +++--- 2 files changed, 9 insertions(+), 64 deletions(-) delete mode 100644 Community/Tdarr_Plugin_075a_FFMPEG_HEVC_Generic.js diff --git a/Community/Tdarr_Plugin_075a_FFMPEG_HEVC_Generic.js b/Community/Tdarr_Plugin_075a_FFMPEG_HEVC_Generic.js deleted file mode 100644 index e699050..0000000 --- a/Community/Tdarr_Plugin_075a_FFMPEG_HEVC_Generic.js +++ /dev/null @@ -1,56 +0,0 @@ -/* eslint-disable */ -const details = () => ({ - id: 'Tdarr_Plugin_075a_FFMPEG_HEVC_Generic', - Stage: 'Pre-processing', - Name: 'FFMPEG H265', - Type: 'Video', - Operation: 'Transcode', - Description: '[Contains built-in filter] This plugin transcodes non h265 files into h265 mkv using default settings. Audio/subtitles not affected. \n\n', - Version: '1.00', - Tags: 'pre-processing,ffmpeg,h265,video only', - Inputs: [], -}); - -// eslint-disable-next-line no-unused-vars -const plugin = (file, librarySettings, inputs, otherArguments) => { - - const lib = require('../methods/lib')(); - // eslint-disable-next-line no-unused-vars,no-param-reassign - inputs = lib.loadDefaultValues(inputs, details); - // Must return this object - - const response = { - processFile: false, - preset: '', - container: '.mp4', - handBrakeMode: false, - FFmpegMode: false, - reQueueAfter: false, - infoLog: '', - }; - - if (file.fileMedium !== 'video') { - response.processFile = false; - response.infoLog += '☒File is not a video! \n'; - return response; - } - response.infoLog += '☑File is a video! \n'; - - if (file.ffProbeData.streams[0].codec_name == 'hevc') { - response.processFile = false; - response.infoLog += '☑File is already in hevc! \n'; - return response; - } - - response.processFile = true; - response.preset = ',-map 0:v -map 0:a -map 0:s? -map 0:d? -c copy -c:v:0 libx265 -max_muxing_queue_size 9999'; - response.container = '.mkv'; - response.handBrakeMode = false; - response.FFmpegMode = true; - response.reQueueAfter = true; - response.infoLog += '☒File is not hevc! \n'; - return response; -}; - -module.exports.details = details; -module.exports.plugin = plugin; diff --git a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js index 9fcd412..82e3ac6 100644 --- a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js +++ b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js @@ -1,14 +1,10 @@ /* eslint-disable max-len */ -const fs = require('fs'); -const path = require('path'); -const loadDefaultValues = require('../methods/loadDefaultValues'); - const details = () => ({ id: 'Tdarr_Plugin_rename_based_on_codec_schadi', Stage: 'Post-processing', Name: 'Rename based on codec Video and Audio', - Type: 'Video_Audio', + Type: 'Video', Operation: 'Transcode', Description: ` If the filename contains a codec information like h264, av1 or similar for video and AC3, AAC or trueHD \n\n @@ -53,7 +49,7 @@ const details = () => ({ }, { name: 'additional_extensions', - type: 'text', + type: 'string', defaultValue: '.nfo,.srt', inputUI: { type: 'text', @@ -67,8 +63,13 @@ const details = () => ({ // eslint-disable-next-line no-unused-vars const plugin = (file, librarySettings, inputs, otherArguments) => { - // eslint-disable-next-line no-param-reassign - inputs = loadDefaultValues(inputs, details); + const lib = require('../methods/lib')(); + // eslint-disable-next-line no-unused-vars,no-param-reassign + inputs = lib.loadDefaultValues(inputs, details); + + const fs = require('fs'); + const path = require('path'); + const fileNameOld = file._id; const response = { From beee66a7c309346ea6254f7bbb2a5642d11cadb8 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:49:33 +0100 Subject: [PATCH 09/11] Revert "Fix checkPlugins issues" This reverts commit 36b3223957ec0ff6d6da56e93ab8301c3ccb9fb2. --- .../Tdarr_Plugin_075a_FFMPEG_HEVC_Generic.js | 56 +++++++++++++++++++ ...arr_Plugin_rename_based_on_codec_schadi.js | 17 +++--- 2 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 Community/Tdarr_Plugin_075a_FFMPEG_HEVC_Generic.js diff --git a/Community/Tdarr_Plugin_075a_FFMPEG_HEVC_Generic.js b/Community/Tdarr_Plugin_075a_FFMPEG_HEVC_Generic.js new file mode 100644 index 0000000..e699050 --- /dev/null +++ b/Community/Tdarr_Plugin_075a_FFMPEG_HEVC_Generic.js @@ -0,0 +1,56 @@ +/* eslint-disable */ +const details = () => ({ + id: 'Tdarr_Plugin_075a_FFMPEG_HEVC_Generic', + Stage: 'Pre-processing', + Name: 'FFMPEG H265', + Type: 'Video', + Operation: 'Transcode', + Description: '[Contains built-in filter] This plugin transcodes non h265 files into h265 mkv using default settings. Audio/subtitles not affected. \n\n', + Version: '1.00', + Tags: 'pre-processing,ffmpeg,h265,video only', + Inputs: [], +}); + +// eslint-disable-next-line no-unused-vars +const plugin = (file, librarySettings, inputs, otherArguments) => { + + const lib = require('../methods/lib')(); + // eslint-disable-next-line no-unused-vars,no-param-reassign + inputs = lib.loadDefaultValues(inputs, details); + // Must return this object + + const response = { + processFile: false, + preset: '', + container: '.mp4', + handBrakeMode: false, + FFmpegMode: false, + reQueueAfter: false, + infoLog: '', + }; + + if (file.fileMedium !== 'video') { + response.processFile = false; + response.infoLog += '☒File is not a video! \n'; + return response; + } + response.infoLog += '☑File is a video! \n'; + + if (file.ffProbeData.streams[0].codec_name == 'hevc') { + response.processFile = false; + response.infoLog += '☑File is already in hevc! \n'; + return response; + } + + response.processFile = true; + response.preset = ',-map 0:v -map 0:a -map 0:s? -map 0:d? -c copy -c:v:0 libx265 -max_muxing_queue_size 9999'; + response.container = '.mkv'; + response.handBrakeMode = false; + response.FFmpegMode = true; + response.reQueueAfter = true; + response.infoLog += '☒File is not hevc! \n'; + return response; +}; + +module.exports.details = details; +module.exports.plugin = plugin; diff --git a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js index 82e3ac6..9fcd412 100644 --- a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js +++ b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js @@ -1,10 +1,14 @@ /* eslint-disable max-len */ +const fs = require('fs'); +const path = require('path'); +const loadDefaultValues = require('../methods/loadDefaultValues'); + const details = () => ({ id: 'Tdarr_Plugin_rename_based_on_codec_schadi', Stage: 'Post-processing', Name: 'Rename based on codec Video and Audio', - Type: 'Video', + Type: 'Video_Audio', Operation: 'Transcode', Description: ` If the filename contains a codec information like h264, av1 or similar for video and AC3, AAC or trueHD \n\n @@ -49,7 +53,7 @@ const details = () => ({ }, { name: 'additional_extensions', - type: 'string', + type: 'text', defaultValue: '.nfo,.srt', inputUI: { type: 'text', @@ -63,13 +67,8 @@ const details = () => ({ // eslint-disable-next-line no-unused-vars const plugin = (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 fs = require('fs'); - const path = require('path'); - + // eslint-disable-next-line no-param-reassign + inputs = loadDefaultValues(inputs, details); const fileNameOld = file._id; const response = { From 3b215069a5d665fa59490fe0a27ba67d3a7542a2 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:49:52 +0100 Subject: [PATCH 10/11] Fix checkPlugin issues --- ...Tdarr_Plugin_rename_based_on_codec_schadi.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js index 9fcd412..82e3ac6 100644 --- a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js +++ b/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js @@ -1,14 +1,10 @@ /* eslint-disable max-len */ -const fs = require('fs'); -const path = require('path'); -const loadDefaultValues = require('../methods/loadDefaultValues'); - const details = () => ({ id: 'Tdarr_Plugin_rename_based_on_codec_schadi', Stage: 'Post-processing', Name: 'Rename based on codec Video and Audio', - Type: 'Video_Audio', + Type: 'Video', Operation: 'Transcode', Description: ` If the filename contains a codec information like h264, av1 or similar for video and AC3, AAC or trueHD \n\n @@ -53,7 +49,7 @@ const details = () => ({ }, { name: 'additional_extensions', - type: 'text', + type: 'string', defaultValue: '.nfo,.srt', inputUI: { type: 'text', @@ -67,8 +63,13 @@ const details = () => ({ // eslint-disable-next-line no-unused-vars const plugin = (file, librarySettings, inputs, otherArguments) => { - // eslint-disable-next-line no-param-reassign - inputs = loadDefaultValues(inputs, details); + const lib = require('../methods/lib')(); + // eslint-disable-next-line no-unused-vars,no-param-reassign + inputs = lib.loadDefaultValues(inputs, details); + + const fs = require('fs'); + const path = require('path'); + const fileNameOld = file._id; const response = { From bc400d9a3f17299670279a90a69467c85e0bf568 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:53:10 +0100 Subject: [PATCH 11/11] Rename plugin --- ...di.js => Tdarr_Plugin_scha_rename_based_on_codec_schadi.js} | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) rename Community/{Tdarr_Plugin_rename_based_on_codec_schadi.js => Tdarr_Plugin_scha_rename_based_on_codec_schadi.js} (98%) diff --git a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js b/Community/Tdarr_Plugin_scha_rename_based_on_codec_schadi.js similarity index 98% rename from Community/Tdarr_Plugin_rename_based_on_codec_schadi.js rename to Community/Tdarr_Plugin_scha_rename_based_on_codec_schadi.js index 82e3ac6..aab6e81 100644 --- a/Community/Tdarr_Plugin_rename_based_on_codec_schadi.js +++ b/Community/Tdarr_Plugin_scha_rename_based_on_codec_schadi.js @@ -1,7 +1,8 @@ /* eslint-disable max-len */ +// tdarrSkipTest const details = () => ({ - id: 'Tdarr_Plugin_rename_based_on_codec_schadi', + id: 'Tdarr_Plugin_scha_rename_based_on_codec_schadi', Stage: 'Post-processing', Name: 'Rename based on codec Video and Audio', Type: 'Video',