@remotion/openai-whisper 4.0.529 → 4.0.530

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.
@@ -0,0 +1,120 @@
1
+ // src/openai-whisper-api-to-captions.ts
2
+ import * as z from "zod/mini";
3
+ var escapeRegex = (text) => {
4
+ return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
5
+ };
6
+ var apostropheVariants = ["'", "‘", "’", "ʼ", "'"];
7
+ var apostropheVariantRegex = `[${apostropheVariants.map(escapeRegex).join("")}]`;
8
+ var escapeWordForRegex = (text) => {
9
+ return Array.from(text).map((character) => {
10
+ return apostropheVariants.includes(character) ? apostropheVariantRegex : escapeRegex(character);
11
+ }).join("");
12
+ };
13
+ var seconds = z.number().check(z.nonnegative());
14
+ var wordSchema = z.object({ word: z.string(), start: seconds, end: seconds });
15
+ var segmentSchema = z.object({
16
+ text: z.string(),
17
+ start: seconds,
18
+ end: seconds
19
+ });
20
+ var transcriptionSchema = z.object({
21
+ text: z.string(),
22
+ words: z.optional(z.array(wordSchema)),
23
+ task: z.optional(z.string())
24
+ });
25
+ var segmentsSchema = z.object({ segments: z.optional(z.array(segmentSchema)) });
26
+ var invalidTranscript = (issue) => {
27
+ const path = issue.path.reduce((acc, key) => typeof key === "number" ? `${acc}[${key}]` : `${acc}${acc ? "." : ""}${String(key)}`, "");
28
+ return new Error(`OpenAI Whisper ${path || "transcription"} ${/(^|\.)(start|end)$/.test(path) ? "must be a finite, non-negative number (seconds)" : `is invalid: ${issue.message}`}.`);
29
+ };
30
+ var openAiWhisperApiToCaptions = ({
31
+ transcription
32
+ }) => {
33
+ const captions = [];
34
+ const result = z.safeParse(transcriptionSchema, transcription);
35
+ if (!result.success) {
36
+ throw invalidTranscript(result.error.issues[0]);
37
+ }
38
+ const parsed = result.data;
39
+ if (parsed.task && parsed.task !== "transcribe" && !parsed.words?.length) {
40
+ throw new Error(`The transcription does need to be a "transcribe" task. The input you gave is "task": "${parsed.task}"`);
41
+ }
42
+ if (!parsed.words?.length) {
43
+ const segmentResult = z.safeParse(segmentsSchema, transcription);
44
+ if (!segmentResult.success) {
45
+ throw invalidTranscript(segmentResult.error.issues[0]);
46
+ }
47
+ if (!segmentResult.data.segments?.length) {
48
+ throw new Error('OpenAI Whisper: No timed captions found. Export verbose_json with timestamp_granularities: ["word"] or timed segments.');
49
+ }
50
+ for (const [index, segment] of segmentResult.data.segments.entries()) {
51
+ if (segment.end < segment.start) {
52
+ throw new Error(`OpenAI Whisper segments[${index}].end must not be earlier than start.`);
53
+ }
54
+ const startMs = segment.start * 1000;
55
+ const endMs = segment.end * 1000;
56
+ captions.push({
57
+ text: segment.text,
58
+ startMs,
59
+ endMs,
60
+ timestampMs: (startMs + endMs) / 2,
61
+ confidence: null
62
+ });
63
+ }
64
+ return { captions };
65
+ }
66
+ for (const [index, word] of parsed.words.entries()) {
67
+ if (word.end < word.start) {
68
+ throw new Error(`OpenAI Whisper words[${index}].end must not be earlier than start.`);
69
+ }
70
+ }
71
+ let remainingText = parsed.text;
72
+ for (let i = 0;i < parsed.words.length; i++) {
73
+ const word = parsed.words[i];
74
+ const firstWord = i === 0;
75
+ if (firstWord) {
76
+ word.word = word.word.trimStart();
77
+ }
78
+ const punctuation = `\\?,\\.\\%\\–\\!\\;\\:\\'\\"\\-\\_\\(\\)\\[\\]\\{\\}\\@\\#\\$\\^\\&\\*\\+\\=\\/\\|\\<\\>\\~\`\\u2018\\u2019\\u02bc\\uff07`;
79
+ const wordToMatch = word.word.replace(new RegExp(`^[${punctuation}]+`), "");
80
+ const match = new RegExp(`^([\\s?${punctuation}]{0,4})${escapeWordForRegex(wordToMatch)}([${punctuation}]{0,3})?`).exec(remainingText);
81
+ if (!match) {
82
+ throw new Error(`Unable to parse punctuation from OpenAI Whisper output. Could not find word "${word.word}" in text "${remainingText.slice(0, 100)}". File an issue under https://remotion.dev/issue and post the input for openAiWhisperApiToCaptions() to ask for a fix.`);
83
+ }
84
+ const foundText = match[0];
85
+ remainingText = remainingText.slice(foundText.length);
86
+ captions.push({
87
+ confidence: null,
88
+ endMs: word.end * 1000,
89
+ startMs: word.start * 1000,
90
+ text: foundText,
91
+ timestampMs: (word.start + word.end) / 2 * 1000
92
+ });
93
+ }
94
+ return { captions };
95
+ };
96
+ // src/is-openai-whisper-transcript.ts
97
+ import * as z2 from "zod/mini";
98
+ var withTask = z2.object({ task: z2.unknown() });
99
+ var withWords = z2.object({
100
+ text: z2.unknown(),
101
+ language: z2.unknown(),
102
+ words: z2.unknown()
103
+ });
104
+ var withDuration = z2.object({
105
+ text: z2.unknown(),
106
+ duration: z2.unknown(),
107
+ segments: z2.unknown()
108
+ });
109
+ var withSegments = z2.object({
110
+ text: z2.unknown(),
111
+ language: z2.unknown(),
112
+ segments: z2.unknown()
113
+ });
114
+ var isOpenAiWhisperTranscript = (input) => {
115
+ return z2.safeParse(withTask, input).success || z2.safeParse(withWords, input).success || z2.safeParse(withDuration, input).success || z2.safeParse(withSegments, input).success;
116
+ };
117
+ export {
118
+ openAiWhisperApiToCaptions,
119
+ isOpenAiWhisperTranscript
120
+ };
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export { OpenAiToCaptionsInput, OpenAiToCaptionsOutput, openAiWhisperApiToCaptions, } from './openai-whisper-api-to-captions';
2
2
  export { OpenAiVerboseTranscription } from './openai-format';
