Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */
- const details = () => ({
- id: 'Tdarr_Plugin_Custom_AAC_Only',
- Stage: 'Pre-processing',
- Name: 'Convert Everything to AAC Stereo (Strict)',
- Type: 'Audio',
- Operation: 'Transcode',
- Description: 'Converts all audio streams to AAC stereo with strict stream mapping, preserving subtitles.',
- Version: '1.2',
- Tags: 'pre-processing,ffmpeg,audio only,custom',
- });
- const plugin = (file, librarySettings, inputs, otherArguments) => {
- const lib = require('../methods/lib')();
- inputs = lib.loadDefaultValues(inputs, details);
- const response = {
- processFile: false,
- container: `.${file.container}`,
- handBrakeMode: false,
- FFmpegMode: true,
- reQueueAfter: true,
- infoLog: '',
- };
- let ffmpegCommandInsert = '';
- let audioIdx = 0;
- let convert = false;
- // Keep video track unchanged
- ffmpegCommandInsert += `-map 0:v -c:v copy `;
- // Loop through each audio stream
- for (let i = 0; i < file.ffProbeData.streams.length; i++) {
- if (file.ffProbeData.streams[i].codec_type.toLowerCase() === 'audio') {
- const codec = file.ffProbeData.streams[i].codec_name;
- const channels = file.ffProbeData.streams[i].channels;
- const bitrate = parseInt(file.ffProbeData.streams[i].bit_rate, 10) || 0;
- let targetBitrate = 96000;
- if (bitrate >= 192000) {
- targetBitrate = 192000;
- } else if (bitrate >= 128000 && bitrate <= 191999) {
- targetBitrate = 128000;
- } else if (bitrate >= 96000 && bitrate <= 127999) {
- targetBitrate = 96000;
- } else {
- targetBitrate = bitrate;
- }
- if (codec === 'aac') {
- if (channels === 1) {
- if (bitrate !== targetBitrate) {
- ffmpegCommandInsert += `-map 0:${i} -c:a:${audioIdx} aac -ac 1 -b:a:${audioIdx} ${targetBitrate} `;
- response.infoLog += `Mono AAC track @ ${bitrate} → ${targetBitrate}bps\n`;
- convert = true;
- } else {
- ffmpegCommandInsert += `-map 0:${i} -c:a:${audioIdx} copy `;
- response.infoLog += `Mono AAC track unchanged @ ${bitrate}bps\n`;
- }
- } else if (channels === 2) {
- if (bitrate !== targetBitrate) {
- ffmpegCommandInsert += `-map 0:${i} -c:a:${audioIdx} aac -ac 2 -b:a:${audioIdx} ${targetBitrate} `;
- response.infoLog += `Stereo AAC track @ ${bitrate} → ${targetBitrate}bps\n`;
- convert = true;
- } else {
- ffmpegCommandInsert += `-map 0:${i} -c:a:${audioIdx} copy `;
- response.infoLog += `Stereo AAC track unchanged @ ${bitrate}bps\n`;
- }
- } else {
- ffmpegCommandInsert += `-map 0:${i} -c:a:${audioIdx} aac -ac 2 -b:a:${audioIdx} ${targetBitrate} `;
- response.infoLog += `Multichannel AAC downmixed to stereo @ ${targetBitrate}bps\n`;
- convert = true;
- }
- } else {
- if (channels === 1) {
- ffmpegCommandInsert += `-map 0:${i} -c:a:${audioIdx} aac -ac 1 -b:a:${audioIdx} ${targetBitrate} `;
- response.infoLog += `Mono ${codec} converted to AAC mono @ ${targetBitrate}bps\n`;
- } else {
- ffmpegCommandInsert += `-map 0:${i} -c:a:${audioIdx} aac -ac 2 -b:a:${audioIdx} ${targetBitrate} `;
- response.infoLog += `${channels}ch ${codec} converted to AAC stereo @ ${targetBitrate}bps\n`;
- }
- convert = true;
- }
- audioIdx++;
- }
- }
- // ✅ Copy all subtitle tracks
- ffmpegCommandInsert += `-map 0:s? -c:s copy `;
- if (convert) {
- response.processFile = true;
- response.preset = `, ${ffmpegCommandInsert} -strict -2 -max_muxing_queue_size 9999 `;
- } else {
- response.infoLog += '☑ File is already in correct format.\n';
- }
- return response;
- };
- module.exports.details = details;
- module.exports.plugin = plugin;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement