@junwan666/remotion-captions 4.0.448-zh.0

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/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # @remotion/captions
2
+
3
+ Primitives for dealing with captions
4
+
5
+ [![NPM Downloads](https://img.shields.io/npm/dm/@remotion/captions.svg?style=flat&color=black&label=Downloads)](https://npmcharts.com/compare/@remotion/captions?minimal=true)
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @remotion/captions --save-exact
11
+ ```
12
+
13
+ When installing a Remotion package, make sure to align the version of all `remotion` and `@remotion/*` packages to the same version.
14
+ Remove the `^` character from the version number to use the exact version.
15
+
16
+ ## Usage
17
+
18
+ See the [documentation](https://remotion.dev/docs/captions/api) for more information.
@@ -0,0 +1,7 @@
1
+ export type Caption = {
2
+ text: string;
3
+ startMs: number;
4
+ endMs: number;
5
+ timestampMs: number | null;
6
+ confidence: number | null;
7
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,20 @@
1
+ import type { Caption } from './caption';
2
+ export type TikTokToken = {
3
+ text: string;
4
+ fromMs: number;
5
+ toMs: number;
6
+ };
7
+ export type TikTokPage = {
8
+ text: string;
9
+ startMs: number;
10
+ tokens: TikTokToken[];
11
+ durationMs: number;
12
+ };
13
+ export type CreateTikTokStyleCaptionsInput = {
14
+ captions: Caption[];
15
+ combineTokensWithinMilliseconds: number;
16
+ };
17
+ export type CreateTikTokStyleCaptionsOutput = {
18
+ pages: TikTokPage[];
19
+ };
20
+ export declare const createTikTokStyleCaptions: ({ captions, combineTokensWithinMilliseconds, }: CreateTikTokStyleCaptionsInput) => CreateTikTokStyleCaptionsOutput;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createTikTokStyleCaptions = void 0;
4
+ const createTikTokStyleCaptions = ({ captions, combineTokensWithinMilliseconds, }) => {
5
+ const tikTokStyleCaptions = [];
6
+ let currentText = '';
7
+ let currentTokens = [];
8
+ let currentFrom = 0;
9
+ let currentTo = 0;
10
+ const add = () => {
11
+ tikTokStyleCaptions.push({
12
+ text: currentText.trimStart(),
13
+ startMs: currentFrom,
14
+ tokens: currentTokens,
15
+ durationMs: Infinity,
16
+ });
17
+ if (tikTokStyleCaptions.length > 1) {
18
+ tikTokStyleCaptions[tikTokStyleCaptions.length - 2].durationMs =
19
+ currentFrom -
20
+ tikTokStyleCaptions[tikTokStyleCaptions.length - 2].startMs;
21
+ }
22
+ };
23
+ captions.forEach((item, index) => {
24
+ const { text } = item;
25
+ // If text starts with a space, push the currentText (if it exists) and start a new one
26
+ if (text.startsWith(' ') &&
27
+ currentTo - currentFrom > combineTokensWithinMilliseconds) {
28
+ if (currentText !== '') {
29
+ add();
30
+ }
31
+ // Start a new sentence
32
+ currentText = text.trimStart();
33
+ currentTokens = [
34
+ { text: currentText, fromMs: item.startMs, toMs: item.endMs },
35
+ ].filter((t) => t.text !== '');
36
+ currentFrom = item.startMs;
37
+ currentTo = item.endMs;
38
+ }
39
+ else {
40
+ // Continuation or start of a new sentence without leading space
41
+ if (currentText === '') {
42
+ // It's the start of the document or after a sentence that started with a space
43
+ currentFrom = item.startMs;
44
+ }
45
+ currentText += text;
46
+ currentText = currentText.trimStart();
47
+ if (text.trim() !== '') {
48
+ currentTokens.push({
49
+ text: currentTokens.length === 0 ? currentText.trimStart() : text,
50
+ fromMs: item.startMs,
51
+ toMs: item.endMs,
52
+ });
53
+ }
54
+ currentTo = item.endMs;
55
+ }
56
+ // Ensure the last sentence is added
57
+ if (index === captions.length - 1 && currentText !== '') {
58
+ add();
59
+ tikTokStyleCaptions[tikTokStyleCaptions.length - 1].durationMs =
60
+ currentTo - tikTokStyleCaptions[tikTokStyleCaptions.length - 1].startMs;
61
+ }
62
+ });
63
+ return { pages: tikTokStyleCaptions };
64
+ };
65
+ exports.createTikTokStyleCaptions = createTikTokStyleCaptions;
@@ -0,0 +1,9 @@
1
+ import type { Caption } from './caption';
2
+ export type EnsureMaxCharactersPerLineInput = {
3
+ captions: Caption[];
4
+ maxCharsPerLine: number;
5
+ };
6
+ export type EnsureMaxCharactersPerLineOutput = {
7
+ segments: Caption[][];
8
+ };
9
+ export declare const ensureMaxCharactersPerLine: ({ captions, maxCharsPerLine, }: EnsureMaxCharactersPerLineInput) => EnsureMaxCharactersPerLineOutput;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ensureMaxCharactersPerLine = void 0;
4
+ const splitWords = (inputCaptions) => {
5
+ const captions = [];
6
+ for (let i = 0; i < inputCaptions.length; i++) {
7
+ const w = inputCaptions[i];
8
+ const words = w.text.split(' ');
9
+ for (let j = 0; j < words.length; j++) {
10
+ const word = words[j];
11
+ captions.push({
12
+ text: j === 0 ? ` ${word}` : word,
13
+ startMs: w.startMs,
14
+ endMs: w.endMs,
15
+ confidence: w.confidence,
16
+ timestampMs: w.timestampMs,
17
+ });
18
+ }
19
+ }
20
+ return captions;
21
+ };
22
+ const ensureMaxCharactersPerLine = ({ captions, maxCharsPerLine, }) => {
23
+ const splitted = splitWords(captions);
24
+ const segments = [];
25
+ let currentSegment = [];
26
+ for (let i = 0; i < splitted.length; i++) {
27
+ const w = splitted[i];
28
+ const remainingWords = splitted.slice(i + 1);
29
+ const filledCharactersInLine = currentSegment
30
+ .map((s) => s.text.length)
31
+ .reduce((a, b) => a + b, 0);
32
+ const preventOrphanWord = remainingWords.length < 4 &&
33
+ remainingWords.length > 1 &&
34
+ filledCharactersInLine > maxCharsPerLine / 2;
35
+ if (filledCharactersInLine + w.text.length > maxCharsPerLine ||
36
+ preventOrphanWord) {
37
+ segments.push(currentSegment);
38
+ currentSegment = [];
39
+ }
40
+ currentSegment.push(w);
41
+ }
42
+ segments.push(currentSegment);
43
+ return { segments };
44
+ };
45
+ exports.ensureMaxCharactersPerLine = ensureMaxCharactersPerLine;
@@ -0,0 +1,8 @@
1
+ export { Caption } from './caption';
2
+ export { createTikTokStyleCaptions, CreateTikTokStyleCaptionsInput, CreateTikTokStyleCaptionsOutput, TikTokPage, TikTokToken, } from './create-tiktok-style-captions';
3
+ export { EnsureMaxCharactersPerLineInput, EnsureMaxCharactersPerLineOutput, } from './ensure-max-characters-per-line';
4
+ export { parseSrt, ParseSrtInput, ParseSrtOutput } from './parse-srt';
5
+ export { serializeSrt, SerializeSrtInput } from './serialize-srt';
6
+ export declare const CaptionsInternals: {
7
+ ensureMaxCharactersPerLine: ({ captions, maxCharsPerLine, }: import("./ensure-max-characters-per-line").EnsureMaxCharactersPerLineInput) => import("./ensure-max-characters-per-line").EnsureMaxCharactersPerLineOutput;
8
+ };
package/dist/index.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CaptionsInternals = exports.serializeSrt = exports.parseSrt = exports.createTikTokStyleCaptions = void 0;
4
+ const ensure_max_characters_per_line_1 = require("./ensure-max-characters-per-line");
5
+ const create_tiktok_style_captions_1 = require("./create-tiktok-style-captions");
6
+ Object.defineProperty(exports, "createTikTokStyleCaptions", { enumerable: true, get: function () { return create_tiktok_style_captions_1.createTikTokStyleCaptions; } });
7
+ const parse_srt_1 = require("./parse-srt");
8
+ Object.defineProperty(exports, "parseSrt", { enumerable: true, get: function () { return parse_srt_1.parseSrt; } });
9
+ const serialize_srt_1 = require("./serialize-srt");
10
+ Object.defineProperty(exports, "serializeSrt", { enumerable: true, get: function () { return serialize_srt_1.serializeSrt; } });
11
+ exports.CaptionsInternals = {
12
+ ensureMaxCharactersPerLine: ensure_max_characters_per_line_1.ensureMaxCharactersPerLine,
13
+ };
@@ -0,0 +1,8 @@
1
+ import type { Caption } from './caption';
2
+ export type ParseSrtInput = {
3
+ input: string;
4
+ };
5
+ export type ParseSrtOutput = {
6
+ captions: Caption[];
7
+ };
8
+ export declare const parseSrt: ({ input }: ParseSrtInput) => ParseSrtOutput;
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseSrt = void 0;
4
+ function toSeconds(time) {
5
+ const [first, second, third] = time.split(':');
6
+ if (!first) {
7
+ throw new Error(`Invalid timestamp:${time}`);
8
+ }
9
+ if (!second) {
10
+ throw new Error(`Invalid timestamp:${time}`);
11
+ }
12
+ if (!third) {
13
+ throw new Error(`Invalid timestamp:${time}`);
14
+ }
15
+ const [seconds, millis] = third.split(',');
16
+ if (!seconds) {
17
+ throw new Error(`Invalid timestamp:${time}`);
18
+ }
19
+ if (!millis) {
20
+ throw new Error(`Invalid timestamp:${time}`);
21
+ }
22
+ return (parseInt(first, 10) * 3600 +
23
+ parseInt(second, 10) * 60 +
24
+ parseInt(seconds, 10) +
25
+ parseInt(millis, 10) / 1000);
26
+ }
27
+ const parseSrt = ({ input }) => {
28
+ const inputLines = input.split('\n');
29
+ const captions = [];
30
+ for (let i = 0; i < inputLines.length; i++) {
31
+ const line = inputLines[i];
32
+ const nextLine = inputLines[i + 1];
33
+ if ((line === null || line === void 0 ? void 0 : line.match(/([0-9]+)/)) && (nextLine === null || nextLine === void 0 ? void 0 : nextLine.includes(' --> '))) {
34
+ const nextLineSplit = nextLine.split(' --> ');
35
+ const start = toSeconds(nextLineSplit[0]);
36
+ const end = toSeconds(nextLineSplit[1]);
37
+ captions.push({
38
+ text: '',
39
+ startMs: start * 1000,
40
+ endMs: end * 1000,
41
+ confidence: 1,
42
+ timestampMs: ((start + end) / 2) * 1000,
43
+ });
44
+ }
45
+ else if (line === null || line === void 0 ? void 0 : line.includes(' --> ')) {
46
+ continue;
47
+ }
48
+ else if ((line === null || line === void 0 ? void 0 : line.trim()) === '') {
49
+ captions[captions.length - 1].text = captions[captions.length - 1].text.trim();
50
+ }
51
+ else {
52
+ captions[captions.length - 1].text += line + '\n';
53
+ }
54
+ }
55
+ return {
56
+ captions: captions.map((l) => {
57
+ return {
58
+ ...l,
59
+ text: l.text.trimEnd(),
60
+ };
61
+ }),
62
+ };
63
+ };
64
+ exports.parseSrt = parseSrt;
@@ -0,0 +1,5 @@
1
+ import type { Caption } from './caption';
2
+ export type SerializeSrtInput = {
3
+ lines: Caption[][];
4
+ };
5
+ export declare const serializeSrt: ({ lines }: SerializeSrtInput) => string;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.serializeSrt = void 0;
4
+ const formatSingleSrtTimestamp = (timestamp) => {
5
+ const hours = Math.floor(timestamp / 3600000);
6
+ const minutes = Math.floor((timestamp % 3600000) / 60000);
7
+ const seconds = Math.floor((timestamp % 60000) / 1000);
8
+ const milliseconds = Math.floor(timestamp % 1000);
9
+ return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')},${String(milliseconds).padStart(3, '0')}`;
10
+ };
11
+ const formatSrtTimestamp = (startMs, endMs) => {
12
+ return `${formatSingleSrtTimestamp(startMs)} --> ${formatSingleSrtTimestamp(endMs)}`;
13
+ };
14
+ const serializeSrt = ({ lines }) => {
15
+ let currentIndex = 0;
16
+ return lines
17
+ .map((s) => {
18
+ currentIndex++;
19
+ if (s.length === 0) {
20
+ return null;
21
+ }
22
+ const firstTimestamp = s[0].startMs;
23
+ const lastTimestamp = s[s.length - 1].endMs;
24
+ return [
25
+ // Index
26
+ currentIndex,
27
+ formatSrtTimestamp(firstTimestamp, lastTimestamp),
28
+ // Text
29
+ s.map((caption) => caption.text).join(''),
30
+ ].join('\n');
31
+ })
32
+ .filter(Boolean)
33
+ .join('\n\n');
34
+ };
35
+ exports.serializeSrt = serializeSrt;
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "repository": {
3
+ "url": "https://github.com/JunWan666/remotion-zh/tree/main/packages/captions"
4
+ },
5
+ "name": "@junwan666/remotion-captions",
6
+ "version": "4.0.448-zh.0",
7
+ "description": "Primitives for dealing with captions",
8
+ "main": "dist/index.js",
9
+ "sideEffects": false,
10
+ "bugs": {
11
+ "url": "https://github.com/remotion-dev/remotion/issues"
12
+ },
13
+ "scripts": {
14
+ "formatting": "oxfmt src --check",
15
+ "format": "oxfmt src",
16
+ "lint": "eslint src",
17
+ "test": "bun test src",
18
+ "make": "tsgo -d"
19
+ },
20
+ "author": "Jonny Burger <jonny@remotion.dev>",
21
+ "license": "MIT",
22
+ "dependencies": {},
23
+ "peerDependencies": {},
24
+ "devDependencies": {
25
+ "@remotion/eslint-config-internal": "npm:@junwan666/remotion-eslint-config-internal@4.0.448-zh.0",
26
+ "eslint": "9.19.0",
27
+ "@typescript/native-preview": "7.0.0-dev.20260217.1"
28
+ },
29
+ "keywords": [
30
+ "remotion"
31
+ ],
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "homepage": "https://remotion.dev/docs/captions/api"
36
+ }