Guest User

Tdarr Plugin Extract eng.sdh and spa Subtitles

a guest
Oct 14th, 2025
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // tdarrSkipTest
  2. const details = () => ({
  3.   id: 'Tdarr_Plugin_0000_Extract_Subtitles',
  4.   Stage: 'Pre-processing',
  5.   Name: 'Extract Subtitles',
  6.   Type: 'Video',
  7.   Operation: 'Transcode',
  8.   Description: 'This plugin extracts embedded english, spanish and SDH subs in one pass and optionally removes them. \n\n '
  9.       + 'Based on rr01_drpeppershaker_extract_subs_to_SRT ',
  10.   Version: '1.05',
  11.   Tags: 'pre-processing,subtitle only,ffmpeg,configurable',
  12.   Inputs: [
  13.     {
  14.       name: 'remove_subs',
  15.       type: 'string',
  16.       defaultValue: 'no',
  17.       inputUI: {
  18.         type: 'text',
  19.       },
  20.       tooltip: `Do you want to remove subtitles after they are extracted?
  21.  
  22.         \\nExample:\\n
  23.  
  24.         yes
  25.  
  26.         \\nExample:\\n
  27.  
  28.         no
  29.         `,
  30.     },
  31.   ],
  32. });
  33.  
  34. // eslint-disable-next-line no-unused-vars
  35. const plugin = (file, librarySettings, inputs, otherArguments) => {
  36.   const lib = require('../methods/lib')(); const fs = require('fs');
  37.   // eslint-disable-next-line no-unused-vars,no-param-reassign
  38.   inputs = lib.loadDefaultValues(inputs, details);
  39.   // Must return this object at some point in the function else plugin will fail.
  40.   const response = {
  41.     processFile: true,
  42.     preset: '',
  43.     container: `.${file.container}`,
  44.     handBrakeMode: false,
  45.     FFmpegMode: true,
  46.     reQueueAfter: false,
  47.     infoLog: '',
  48.   };
  49.  
  50.   if (inputs.remove_subs === undefined) {
  51.     response.processFile = false;
  52.     response.infoLog += '☒ Inputs not entered! \n';
  53.     return response;
  54.   }
  55.  
  56.   const subsArr = file.ffProbeData.streams.filter((row) => row.codec_name === 'subrip');
  57.  
  58.   if (subsArr.length === 0) {
  59.     response.infoLog += 'No subs in file to extract!\n';
  60.     response.processFile = false;
  61.     return response;
  62.   }
  63.   response.infoLog += 'Found subs to extract!\n';
  64.  
  65.   let command = '-y <io>';
  66.   for (let i = 0; i < subsArr.length; i += 1) {
  67.     const subStream = subsArr[i];
  68.     let lang = '';
  69.     let title = 'none';
  70.  
  71.     if (subStream && subStream.tags && subStream.tags.language) {
  72.       lang = subStream.tags.language;
  73.     }
  74.  
  75.     if (subStream && subStream.tags && subStream.tags.title) {
  76.       title = subStream.tags.title;
  77.     }
  78.  
  79.     // Filter for English, Spanish, and SDH subtitles
  80.     if (!['eng', 'spa'].includes(lang) && !(title.toLowerCase().includes('sdh') && lang === 'eng')) {
  81.       continue; // Skip if the subtitle isn't English, Spanish, or SDH
  82.     }
  83.  
  84.     const { originalLibraryFile } = otherArguments;
  85.  
  86.     let subsFile = '';
  87.  
  88.     // for Tdarr V2 (2.00.05+)
  89.     if (originalLibraryFile && originalLibraryFile.file) {
  90.       subsFile = originalLibraryFile.file;
  91.     } else {
  92.       // for Tdarr V1
  93.       subsFile = file.file;
  94.     }
  95.     subsFile = subsFile.split('.');
  96.     if (title.toLowerCase().includes('sdh') && lang === 'eng') {
  97.       subsFile[subsFile.length - 2] += `.eng.sdh`;
  98.     } else {
  99.       subsFile[subsFile.length - 2] += `.${lang}`;
  100.     }
  101.     subsFile[subsFile.length - 1] = 'srt';
  102.     subsFile = subsFile.join('.');
  103.  
  104.     const { index } = subStream;
  105.     if (fs.existsSync(`${subsFile}`)) {
  106.       response.infoLog += `${lang}.srt already exists. Skipping!\n`;
  107.     } else if (typeof title === 'string'
  108.     && (title.toLowerCase().includes('commentary')
  109.     || title.toLowerCase().includes('description'))) {
  110.       response.infoLog += `Stream ${i} ${lang}.srt is a ${title} track. Skipping!\n`;
  111.     } else {
  112.       response.infoLog += `Extracting ${lang}.srt\n`;
  113.       command += ` -map 0:${index} "${subsFile}"`;
  114.     }
  115.   }
  116.  
  117.   if (command === '-y <io>') {
  118.     response.infoLog += 'All subs already extracted!\n';
  119.     if (inputs.remove_subs === 'no') {
  120.       response.processFile = false;
  121.       return response;
  122.     }
  123.   }
  124.  
  125.   response.preset = command;
  126.  
  127.   if (inputs.remove_subs === 'yes') {
  128.     response.preset += ' -map 0 -map -0:s -c copy';
  129.   }
  130.  
  131.   if (inputs.remove_subs === 'no') {
  132.     response.preset += ' -map 0 -c copy';
  133.   }
  134.  
  135.   return response;
  136. };
  137.  
  138. module.exports.details = details;
  139. module.exports.plugin = plugin;
Tags: Tdarr
Advertisement
Add Comment
Please, Sign In to add comment