3
+ export { isOpenAiWhisperTranscript } from './is-openai-whisper-transcript';
package/dist/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.openAiWhisperApiToCaptions = void 0;
3
+ exports.isOpenAiWhisperTranscript = exports.openAiWhisperApiToCaptions = void 0;
4
4
  const openai_whisper_api_to_captions_1 = require("./openai-whisper-api-to-captions");
5
5
  Object.defineProperty(exports, "openAiWhisperApiToCaptions", { enumerable: true, get: function () { return openai_whisper_api_to_captions_1.openAiWhisperApiToCaptions; } });
6
+ const is_openai_whisper_transcript_1 = require("./is-openai-whisper-transcript");
7
+ Object.defineProperty(exports, "isOpenAiWhisperTranscript", { enumerable: true, get: function () { return is_openai_whisper_transcript_1.isOpenAiWhisperTranscript; } });
@@ -0,0 +1 @@
1
+ export declare const isOpenAiWhisperTranscript: (input: unknown) => boolean;
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.isOpenAiWhisperTranscript = void 0;
37
+ const z = __importStar(require("zod/mini"));
38
+ // Permissive signatures, not proof that the transcript is valid.
39
+ const withTask = z.object({ task: z.unknown() });
40
+ const withWords = z.object({
41
+ text: z.unknown(),
42
+ language: z.unknown(),
43
+ words: z.unknown(),
44
+ });
45
+ const withDuration = z.object({
46
+ text: z.unknown(),
47
+ duration: z.unknown(),
48
+ segments: z.unknown(),
49
+ });
50
+ const withSegments = z.object({
51
+ text: z.unknown(),
52
+ language: z.unknown(),
53
+ segments: z.unknown(),
54
+ });
55
+ const isOpenAiWhisperTranscript = (input) => {
56
+ return (z.safeParse(withTask, input).success ||
57
+ z.safeParse(withWords, input).success ||
58
+ z.safeParse(withDuration, input).success ||
59
+ z.safeParse(withSegments, input).success);
60
+ };
61
+ exports.isOpenAiWhisperTranscript = isOpenAiWhisperTranscript;
@@ -1,7 +1,6 @@
1
1
  import type { Caption } from '@remotion/captions';
