@remotion/captions 4.0.514 → 4.0.515
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/index.mjs +200 -0
- package/package.json +14 -3
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// src/ensure-max-characters-per-line.ts
|
|
2
|
+
var splitWords = (inputCaptions) => {
|
|
3
|
+
const captions = [];
|
|
4
|
+
for (let i = 0;i < inputCaptions.length; i++) {
|
|
5
|
+
const w = inputCaptions[i];
|
|
6
|
+
const words = w.text.split(" ").filter(Boolean);
|
|
7
|
+
for (let j = 0;j < words.length; j++) {
|
|
8
|
+
const word = words[j];
|
|
9
|
+
captions.push({
|
|
10
|
+
text: j === 0 ? ` ${word}` : word,
|
|
11
|
+
startMs: w.startMs,
|
|
12
|
+
endMs: w.endMs,
|
|
13
|
+
confidence: w.confidence,
|
|
14
|
+
timestampMs: w.timestampMs
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return captions;
|
|
19
|
+
};
|
|
20
|
+
var ensureMaxCharactersPerLine = ({
|
|
21
|
+
captions,
|
|
22
|
+
maxCharsPerLine
|
|
23
|
+
}) => {
|
|
24
|
+
const splitted = splitWords(captions);
|
|
25
|
+
const segments = [];
|
|
26
|
+
let currentSegment = [];
|
|
27
|
+
for (let i = 0;i < splitted.length; i++) {
|
|
28
|
+
const w = splitted[i];
|
|
29
|
+
const remainingWords = splitted.slice(i + 1);
|
|
30
|
+
const filledCharactersInLine = currentSegment.map((s) => s.text.length).reduce((a, b) => a + b, 0);
|
|
31
|
+
const preventOrphanWord = remainingWords.length < 4 && remainingWords.length > 1 && filledCharactersInLine > maxCharsPerLine / 2;
|
|
32
|
+
if (filledCharactersInLine + w.text.length > maxCharsPerLine || preventOrphanWord) {
|
|
33
|
+
segments.push(currentSegment);
|
|
34
|
+
currentSegment = [];
|
|
35
|
+
}
|
|
36
|
+
currentSegment.push(w);
|
|
37
|
+
}
|
|
38
|
+
segments.push(currentSegment);
|
|
39
|
+
return { segments };
|
|
40
|
+
};
|
|
41
|
+
// src/create-tiktok-style-captions.ts
|
|
42
|
+
var createTikTokStyleCaptions = ({
|
|
43
|
+
captions,
|
|
44
|
+
combineTokensWithinMilliseconds,
|
|
45
|
+
breakOnSilenceAfterMilliseconds
|
|
46
|
+
}) => {
|
|
47
|
+
const tikTokStyleCaptions = [];
|
|
48
|
+
let currentText = "";
|
|
49
|
+
let currentTokens = [];
|
|
50
|
+
let currentFrom = 0;
|
|
51
|
+
let currentTo = 0;
|
|
52
|
+
const add = () => {
|
|
53
|
+
tikTokStyleCaptions.push({
|
|
54
|
+
text: currentText.trimStart(),
|
|
55
|
+
startMs: currentFrom,
|
|
56
|
+
tokens: currentTokens,
|
|
57
|
+
durationMs: Infinity
|
|
58
|
+
});
|
|
59
|
+
if (tikTokStyleCaptions.length > 1) {
|
|
60
|
+
tikTokStyleCaptions[tikTokStyleCaptions.length - 2].durationMs = currentFrom - tikTokStyleCaptions[tikTokStyleCaptions.length - 2].startMs;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
captions.forEach((item, index) => {
|
|
64
|
+
const { text } = item;
|
|
65
|
+
const exceedsDuration = currentTo - currentFrom > combineTokensWithinMilliseconds;
|
|
66
|
+
const shouldBreakOnSilence = breakOnSilenceAfterMilliseconds !== undefined && currentText !== "" && item.startMs - currentTo >= breakOnSilenceAfterMilliseconds;
|
|
67
|
+
if (text.startsWith(" ") && (exceedsDuration || shouldBreakOnSilence)) {
|
|
68
|
+
if (currentText !== "") {
|
|
69
|
+
add();
|
|
70
|
+
}
|
|
71
|
+
currentText = text.trimStart();
|
|
72
|
+
currentTokens = [
|
|
73
|
+
{ text: currentText, fromMs: item.startMs, toMs: item.endMs }
|
|
74
|
+
].filter((t) => t.text !== "");
|
|
75
|
+
currentFrom = item.startMs;
|
|
76
|
+
currentTo = item.endMs;
|
|
77
|
+
} else {
|
|
78
|
+
if (currentText === "") {
|
|
79
|
+
currentFrom = item.startMs;
|
|
80
|
+
}
|
|
81
|
+
currentText += text;
|
|
82
|
+
currentText = currentText.trimStart();
|
|
83
|
+
if (text.trim() !== "") {
|
|
84
|
+
currentTokens.push({
|
|
85
|
+
text: currentTokens.length === 0 ? currentText.trimStart() : text,
|
|
86
|
+
fromMs: item.startMs,
|
|
87
|
+
toMs: item.endMs
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
currentTo = item.endMs;
|
|
91
|
+
}
|
|
92
|
+
if (index === captions.length - 1 && currentText !== "") {
|
|
93
|
+
add();
|
|
94
|
+
tikTokStyleCaptions[tikTokStyleCaptions.length - 1].durationMs = currentTo - tikTokStyleCaptions[tikTokStyleCaptions.length - 1].startMs;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
const lastPage = tikTokStyleCaptions[tikTokStyleCaptions.length - 1];
|
|
98
|
+
if (lastPage && lastPage.durationMs === Infinity) {
|
|
99
|
+
lastPage.durationMs = currentTo - lastPage.startMs;
|
|
100
|
+
}
|
|
101
|
+
return { pages: tikTokStyleCaptions };
|
|
102
|
+
};
|
|
103
|
+
// src/parse-srt.ts
|
|
104
|
+
function toSeconds(time) {
|
|
105
|
+
const [first, second, third] = time.split(":");
|
|
106
|
+
if (!first) {
|
|
107
|
+
throw new Error(`Invalid timestamp:${time}`);
|
|
108
|
+
}
|
|
109
|
+
if (!second) {
|
|
110
|
+
throw new Error(`Invalid timestamp:${time}`);
|
|
111
|
+
}
|
|
112
|
+
if (!third) {
|
|
113
|
+
throw new Error(`Invalid timestamp:${time}`);
|
|
114
|
+
}
|
|
115
|
+
const [seconds, millis] = third.split(",");
|
|
116
|
+
if (!seconds) {
|
|
117
|
+
throw new Error(`Invalid timestamp:${time}`);
|
|
118
|
+
}
|
|
119
|
+
if (!millis) {
|
|
120
|
+
throw new Error(`Invalid timestamp:${time}`);
|
|
121
|
+
}
|
|
122
|
+
return parseInt(first, 10) * 3600 + parseInt(second, 10) * 60 + parseInt(seconds, 10) + parseInt(millis, 10) / 1000;
|
|
123
|
+
}
|
|
124
|
+
var parseSrt = ({ input }) => {
|
|
125
|
+
const inputLines = input.split(`
|
|
126
|
+
`);
|
|
127
|
+
const captions = [];
|
|
128
|
+
for (let i = 0;i < inputLines.length; i++) {
|
|
129
|
+
const line = inputLines[i];
|
|
130
|
+
const nextLine = inputLines[i + 1];
|
|
131
|
+
if (line?.match(/([0-9]+)/) && nextLine?.includes(" --> ")) {
|
|
132
|
+
const nextLineSplit = nextLine.split(" --> ");
|
|
133
|
+
const start = toSeconds(nextLineSplit[0]);
|
|
134
|
+
const end = toSeconds(nextLineSplit[1]);
|
|
135
|
+
captions.push({
|
|
136
|
+
text: "",
|
|
137
|
+
startMs: start * 1000,
|
|
138
|
+
endMs: end * 1000,
|
|
139
|
+
confidence: 1,
|
|
140
|
+
timestampMs: (start + end) / 2 * 1000
|
|
141
|
+
});
|
|
142
|
+
} else if (line?.includes(" --> ")) {
|
|
143
|
+
continue;
|
|
144
|
+
} else if (line?.trim() === "") {
|
|
145
|
+
captions[captions.length - 1].text = captions[captions.length - 1].text.trim();
|
|
146
|
+
} else {
|
|
147
|
+
captions[captions.length - 1].text += line + `
|
|
148
|
+
`;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
captions: captions.map((l) => {
|
|
153
|
+
return {
|
|
154
|
+
...l,
|
|
155
|
+
text: l.text.trimEnd()
|
|
156
|
+
};
|
|
157
|
+
})
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
// src/serialize-srt.ts
|
|
161
|
+
var formatSingleSrtTimestamp = (timestamp) => {
|
|
162
|
+
const hours = Math.floor(timestamp / 3600000);
|
|
163
|
+
const minutes = Math.floor(timestamp % 3600000 / 60000);
|
|
164
|
+
const seconds = Math.floor(timestamp % 60000 / 1000);
|
|
165
|
+
const milliseconds = Math.floor(timestamp % 1000);
|
|
166
|
+
return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")},${String(milliseconds).padStart(3, "0")}`;
|
|
167
|
+
};
|
|
168
|
+
var formatSrtTimestamp = (startMs, endMs) => {
|
|
169
|
+
return `${formatSingleSrtTimestamp(startMs)} --> ${formatSingleSrtTimestamp(endMs)}`;
|
|
170
|
+
};
|
|
171
|
+
var serializeSrt = ({ lines }) => {
|
|
172
|
+
let currentIndex = 0;
|
|
173
|
+
return lines.map((s) => {
|
|
174
|
+
currentIndex++;
|
|
175
|
+
if (s.length === 0) {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
const firstTimestamp = s[0].startMs;
|
|
179
|
+
const lastTimestamp = s[s.length - 1].endMs;
|
|
180
|
+
return [
|
|
181
|
+
currentIndex,
|
|
182
|
+
formatSrtTimestamp(firstTimestamp, lastTimestamp),
|
|
183
|
+
s.map((caption) => caption.text).join("")
|
|
184
|
+
].join(`
|
|
185
|
+
`);
|
|
186
|
+
}).filter(Boolean).join(`
|
|
187
|
+
|
|
188
|
+
`);
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
// src/index.ts
|
|
192
|
+
var CaptionsInternals = {
|
|
193
|
+
ensureMaxCharactersPerLine
|
|
194
|
+
};
|
|
195
|
+
export {
|
|
196
|
+
serializeSrt,
|
|
197
|
+
parseSrt,
|
|
198
|
+
createTikTokStyleCaptions,
|
|
199
|
+
CaptionsInternals
|
|
200
|
+
};
|
package/package.json
CHANGED
|
@@ -3,9 +3,11 @@
|
|
|
3
3
|
"url": "https://github.com/remotion-dev/remotion/tree/main/packages/captions"
|
|
4
4
|
},
|
|
5
5
|
"name": "@remotion/captions",
|
|
6
|
-
"version": "4.0.
|
|
6
|
+
"version": "4.0.515",
|
|
7
7
|
"description": "Primitives for dealing with captions",
|
|
8
8
|
"main": "dist/index.js",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"module": "dist/esm/index.mjs",
|
|
9
11
|
"bugs": {
|
|
10
12
|
"url": "https://github.com/remotion-dev/remotion/issues"
|
|
11
13
|
},
|
|
@@ -14,20 +16,29 @@
|
|
|
14
16
|
"format": "oxfmt src",
|
|
15
17
|
"lint": "eslint src",
|
|
16
18
|
"test": "bun test src",
|
|
17
|
-
"make": "tsgo -d"
|
|
19
|
+
"make": "tsgo -d && bun --env-file=../.env.bundle bundle.ts"
|
|
18
20
|
},
|
|
19
21
|
"author": "Jonny Burger <jonny@remotion.dev>",
|
|
20
22
|
"license": "MIT",
|
|
21
23
|
"dependencies": {},
|
|
22
24
|
"peerDependencies": {},
|
|
23
25
|
"devDependencies": {
|
|
24
|
-
"@remotion/eslint-config-internal": "4.0.
|
|
26
|
+
"@remotion/eslint-config-internal": "4.0.515",
|
|
25
27
|
"eslint": "9.19.0",
|
|
26
28
|
"@typescript/native-preview": "7.0.0-dev.20260217.1"
|
|
27
29
|
},
|
|
28
30
|
"keywords": [
|
|
29
31
|
"remotion"
|
|
30
32
|
],
|
|
33
|
+
"exports": {
|
|
34
|
+
"./package.json": "./package.json",
|
|
35
|
+
".": {
|
|
36
|
+
"types": "./dist/index.d.ts",
|
|
37
|
+
"module": "./dist/esm/index.mjs",
|
|
38
|
+
"import": "./dist/esm/index.mjs",
|
|
39
|
+
"require": "./dist/index.js"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
31
42
|
"publishConfig": {
|
|
32
43
|
"access": "public"
|
|
33
44
|
},
|