forked from HaveAGitGat/Tdarr_Plugins
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
130 lines (114 loc) · 3.74 KB
/
index.ts
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
/* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */
import {
IffmpegCommandStream,
IpluginDetails,
IpluginInputArgs,
IpluginOutputArgs,
} from '../../../../FlowHelpers/1.0.0/interfaces/interfaces';
/* eslint-disable no-param-reassign */
const details = ():IpluginDetails => ({
name: 'Extract Streams',
description: `
Extract raw HEVC and srt streams from file
Make sure that the video stream is the last stream, otherwise the plugin will fail!
Use the reorder streams plugin before this.
`,
style: {
borderColor: '#6efefc',
},
tags: 'video',
isStartPlugin: false,
pType: '',
requiresVersion: '2.11.01',
sidebarPosition: -1,
icon: '',
inputs: [
{
label: 'Subtitle languages',
name: 'subtitle_languages',
type: 'string',
defaultValue: 'eng,hun',
inputUI: {
type: 'text',
},
tooltip: 'Specify subtitle languages to keep using comma seperated list e.g. eng,hun',
},
],
outputs: [
{
number: 1,
tooltip: 'Continue to next plugin',
},
],
});
const getOuputStreamIndex = (streams: IffmpegCommandStream[], stream: IffmpegCommandStream): number => {
let index = -1;
for (let idx = 0; idx < streams.length; idx += 1) {
if (!stream.removed) {
index += 1;
}
if (streams[idx].index === stream.index) {
break;
}
}
return index;
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const plugin = (args:IpluginInputArgs):IpluginOutputArgs => {
const lib = require('../../../../../methods/lib')();
// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign
args.inputs = lib.loadDefaultValues(args.inputs, details);
const subtitle_languages = String(args.inputs.subtitle_languages).trim().split(',');
args.variables.ffmpegCommand.container = 'hevc';
args.variables.ffmpegCommand.shouldProcess = true;
const subs_dir = `${args.workDir}/sub_streams`;
const { streams } = args.variables.ffmpegCommand;
streams.forEach((stream) => {
const index = getOuputStreamIndex(streams, stream);
if (stream.codec_type === 'subtitle') {
const dir = subs_dir;
let lang = stream.tags?.language ? stream.tags.language : 'und';
const format = stream.codec_name.toLowerCase();
// Ignore all subtitle formats that can't be converted to srt easily
if (
(format !== 'ass' && format !== 'subrip' && format !== 'srt')
|| (subtitle_languages.length !== 0 && !subtitle_languages.includes(lang))
) {
stream.removed = true;
} else {
// Add supported flags to subtitle file names to retain them
// eslint-disable-next-line no-prototype-builtins
if (stream.hasOwnProperty('disposition')) {
const def = stream.disposition.default === 1 ? '.default' : '';
const forced = stream.disposition.forced === 1 ? '.forced' : '';
const sdh = stream.disposition.hearing_impaired === 1 ? '.sdh' : '';
lang = `${lang}${def}${forced}${sdh}`;
}
args.deps.fsextra.ensureDirSync(dir);
stream.outputArgs.push('-c:s:0');
if (format === 'ass') {
stream.outputArgs.push('srt');
} else if (format === 'subrip' || format === 'srt') {
stream.outputArgs.push('copy');
}
stream.outputArgs.push(`${dir}/${index}.${lang}.srt`);
}
} else if (stream.codec_type === 'video') {
stream.outputArgs.push('-c:v');
stream.outputArgs.push('copy');
stream.outputArgs.push('-bsf:v');
stream.outputArgs.push('hevc_mp4toannexb');
} else {
stream.removed = true;
}
});
return {
outputFileObj: args.inputFileObj,
outputNumber: 1,
variables: args.variables,
};
};
export {
details,
plugin,
};