|
| 1 | +/* eslint-disable no-console */ |
| 2 | +const details = () => ({ |
| 3 | + id: 'Tdarr_Plugin_rdub_audio_rename_channels', |
| 4 | + Stage: 'Pre-processing', |
| 5 | + Name: 'Rdub Rename Audio Titles', |
| 6 | + Type: 'Video', |
| 7 | + Operation: 'Transcode', |
| 8 | + Description: 'This plugin renames titles for audio streams. ' |
| 9 | + + 'An example would be in VLC -> Audio -> Audio Track -> "5.1 - [English]" \n\n' |
| 10 | + + '- 8 Channels will replace the title with 7.1\n ' |
| 11 | + + '- 6 Channels will replace the title with 5.1"\n ' |
| 12 | + + '- 2 Channels will replace the title with 2.0', |
| 13 | + Version: '1.0', |
| 14 | + Tags: 'pre-processing,ffmpeg', |
| 15 | + Inputs: [], |
| 16 | +}); |
| 17 | + |
| 18 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 19 | +const plugin = (file, librarySettings, inputs, otherArguments) => { |
| 20 | + const lib = require('../methods/lib')(); |
| 21 | + |
| 22 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign |
| 23 | + inputs = lib.loadDefaultValues(inputs, details); |
| 24 | + |
| 25 | + const response = { |
| 26 | + processFile: false, |
| 27 | + preset: '', |
| 28 | + container: `.${file.container}`, |
| 29 | + handBrakeMode: false, |
| 30 | + FFmpegMode: true, |
| 31 | + reQueueAfter: false, |
| 32 | + infoLog: '', |
| 33 | + }; |
| 34 | + |
| 35 | + // Initialize ffmpeg command insert |
| 36 | + let ffmpegCommandInsert = ''; |
| 37 | + let modifyFile = false; |
| 38 | + |
| 39 | + // Check if file is a video. If it isn't, exit the plugin. |
| 40 | + if (file.fileMedium !== 'video') { |
| 41 | + response.infoLog += '☒ File is not a video. Skipping processing.\n'; |
| 42 | + response.processFile = false; |
| 43 | + return response; |
| 44 | + } |
| 45 | + |
| 46 | + // Initialize audioIndex to track audio streams separately |
| 47 | + let audioIndex = 0; |
| 48 | + |
| 49 | + // Go through each stream in the file |
| 50 | + for (let i = 0; i < file.ffProbeData.streams.length; i += 1) { |
| 51 | + // Check if the stream is an audio stream |
| 52 | + if (file.ffProbeData.streams[i].codec_type.toLowerCase() === 'audio') { |
| 53 | + try { |
| 54 | + // Retrieve current title metadata |
| 55 | + let currentTitle = file.ffProbeData.streams[i].tags?.title || ''; |
| 56 | + |
| 57 | + // Trim whitespace |
| 58 | + currentTitle = currentTitle.trim(); |
| 59 | + |
| 60 | + // Check if the title matches a standard format ("7.1", "5.1", "2.0") |
| 61 | + if (['7.1', '5.1', '2.0'].includes(currentTitle)) { |
| 62 | + response.infoLog += `☑ Audio stream ${audioIndex} already has a renamed title: "${currentTitle}".` |
| 63 | + + ' Skipping further renaming.\n'; |
| 64 | + |
| 65 | + // Increment audioIndex since we are processing an audio stream |
| 66 | + // eslint-disable-next-line no-plusplus |
| 67 | + audioIndex++; |
| 68 | + |
| 69 | + // Skip further renaming for this stream |
| 70 | + // eslint-disable-next-line no-continue |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + // Determine new title based on the channel count |
| 75 | + let newTitle = ''; |
| 76 | + if (file.ffProbeData.streams[i].channels === 8) { |
| 77 | + newTitle = '7.1'; |
| 78 | + } else if (file.ffProbeData.streams[i].channels === 6) { |
| 79 | + newTitle = '5.1'; |
| 80 | + } else if (file.ffProbeData.streams[i].channels === 2) { |
| 81 | + newTitle = '2.0'; |
| 82 | + } |
| 83 | + |
| 84 | + // Rename the title if applicable |
| 85 | + if (newTitle) { |
| 86 | + response.infoLog += `☑ Audio stream ${audioIndex} has ${file.ffProbeData.streams[i].channels} channels. ` |
| 87 | + + `Renaming title to "${newTitle}"\n`; |
| 88 | + |
| 89 | + ffmpegCommandInsert += ` -metadata:s:a:${audioIndex} title=${newTitle} `; |
| 90 | + modifyFile = true; |
| 91 | + } |
| 92 | + } catch (err) { |
| 93 | + // Log error during audio stream processing |
| 94 | + response.infoLog += `Error processing audio stream ${audioIndex}: ${err}\n`; |
| 95 | + } |
| 96 | + |
| 97 | + // Increment audioIndex for the next audio stream |
| 98 | + // eslint-disable-next-line no-plusplus |
| 99 | + audioIndex++; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Finalize the command if modifications were made |
| 104 | + if (modifyFile) { |
| 105 | + response.infoLog += '☒ File has audio tracks to rename. Renaming...\n'; |
| 106 | + |
| 107 | + // Set the new preset |
| 108 | + response.preset = `, ${ffmpegCommandInsert} -c copy -map 0 -max_muxing_queue_size 9999`; |
| 109 | + |
| 110 | + // Re-queue the file for further processing |
| 111 | + response.reQueueAfter = true; |
| 112 | + response.processFile = true; |
| 113 | + } else { |
| 114 | + // No modifications needed |
| 115 | + response.infoLog += '☑ File has no need to rename audio streams.\n'; |
| 116 | + } |
| 117 | + |
| 118 | + return response; |
| 119 | +}; |
| 120 | + |
| 121 | +module.exports.details = details; |
| 122 | +module.exports.plugin = plugin; |
0 commit comments