Skip to content

Commit

Permalink
Add rdub rename audio classic plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rwarner committed Jan 1, 2025
1 parent 7b3f9ee commit 14817f6
Show file tree
Hide file tree
Showing 2 changed files with 276 additions and 0 deletions.
148 changes: 148 additions & 0 deletions Community/Tdarr_Plugin_rdub_audio_rename_channels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/* eslint-disable no-console */
const details = () => ({
id: 'Tdarr_Plugin_rdub_audio_rename_channels',
Stage: 'Pre-processing',
Name: 'Rdub Rename Audio',
Type: 'Video',
Operation: 'Transcode',
Description: 'This plugin renames titles for audio streams. '
+ 'An example would be in VLC -> Audio -> Audio Track -> " "5.1" - [English]" \n\n'
+ '- 8 Channels will replace the title with "7.1"\n '
+ '- 6 Channels will replace the title with "5.1"\n '
+ '- 2 Channels will replace the title with "2.0"',
Version: '1.0',
Tags: 'pre-processing,ffmpeg',
Inputs:[],
});

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const plugin = (file, librarySettings, inputs, otherArguments) => {
const lib = require('../methods/lib')();

// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign
inputs = lib.loadDefaultValues(inputs, details);

const response = {
processFile: false,
preset: '',
container: `.${file.container}`,
handBrakeMode: false,
FFmpegMode: true,
reQueueAfter: false,
infoLog: '',
};

// Set up required variables
let ffmpegCommandInsert = '';
let modifyFile = false;

// Check if file is a video. If it isn't then exit plugin.
if (file.fileMedium !== 'video') {
response.infoLog += '☒ File is not video \n';
response.processFile = false;
return response;
}

// Go through each stream in the file
for (let i = 0; i < file.ffProbeData.streams.length; i += 1) {
// Check if the stream is an audio stream
if (file.ffProbeData.streams[i].codec_type.toLowerCase() === 'audio') {
try {
// Retrieve current title metadata
let currentTitle = file.ffProbeData.streams[i].tags?.title || '';

// Trim whitespace and strip only one set of surrounding double quotes
currentTitle = currentTitle.trim().replace(/^"|"$/g, '');

response.infoLog += `Current Title: [${i}] "${currentTitle}" \n`;

// Check if the title is already renamed to a standard format ("7.1", "5.1", "2.0")
if (['7.1', '5.1', '2.0'].includes(currentTitle)) {
response.infoLog
+= `☑ Audio stream ${i} already has a renamed title: `
+ `"${currentTitle}". Skipping further renaming. \n`;

response.infoLog += 'skipping \n';

// Skip further renaming for this stream
// eslint-disable-next-line no-continue
continue;
}

// Check if the stream has 8 channels and rename it to "7.1"
if (file.ffProbeData.streams[i].channels === 8) {
try {
response.infoLog
+= `☑ Audio stream ${i} has 8 channels. `
+ 'Renaming title to "7.1" \n';

// Add ffmpeg line to rename stream
ffmpegCommandInsert += ` -metadata:s:a:${i} title=7.1 `;

modifyFile = true;
} catch (err) {
// Error while modifying metadata
response.infoLog += `Error ${err} \n`;
}
}

// Check if the stream has 6 channels and rename it to "5.1"
if (file.ffProbeData.streams[i].channels === 6) {
try {
response.infoLog
+= `☑ Audio stream ${i} has 6 channels. `
+ 'Renaming title to "5.1" \n';

// Add ffmpeg line to rename stream
ffmpegCommandInsert += ` -metadata:s:a:${i} title=5.1 `;

modifyFile = true;
} catch (err) {
// Error while modifying metadata
response.infoLog += `Error ${err} \n`;
}
}

// Check if the stream has 2 channels and rename it to "2.0"
if (file.ffProbeData.streams[i].channels === 2) {
try {
response.infoLog
+= `☑ Audio stream ${i} has 2 channels. `
+ 'Renaming title to "2.0" \n';

// Add ffmpeg line to rename stream
ffmpegCommandInsert += ` -metadata:s:a:${i} title=2.0 `;
modifyFile = true;
} catch (err) {
// Error while modifying metadata
response.infoLog += `Error ${err} \n`;
}
}
} catch (err) {
// Error in processing audio stream
response.infoLog += `Error ${err} \n`;
}
}
}

// Convert file if convert variable is set to true.
if (modifyFile === true) {
// If we have a modified file flagged
response.infoLog += '☒ File has audio tracks to rename. Renaming...\n';

// Set the new preset
response.preset = `, ${ffmpegCommandInsert} -c copy -map 0 -max_muxing_queue_size 9999`;

// Set to re-queue the file and re-process the file
response.reQueueAfter = true;
response.processFile = true;
} else {
// No modifictions needed, output success
response.infoLog += '☑ File has no need to rename audio streams \n';
}

return response;
};

