-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTdarr_Plugin_CleanTitle.js
184 lines (162 loc) · 6.31 KB
/
Tdarr_Plugin_CleanTitle.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const details = () => ({
id: "Tdarr_Plugin_CleanTitle",
Stage: "Pre-processing",
Name: "Clean title metadata",
Type: "Video",
Operation: "Transcode",
Description: "This plugin removes title metadata from video/audio/subtitles",
Version: "1.9",
Tags: "pre-processing,ffmpeg,configurable",
Inputs: [
{
name: "custom_title_matching",
type: "string",
defaultValue: "",
inputUI: {
type: "text",
},
tooltip: `Remove unwanted audio stream based on the stream title. Example: sdh,full`,
},
{
name: "custom_title_matching_subtitles",
type: "string",
defaultValue: "",
inputUI: {
type: "text",
},
tooltip: `Remove unwanted subtitles stream based on the stream title. Example: vfq,original`,
},
],
});
const plugin = (file, librarySettings, inputs, otherArguments) => {
const lib = require("../methods/lib")();
inputs = lib.loadDefaultValues(inputs, details);
const response = {
processFile: false,
preset: "",
container: `.${file.container}`,
handBrakeMode: false,
FFmpegMode: true,
reQueueAfter: false,
infoLog: "",
};
// Set up required variables.
let ffmpegCommandInsert = "";
let subtitleMapping = "";
let audioMapping = "";
let forcedsubtitles = "";
let videoIdx = 0;
let audioIdx = 0;
let subtitleIdx = 0;
let convert = false;
const forcedKeywords = ["forced", "forces"];
const sdhKeywords = ["sdh", "commentaires", "commentary"];
const formatCustomTitles = (titles) =>
titles ? titles.toLowerCase().split(",") : "";
const checkNotEmptyTitle = (title) => {
return !(title === undefined || title === '""' || title === "");
};
const custom_title_matching_audio = formatCustomTitles(
inputs.custom_title_matching,
);
const custom_title_matching_subtitles = formatCustomTitles(
inputs.custom_title_matching_subtitles,
);
response.infoLog += `All audio custom titles unwanted: ${custom_title_matching_audio}\n`;
response.infoLog += `All subtitles custom titles unwanted: ${custom_title_matching_subtitles}\n`;
// Check if inputs.custom_title_matching has been configured. If it has then set variable
// Check if file is a video. If it isn't then exit plugin.
if (file.fileMedium !== "video") {
response.infoLog += "☒File is not video \n";
response.processFile = false;
return response;
}
// Check if overall file metadata title is not empty, if it's not empty set to "".
if (checkNotEmptyTitle(file.meta.Title)) {
ffmpegCommandInsert += " -metadata title= ";
convert = true;
}
for (let i = 0; i < file.ffProbeData.streams.length; i += 1) {
const codecType = file.ffProbeData.streams[i].codec_type.toLowerCase();
const streamTitle = file.ffProbeData.streams[i].tags?.title
? file.ffProbeData.streams[i].tags.title.toLowerCase()
: undefined;
if (codecType === "video") {
if (checkNotEmptyTitle(streamTitle)) {
response.infoLog += `Video stream title is not empty. Removing title from stream ${i} \n`;
ffmpegCommandInsert += ` -metadata:s:v:${videoIdx} title= `;
convert = true;
}
videoIdx += 1;
}
if (codecType === "audio") {
response.infoLog += `Audio stream ${audioIdx}\n`;
if (checkNotEmptyTitle(streamTitle)) {
if (
custom_title_matching_audio.some((title) =>
title.includes(streamTitle),
)
) {
audioMapping = `${audioMapping} -map -0:a:${audioIdx}`;
response.infoLog += `Audio stream ${audioIdx} has unwanted audio \n`;
} else {
response.infoLog += `Audio stream title is not empty. Removing title from stream ${i} \n`;
ffmpegCommandInsert += ` -metadata:s:a:${audioIdx} title= `;
}
convert = true;
}
audioIdx += 1;
}
if (codecType === "subtitle") {
response.infoLog += `Subtitle stream ${subtitleIdx}\n`;
if (checkNotEmptyTitle(streamTitle)) {
if (
custom_title_matching_subtitles.some((title) =>
title.includes(streamTitle),
)
) {
response.infoLog += `This stream has unwanted subtitles : ${streamTitle}\n`;
subtitleMapping = `${subtitleMapping} -map -0:s:${subtitleIdx}`;
} else {
response.infoLog += `Subtitle stream ${subtitleIdx} has no unwanted subtitles \n`;
if (forcedKeywords.some((keyword) => streamTitle.includes(keyword))) {
if (file.ffProbeData.streams[i].disposition.forced === 1) {
response.infoLog += `Subtitle stream ${subtitleIdx} has already forced flag : ${streamTitle}\n`;
} else {
forcedsubtitles += ` -disposition:s:${subtitleIdx} forced`;
response.infoLog += `Subtitle stream ${subtitleIdx} forced subtitles and has no forced flag : ${streamTitle}\n`;
}
} else {
response.infoLog += `Subtitle stream ${subtitleIdx} has no forced subtitles \n`;
}
if (sdhKeywords.some((keyword) => streamTitle.includes(keyword))) {
if (
file.ffProbeData.streams[i].disposition.hearing_impaired === 1
) {
response.infoLog += `Subtitle stream ${subtitleIdx} has already hearing_impaired flag : ${streamTitle}\n`;
} else {
forcedsubtitles += ` -disposition:s:${subtitleIdx} hearing_impaired`;
response.infoLog += `Subtitle stream ${subtitleIdx} hearing_impaired subtitles and has no hearing_impaired flag : ${streamTitle}\n`;
}
} else {
response.infoLog += `Subtitle stream ${subtitleIdx} has no hearing_impaired subtitles \n`;
}
response.infoLog += `Subtitle stream title is not empty. Removing title from stream ${i}\n`;
ffmpegCommandInsert += ` -metadata:s:s:${subtitleIdx} title= `;
}
convert = true;
}
subtitleIdx += 1;
}
}
if (convert) {
response.preset = `,${ffmpegCommandInsert} ${forcedsubtitles} -c copy -map 0 ${subtitleMapping} ${audioMapping} -max_muxing_queue_size 9999 -fflags +bitexact`;
response.reQueueAfter = true;
response.processFile = true;
} else {
response.infoLog += "Skipping, file has no title metadata \n";
}
return response;
};
module.exports.details = details;
module.exports.plugin = plugin;