forked from HaveAGitGat/Tdarr_Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTdarr_Plugin_076b_re_order_subtitle_streams.js
More file actions
149 lines (117 loc) · 3.61 KB
/
Copy pathTdarr_Plugin_076b_re_order_subtitle_streams.js
File metadata and controls
149 lines (117 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* eslint-disable */
const details = () => {
return {
id: "Tdarr_Plugin_076b_re_order_subtitle_streams",
Stage: "Pre-processing",
Name: "Re-order Subtitle Streams",
Type: "Subtitle",
Operation: "Transcode",
Description: `[Contains built-in filter] Specify a language tag for Tdarr to try and put as 1st subtitle track \n\n`,
Version: "1.00",
Tags: "pre-processing,subtitle only,ffmpeg,configurable",
Inputs: [
{
name: "preferred_language",
type:'string',
defaultValue:'eng',
inputUI: {
type: 'text',
},
tooltip: `Specify one language tag for Tdarr to try and put as 1st subtitle track
\\nExample:\\n
eng
\\nExample:\\n
en
\\nExample:\\n
fr
\\nExample:\\n
de
`,
},
],
};
};
// 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);
//Must return this object
var response = {
processFile: false,
preset: "",
container: ".mp4",
handBrakeMode: false,
FFmpegMode: false,
reQueueAfter: false,
infoLog: "",
};
console.log(inputs.preferred_language);
if (inputs.preferred_language === undefined) {
response.processFile = false;
response.infoLog += "☒ Inputs not entered! \n";
return response;
}
var desiredTrackPosition = file.ffProbeData.streams.filter(
(stream) =>
stream.codec_type.toLowerCase() == "video" ||
stream.codec_type.toLowerCase() == "audio"
).length;
var subtitleInLang = file.ffProbeData.streams.filter((stream) => {
if (
stream.codec_type.toLowerCase() == "subtitle" &&
stream.tags &&
stream.tags.language &&
inputs.preferred_language.includes(stream.tags.language.toLowerCase())
) {
return true;
}
return false;
});
if (subtitleInLang.length == 0) {
response.processFile = false;
response.infoLog += "☒ No subtitle tracks in desired language! \n";
return response;
}
var streamToMove = subtitleInLang[0];
if (streamToMove.index == desiredTrackPosition) {
response.processFile = false;
response.infoLog +=
"☑ Preferred language is already first subtitle track! \n";
return response;
}
var ffmpegCommand = ", -c copy ";
if (file.ffProbeData.streams[0].codec_type.toLowerCase() == "video") {
ffmpegCommand += ` -map 0:v -map 0:a `;
}
var allSubtitleTracks = file.ffProbeData.streams.filter(
(stream) => stream.codec_type.toLowerCase() == "subtitle"
);
var streamIdx;
for (var i = 0; i < allSubtitleTracks.length; i++) {
if (allSubtitleTracks[i].index == streamToMove.index) {
streamIdx = i;
break;
}
}
ffmpegCommand += ` -map 0:s:${streamIdx} -disposition:s:0 default`;
for (var i = 0; i < allSubtitleTracks.length; i++) {
if (i !== streamIdx) {
ffmpegCommand += ` -map 0:s:${i} `;
}
if (i !== 0) {
ffmpegCommand += ` -disposition:s:${i} 0 `;
}
}
ffmpegCommand += ` -map 0:d? `;
response.processFile = true;
response.preset = ffmpegCommand;
response.container = `.` + file.container;
response.handBrakeMode = false;
response.FFmpegMode = true;
response.reQueueAfter = true;
response.infoLog += `☒ Desired subtitle lang is not first subtitle stream, moving! \n`;
return response;
};
module.exports.details = details;
module.exports.plugin = plugin;