-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrt-processor.js
More file actions
153 lines (134 loc) · 4.51 KB
/
srt-processor.js
File metadata and controls
153 lines (134 loc) · 4.51 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
150
151
152
153
function timecodeToMilliseconds(timecode) {
const parts = timecode.split(":");
const secondsAndMillis = parts[2].split(",");
const hours = parseInt(parts[0], 10);
const minutes = parseInt(parts[1], 10);
const seconds = parseInt(secondsAndMillis[0], 10);
const milliseconds = parseInt(secondsAndMillis[1], 10);
return (hours * 3600 + minutes * 60 + seconds) * 1000 + milliseconds;
}
function processSegments(segments, variables, errors, warnings) {
let lastTimecodeLine = null;
return segments.map((segment, index) => {
// console.debug({ index, segment });
const lines = segment.split("\n");
const newIndex = index + 1;
let timecodeLine = lines[1] || "";
const originalTimecodeLine = timecodeLine;
const content = lines.slice(2).join("\n");
// Timecode validation
const timecodeRegex =
/\d{1,2}:\d{1,2}:\d{1,2},\d{3} --> \d{1,2}:\d{1,2}:\d{1,2},\d{3}/;
if (!timecodeRegex.test(timecodeLine)) {
const singleTimecodeRegex = /\d{1,2}:\d{1,2}:\d{1,2},\d{3}/;
if (singleTimecodeRegex.test(timecodeLine.trim())) {
if (index < segments.length - 1) {
const nextSegmentLines = segments[index + 1].split("\n");
const nextStartTime = nextSegmentLines[1].split(" --> ")[0];
if (nextStartTime) {
timecodeLine = `${timecodeLine.trim()} --> ${nextStartTime.trim()}`;
warnings.push(
`Segment ${newIndex}: Missing start or end time. Original: "${originalTimecodeLine}". Corrected to: "${timecodeLine}"`
);
} else {
errors.push(
`Segment ${newIndex}: Invalid timecode "${originalTimecodeLine}" and the next segment's timecode is also invalid.`
);
}
} else {
errors.push(
`Segment ${newIndex}: Missing an end time and it's the last segment. Timecode: "${originalTimecodeLine}"`
);
}
} else {
errors.push(
`Segment ${newIndex}: Invalid timecode format. Timecode: "${originalTimecodeLine}"`
);
}
}
const [startTime, endTime] = timecodeLine.split(" --> ");
if (startTime && endTime) {
const startTimeMs = timecodeToMilliseconds(startTime);
const endTimeMs = timecodeToMilliseconds(endTime);
if (startTimeMs >= endTimeMs) {
errors.push(
`Segment ${newIndex}: Invalid timecode (start >= end). Timecode: "${timecodeLine}"`
);
}
if (lastTimecodeLine) {
const prevEndTimeMs = timecodeToMilliseconds(
lastTimecodeLine.split(" --> ")[1]
);
if (prevEndTimeMs > startTimeMs) {
warnings.push(
`Segment ${newIndex}: Overlaps with the previous segment. Previous end time: ${
lastTimecodeLine.split(" --> ")[1]
}, current start time: ${startTime}`
);
}
}
}
lastTimecodeLine = timecodeLine;
return {
index: newIndex,
timecode: timecodeLine,
content: content,
};
});
}
function processSrt(data, variables = null) {
const errors = [];
const warnings = [];
let newVariablesCount = 0;
let substitutedCount = 0;
let unhandledVariablesCount = 0;
const segments = data
.replace(/\r\n/g, "\n")
.split(/\n\n+/)
.map((segment) => segment.trim());
const filteredSegments = segments.filter((segment) => segment);
const processedSegments = processSegments(
filteredSegments,
variables,
errors,
warnings
);
if (variables) {
processedSegments.forEach((segment) => {
const newContent = segment.content.replace(
/{{([^}]+)}}/g,
(match, variableName) => {
if (variables.hasOwnProperty(variableName)) {
// if variable is empty, don't replace it.
if (!variables[variableName]) {
unhandledVariablesCount++;
return match;
}
substitutedCount++;
return variables[variableName];
}
newVariablesCount++;
unhandledVariablesCount++;
variables[variableName] = "";
return match;
}
);
segment.content = newContent;
});
}
const outputContent = processedSegments
.map(
(segment) => `${segment.index}\n${segment.timecode}\n${segment.content}`
)
.join("\n\n");
return {
outputContent,
errors,
warnings,
newVariablesCount,
substitutedCount,
unhandledVariablesCount,
variables,
};
}
module.exports = { processSrt, timecodeToMilliseconds, processSegments };