module.exports.details = details;
module.exports.plugin = plugin;
128 changes: 128 additions & 0 deletions tests/Community/Tdarr_Plugin_rdub_audio_rename_channels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/* eslint max-len: 0 */
const _ = require('lodash');
const run = require('../helpers/run');

// Test suite for the Tdarr_Plugin_rdub_audio_rename_channels plugin
const pluginTests = [

{
input: {
file: (() => {
const file = _.cloneDeep(require('../sampleData/media/sampleH265_1.json'));
// Simulate a video file with multiple audio streams
file.fileMedium = 'video';
file.ffProbeData.streams = [
{
codec_type: 'audio',
channels: 8,
tags: { title: '' },
},
{
codec_type: 'audio',
channels: 6,
tags: { title: '' },
},
{
codec_type: 'audio',
channels: 2,
tags: { title: '' },
},
];
return file;
})(),
librarySettings: {},
inputs: {},
otherArguments: {},
},
output: {
processFile: true,
preset: ', -metadata:s:a:0 title=7.1 -metadata:s:a:1 title=5.1 -metadata:s:a:2 title=2.0 -c copy -map 0 -max_muxing_queue_size 9999',
container: '.mkv',
handBrakeMode: false,
FFmpegMode: true,
reQueueAfter: true,
infoLog: 'Current Title: [0] "" \n'
+ '☑ Audio stream 0 has 8 channels. Renaming title to "7.1" \n'
+ 'Current Title: [1] "" \n'
+ '☑ Audio stream 1 has 6 channels. Renaming title to "5.1" \n'
+ 'Current Title: [2] "" \n'
+ '☑ Audio stream 2 has 2 channels. Renaming title to "2.0" \n'
+ '☒ File has audio tracks to rename. Renaming...\n',
},
},

{
input: {
file: (() => {
const file = _.cloneDeep(require('../sampleData/media/sampleH265_1.json'));
// Simulate a video file with already renamed audio streams
file.fileMedium = 'video';
file.ffProbeData.streams = [
{
codec_type: 'audio',
channels: 8,
tags: { title: '7.1' },
},
{
codec_type: 'audio',
channels: 6,
tags: { title: '5.1' },
},
{
codec_type: 'audio',
channels: 2,
tags: { title: '2.0' },
},
];
return file;
})(),
librarySettings: {},
inputs: {},
otherArguments: {},
},
output: {
processFile: false,
preset: '',
container: '.mkv',
handBrakeMode: false,
FFmpegMode: true,
reQueueAfter: false,
infoLog: 'Current Title: [0] "7.1" \n'
+ '☑ Audio stream 0 already has a renamed title: "7.1". Skipping further renaming. \n'
+ 'skipping \n'
+ 'Current Title: [1] "5.1" \n'
+ '☑ Audio stream 1 already has a renamed title: "5.1". Skipping further renaming. \n'
+ 'skipping \n'
+ 'Current Title: [2] "2.0" \n'
+ '☑ Audio stream 2 already has a renamed title: "2.0". Skipping further renaming. \n'
+ 'skipping \n'
+ '☑ File has no need to rename audio streams \n',
},
},

{
input: {
file: (() => {
const file = _.cloneDeep(require('../sampleData/media/sampleH265_1.json'));
// Simulate a non-video file
file.fileMedium = 'audio';
file.ffProbeData.streams = [];
return file;
})(),
librarySettings: {},
inputs: {},
otherArguments: {},
},
output: {
processFile: false,
preset: '',
container: '.mkv',
handBrakeMode: false,
FFmpegMode: true,
reQueueAfter: false,
infoLog: '☒ File is not video \n',
},
},
];

void run(pluginTests);

0 comments on commit 14817f6

Please sign in to comment.