2
- import type { OpenAiVerboseTranscription } from './openai-format';
3
2
  export type OpenAiToCaptionsInput = {
4
- transcription: OpenAiVerboseTranscription;
3
+ transcription: unknown;
5
4
  };
6
5
  export type OpenAiToCaptionsOutput = {
7
6
  captions: Caption[];
@@ -1,6 +1,40 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
3
36
  exports.openAiWhisperApiToCaptions = void 0;
37
+ const z = __importStar(require("zod/mini"));
4
38
  const escapeRegex = (text) => {
5
39
  return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
6
40
  };
@@ -15,17 +49,68 @@ const escapeWordForRegex = (text) => {
15
49
  })
16
50
  .join('');
17
51
  };
52
+ const seconds = z.number().check(z.nonnegative());
53
+ const wordSchema = z.object({ word: z.string(), start: seconds, end: seconds });
54
+ const segmentSchema = z.object({
55
+ text: z.string(),
56
+ start: seconds,
57
+ end: seconds,
58
+ });
59
+ const transcriptionSchema = z.object({
60
+ text: z.string(),
61
+ words: z.optional(z.array(wordSchema)),
62
+ task: z.optional(z.string()),
63
+ });
64
+ const segmentsSchema = z.object({ segments: z.optional(z.array(segmentSchema)) });
65
+ const invalidTranscript = (issue) => {
66
+ const path = issue.path.reduce((acc, key) => typeof key === 'number'
67
+ ? `${acc}[${key}]`
68
+ : `${acc}${acc ? '.' : ''}${String(key)}`, '');
69
+ return new Error(`OpenAI Whisper ${path || 'transcription'} ${/(^|\.)(start|end)$/.test(path) ? 'must be a finite, non-negative number (seconds)' : `is invalid: ${issue.message}`}.`);
70
+ };
18
71
  const openAiWhisperApiToCaptions = ({ transcription, }) => {
72
+ var _a, _b, _c;
19
73
  const captions = [];
20
- if (!transcription.words) {
21
- if (transcription.task && transcription.task !== 'transcribe') {
22
- throw new Error(`The transcription does need to be a "transcribe" task. The input you gave is "task": "${transcription.task}"`);
74
+ const result = z.safeParse(transcriptionSchema, transcription);
75
+ if (!result.success) {
76
+ throw invalidTranscript(result.error.issues[0]);
77
+ }
78
+ const parsed = result.data;
79
+ if (parsed.task && parsed.task !== 'transcribe' && !((_a = parsed.words) === null || _a === void 0 ? void 0 : _a.length)) {
80
+ throw new Error(`The transcription does need to be a "transcribe" task. The input you gave is "task": "${parsed.task}"`);
81
+ }
82
+ if (!((_b = parsed.words) === null || _b === void 0 ? void 0 : _b.length)) {
83
+ const segmentResult = z.safeParse(segmentsSchema, transcription);
84
+ if (!segmentResult.success) {
85
+ throw invalidTranscript(segmentResult.error.issues[0]);
86
+ }
87
+ if (!((_c = segmentResult.data.segments) === null || _c === void 0 ? void 0 : _c.length)) {
88
+ throw new Error('OpenAI Whisper: No timed captions found. Export verbose_json with timestamp_granularities: ["word"] or timed segments.');
89
+ }
90
+ for (const [index, segment] of segmentResult.data.segments.entries()) {
91
+ if (segment.end < segment.start) {
92
+ throw new Error(`OpenAI Whisper segments[${index}].end must not be earlier than start.`);
93
+ }
94
+ const startMs = segment.start * 1000;
95
+ const endMs = segment.end * 1000;
96
+ captions.push({
97
+ text: segment.text,
98
+ startMs,
99
+ endMs,
100
+ timestampMs: (startMs + endMs) / 2,
101
+ confidence: null,
102
+ });
103
+ }
104
+ return { captions };
105
+ }
106
+ for (const [index, word] of parsed.words.entries()) {
107
+ if (word.end < word.start) {
108
+ throw new Error(`OpenAI Whisper words[${index}].end must not be earlier than start.`);
23
109
  }
24
- throw new Error('The transcription does need to be been generated with `timestamp_granularities: ["word"]`');
25
110
  }
26
- let remainingText = transcription.text;
27
- for (let i = 0; i < transcription.words.length; i++) {
28
- const word = transcription.words[i];
111
+ let remainingText = parsed.text;
112
+ for (let i = 0; i < parsed.words.length; i++) {
113
+ const word = parsed.words[i];
29
114
  const firstWord = i === 0;
30
115
  // https://github.com/remotion-dev/remotion/issues/5031
31
116
  if (firstWord) {
package/package.json CHANGED
@@ -3,9 +3,10 @@
3
3
  "url": "https://github.com/remotion-dev/remotion/tree/main/packages/openai-whisper"
4
4
  },
5
5
  "name": "@remotion/openai-whisper",
6
- "version": "4.0.529",
6
+ "version": "4.0.530",
7
7
  "description": "Work with the output of the OpenAI Whisper API",
8
8
  "main": "dist/index.js",
9
+ "module": "dist/esm/index.mjs",
9
10
  "bugs": {
10
11
  "url": "https://github.com/remotion-dev/remotion/issues"
11
12
  },
@@ -14,17 +15,18 @@
14
15
  "format": "oxfmt src",
15
16
  "lint": "eslint src",
16
17
  "test": "bun test src",
17
- "make": "tsgo -d"
18
+ "make": "tsgo -d && bun --env-file=../.env.bundle bundle.ts"
18
19
  },
19
20
  "author": "Jonny Burger <jonny@remotion.dev>",
20
21
  "license": "MIT",
21
22
  "dependencies": {
22
- "@remotion/captions": "4.0.529"
23
+ "@remotion/captions": "4.0.530",
24
+ "zod": "4.5.4"
23
25
  },
24
26
  "peerDependencies": {},
25
27
  "devDependencies": {
26
28
  "openai": "4.67.1",
27
- "@remotion/eslint-config-internal": "4.0.529",
29
+ "@remotion/eslint-config-internal": "4.0.530",
28
30
  "eslint": "9.19.0",
29
31
  "@typescript/native-preview": "7.0.0-dev.20260217.1"
30
32
  },
@@ -34,5 +36,14 @@
34
36
  "publishConfig": {
35
37
  "access": "public"
36
38
  },
37
- "homepage": "https://www.remotion.dev/docs/openai-whisper"
39
+ "homepage": "https://www.remotion.dev/docs/openai-whisper",
40
+ "exports": {
41
+ "./package.json": "./package.json",
42
+ ".": {
43
+ "types": "./dist/index.d.ts",
44
+ "require": "./dist/index.js",
45
+ "module": "./dist/esm/index.mjs",
46
+ "import": "./dist/esm/index.mjs"
47
+ }
48
+ }
38
49
  }