Advertisement
Harakiri88

AAC Plugin Tdarr

Apr 8th, 2025 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. /* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */
  2. const details = () => ({
  3. id: 'Tdarr_Plugin_Custom_AAC_Only',
  4. Stage: 'Pre-processing',
  5. Name: 'Convert Everything to AAC Stereo (Strict)',
  6. Type: 'Audio',
  7. Operation: 'Transcode',
  8. Description: 'Converts all audio streams to AAC stereo with strict stream mapping, preserving subtitles.',
  9. Version: '1.2',
  10. Tags: 'pre-processing,ffmpeg,audio only,custom',
  11. });
  12.  
  13. const plugin = (file, librarySettings, inputs, otherArguments) => {
  14. const lib = require('../methods/lib')();
  15. inputs = lib.loadDefaultValues(inputs, details);
  16.  
  17. const response = {
  18. processFile: false,
  19. container: `.${file.container}`,
  20. handBrakeMode: false,
  21. FFmpegMode: true,
  22. reQueueAfter: true,
  23. infoLog: '',
  24. };
  25.  
  26. let ffmpegCommandInsert = '';
  27. let audioIdx = 0;
  28. let convert = false;
  29.  
  30. // Keep video track unchanged
  31. ffmpegCommandInsert += `-map 0:v -c:v copy `;
  32.  
  33. // Loop through each audio stream
  34. for (let i = 0; i < file.ffProbeData.streams.length; i++) {
  35. if (file.ffProbeData.streams[i].codec_type.toLowerCase() === 'audio') {
  36. const codec = file.ffProbeData.streams[i].codec_name;
  37. const channels = file.ffProbeData.streams[i].channels;
  38. const bitrate = parseInt(file.ffProbeData.streams[i].bit_rate, 10) || 0;
  39.  
  40. let targetBitrate = 96000;
  41. if (bitrate >= 192000) {
  42. targetBitrate = 192000;
  43. } else if (bitrate >= 128000 && bitrate <= 191999) {
  44. targetBitrate = 128000;
  45. } else if (bitrate >= 96000 && bitrate <= 127999) {
  46. targetBitrate = 96000;
  47. } else {
  48. targetBitrate = bitrate;
  49. }
  50.  
  51. if (codec === 'aac') {
  52. if (channels === 1) {
  53. if (bitrate !== targetBitrate) {
  54. ffmpegCommandInsert += `-map 0:${i} -c:a:${audioIdx} aac -ac 1 -b:a:${audioIdx} ${targetBitrate} `;
  55. response.infoLog += `Mono AAC track @ ${bitrate} → ${targetBitrate}bps\n`;
  56. convert = true;
  57. } else {
  58. ffmpegCommandInsert += `-map 0:${i} -c:a:${audioIdx} copy `;
  59. response.infoLog += `Mono AAC track unchanged @ ${bitrate}bps\n`;
  60. }
  61. } else if (channels === 2) {
  62. if (bitrate !== targetBitrate) {
  63. ffmpegCommandInsert += `-map 0:${i} -c:a:${audioIdx} aac -ac 2 -b:a:${audioIdx} ${targetBitrate} `;
  64. response.infoLog += `Stereo AAC track @ ${bitrate} → ${targetBitrate}bps\n`;
  65. convert = true;
  66. } else {
  67. ffmpegCommandInsert += `-map 0:${i} -c:a:${audioIdx} copy `;
  68. response.infoLog += `Stereo AAC track unchanged @ ${bitrate}bps\n`;
  69. }
  70. } else {
  71. ffmpegCommandInsert += `-map 0:${i} -c:a:${audioIdx} aac -ac 2 -b:a:${audioIdx} ${targetBitrate} `;
  72. response.infoLog += `Multichannel AAC downmixed to stereo @ ${targetBitrate}bps\n`;
  73. convert = true;
  74. }
  75. } else {
  76. if (channels === 1) {
  77. ffmpegCommandInsert += `-map 0:${i} -c:a:${audioIdx} aac -ac 1 -b:a:${audioIdx} ${targetBitrate} `;
  78. response.infoLog += `Mono ${codec} converted to AAC mono @ ${targetBitrate}bps\n`;
  79. } else {
  80. ffmpegCommandInsert += `-map 0:${i} -c:a:${audioIdx} aac -ac 2 -b:a:${audioIdx} ${targetBitrate} `;
  81. response.infoLog += `${channels}ch ${codec} converted to AAC stereo @ ${targetBitrate}bps\n`;
  82. }
  83. convert = true;
  84. }
  85.  
  86. audioIdx++;
  87. }
  88. }
  89.  
  90. // ✅ Copy all subtitle tracks
  91. ffmpegCommandInsert += `-map 0:s? -c:s copy `;
  92.  
  93. if (convert) {
  94. response.processFile = true;
  95. response.preset = `, ${ffmpegCommandInsert} -strict -2 -max_muxing_queue_size 9999 `;
  96. } else {
  97. response.infoLog += '☑ File is already in correct format.\n';
  98. }
  99.  
  100. return response;
  101. };
  102.  
  103. module.exports.details = details;
  104. module.exports.plugin = plugin;
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement