🎥 Make videos programmatically with React
openai
Jonny Burger5d73458a1d9d
diff --git a/packages/ai-improvements/package.json b/packages/ai-improvements/package.jsonindex bce8214f75..60682b48ba 100644--- a/packages/ai-improvements/package.json+++ b/packages/ai-improvements/package.json@@ -4,7 +4,7 @@},"name": "ai-improvements","dependencies": {- "openai": "^4.18.0"+ "openai": "4.67.1"},"private": true,"version": "4.0.216"diff --git a/packages/docs/components/TableOfContents/api.tsx b/packages/docs/components/TableOfContents/api.tsxindex 57ef4f7594..840add9d9f 100644--- a/packages/docs/components/TableOfContents/api.tsx+++ b/packages/docs/components/TableOfContents/api.tsx@@ -5,6 +5,7 @@ import {TableOfContents as EnableScssTableOfContents} from '../../docs/enable-scimport {TableOfContents as FontsTableOfContents} from '../../docs/fonts-api/TableOfContents';import {TableOfContents as InstallWhisperCppTableOfContents} from '../../docs/install-whisper-cpp/install-whisper-cpp';import {TableOfContents as MediaParserTableOfContents} from '../../docs/media-parser/TableOfContents';+import {TableOfContents as OpenAiWhisperTableOfContents} from '../../docs/openai-whisper/TableOfContents';import {TableOfContents as SkiaTableOfContents} from '../../docs/skia/TableOfContents';import {TableOfContents as StudioTableOfContents} from '../../docs/studio/TableOfContents';import {TableOfContents as TailwindTableOfContents} from '../../docs/tailwind/TableOfContents';@@ -121,6 +122,9 @@ export const TableOfContents: React.FC = () => {<h2>@remotion/install-whisper-cpp</h2><p>Whisper.cpp installation and transcription</p><InstallWhisperCppTableOfContents />+ <h2>@remotion/openai-whisper</h2>+ <p>Work with transcriptions from OpenAI Whisper</p>+ <OpenAiWhisperTableOfContents /><h2>@remotion/animated-emoji</h2><p>Google Fonts Animated Emojis as Remotion Components</p><AnimatedEmojiTableOfContents />diff --git a/packages/docs/docs/captions/caption.mdx b/packages/docs/docs/captions/caption.mdxindex b0772b9710..008870688d 100644--- a/packages/docs/docs/captions/caption.mdx+++ b/packages/docs/docs/captions/caption.mdx@@ -9,16 +9,16 @@ crumb: '@remotion/captions'This is a simple data structure for a caption.```tsx twoslash-import {Caption} from '@remotion/captions';-// ^?+import type {Caption} from '@remotion/captions';+// ^?```By establishing a standard data structure, we allow many operations that involve captions to be interoperable:-- Transcribing (using the [`@remotion/install-whisper-cpp`](/docs/install-whisper-cpp) package)-- Formatting (for example, creating pages using [`createTikTokStyleCaptions()`](/docs/captions/create-tiktok-style-captions))-- Parsing (using the [`parseSrt()`](/docs/captions/parse-srt) function)-- Serializing (for example to a .srt file)+- **Transcribing**: Using the [`@remotion/install-whisper-cpp`](/docs/install-whisper-cpp) or [`@remotion/openai-whisper`](/docs/openai-whisper) packages+- **Formatting**: For example, creating pages using [`createTikTokStyleCaptions()`](/docs/captions/create-tiktok-style-captions)+- **Parsing**: Using the [`parseSrt()`](/docs/captions/parse-srt) function+- **Serializing**: For example to a `.srt` file using [`serializeSrt()`](/docs/captions/serialize-srt)## Fieldsdiff --git a/packages/docs/docs/openai-whisper/TableOfContents.tsx b/packages/docs/docs/openai-whisper/TableOfContents.tsxnew file mode 100644index 0000000000..0488a2a185--- /dev/null+++ b/packages/docs/docs/openai-whisper/TableOfContents.tsx@@ -0,0 +1,19 @@+import React from 'react';+import {Grid} from '../../components/TableOfContents/Grid';+import {TOCItem} from '../../components/TableOfContents/TOCItem';++export const TableOfContents: React.FC = () => {+ return (+ <div>+ <Grid>+ <TOCItem link="/docs/openai-whisper/openai-whisper-api-to-captions">+ <strong>{'openAiWhisperApiToCaptions()'}</strong>+ <div>+ Turn OpenAI Whisper API transcriptions into an array of{' '}+ <code>Caption</code>+ </div>+ </TOCItem>+ </Grid>+ </div>+ );+};diff --git a/packages/docs/docs/openai-whisper/index.mdx b/packages/docs/docs/openai-whisper/index.mdxnew file mode 100644index 0000000000..e29630f629--- /dev/null+++ b/packages/docs/docs/openai-whisper/index.mdx@@ -0,0 +1,28 @@+---+image: /generated/articles-docs-openai-whisper-index.png+title: '@remotion/openai-whisper'+crumb: 'Subtitle videos'+---++_Available from v4.0.217_++The `@remotion/openai-whisper` package provides utilities for working with the OpenAI Whisper API.++It provides a transformation function for converting the output of the OpenAI Whisper API into an array of [`Caption`](/docs/captions/caption) objects in order to make the captions compatible with other caption-related Remotion APIs.++import {TableOfContents} from './TableOfContents';++<Installation pkg="@remotion/openai-whisper" />++## APIs++<TableOfContents />++## License++MIT++## See also++- [`@remotion/captions`](/docs/captions)+- [`@remotion/install-whisper-cpp`](/docs/install-whisper-cpp)diff --git a/packages/docs/docs/openai-whisper/openai-whisper-api-to-captions.mdx b/packages/docs/docs/openai-whisper/openai-whisper-api-to-captions.mdxnew file mode 100644index 0000000000..1877fb9ed0--- /dev/null+++ b/packages/docs/docs/openai-whisper/openai-whisper-api-to-captions.mdx@@ -0,0 +1,41 @@+---+image: /generated/articles-docs-openai-whisper-openai-whisper-api-to-captions.png+title: openAiWhisperApiToCaptions()+crumb: '@remotion/openai-whisper'+---++# openAiWhisperApiToCaptions()<AvailableFrom v="4.0.217"/>++Turns the output from [`openai.audio.transcriptions.create`](https://platform.openai.com/docs/guides/speech-to-text/transcriptions) from the [openai](https://npm.im/openai) package into an array of [`Caption`](/docs/captions/caption) objects.++This package performs processing on the captions in order to retain the punctuation in the words, which is not by default included in the OpenAI response.++This function can be used in any JavaScript environment, but you should not use the OpenAI API in the browser because your API key will be exposed to the browser.++```tsx twoslash title="Example usage"+// @module: ESNext+// @target: ESNext++import fs from 'fs';+import {OpenAI} from 'openai';+import {openAiWhisperApiToCaptions} from '@remotion/openai-whisper';++const openai = new OpenAI();++const transcription = await openai.audio.transcriptions.create({+ file: fs.createReadStream('audio.mp3'),+ model: 'whisper-1',+ response_format: 'verbose_json',+ prompt: 'Hello, welcome to my lecture.',+ timestamp_granularities: ['word'],+});++const {captions} = openAiWhisperApiToCaptions({transcription});+```++## See also++- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/openai-whisper/src/openai-whisper-api-to-captions.ts)+- [`@remotion/openai-whisper`](/docs/openai-whisper)+- [`@remotion/install-whisper-cpp`](/docs/install-whisper-cpp)+- [`@remotion/captions`](/docs/captions)diff --git a/packages/docs/package.json b/packages/docs/package.jsonindex efd1e9dac2..68986852ce 100644--- a/packages/docs/package.json+++ b/packages/docs/package.json@@ -51,6 +51,7 @@"@remotion/noise": "workspace:*","@remotion/paths": "workspace:*","@remotion/player": "workspace:*",+ "@remotion/openai-whisper": "workspace:*","@remotion/preload": "workspace:*","@remotion/renderer": "workspace:*","@remotion/rive": "workspace:*",@@ -94,6 +95,7 @@"@types/unist": "^2.0.0","@types/node": "20.12.14","lodash.sortby": "^4.7.0",- "url-loader": "^4.1.1"+ "url-loader": "^4.1.1",+ "openai": "4.67.1"}}diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex 5717cb42f4..59c23d57ae 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -596,6 +596,15 @@ module.exports = {'install-whisper-cpp/convert-to-captions',],},+ {+ type: 'category',+ label: '@remotion/openai-whisper',+ link: {+ type: 'doc',+ id: 'openai-whisper/index',+ },+ items: ['openai-whisper/openai-whisper-api-to-captions'],+ },{type: 'category',label: '@remotion/fonts',diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex 2f51eda26e..6f45dff3c8 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -398,6 +398,13 @@ export const articles = [compId: 'articles-docs-renderer-render-frames',crumb: '@remotion/renderer',},+ {+ id: 'metadata',+ title: 'Setting video metadata',+ relativePath: 'docs/metadata.mdx',+ compId: 'articles-docs-metadata',+ crumb: 'Techniques',+ },{id: 'bundle',title: 'bundle()',@@ -790,6 +797,20 @@ export const articles = [compId: 'articles-docs-get-image-dimensions',crumb: '@remotion/media-utils',},+ {+ id: 'openai-whisper/index',+ title: '@remotion/openai-whisper',+ relativePath: 'docs/openai-whisper/index.mdx',+ compId: 'articles-docs-openai-whisper-index',+ crumb: 'Subtitle videos',+ },+ {+ id: 'openai-whisper/openai-whisper-api-to-captions',+ title: 'openAiWhisperApiToCaptions()',+ relativePath: 'docs/openai-whisper/openai-whisper-api-to-captions.mdx',+ compId: 'articles-docs-openai-whisper-openai-whisper-api-to-captions',+ crumb: '@remotion/openai-whisper',+ },{id: 'cancel-render',title: 'cancelRender()',@@ -1633,13 +1654,6 @@ export const articles = [compId: 'articles-docs-miscellaneous-chrome-headless-shell',crumb: 'FAQ',},- {- id: 'miscellaneous/render-in-browser',- title: 'Can I render videos in the browser?',- relativePath: 'docs/miscellaneous/render-in-browser.mdx',- compId: 'articles-docs-miscellaneous-render-in-browser',- crumb: 'Techniques',- },{id: 'miscellaneous/render-in-browser',title: 'Can I render videos in the browser?',diff --git a/packages/docs/static/generated/articles-docs-openai-whisper-index.png b/packages/docs/static/generated/articles-docs-openai-whisper-index.pngnew file mode 100644index 0000000000..f4b1e445e4Binary files /dev/null and b/packages/docs/static/generated/articles-docs-openai-whisper-index.png differdiff --git a/packages/docs/static/generated/articles-docs-openai-whisper-openai-whisper-api-to-captions.png b/packages/docs/static/generated/articles-docs-openai-whisper-openai-whisper-api-to-captions.pngnew file mode 100644index 0000000000..ac0ce21d21Binary files /dev/null and b/packages/docs/static/generated/articles-docs-openai-whisper-openai-whisper-api-to-captions.png differdiff --git a/packages/openai-whisper/src/index.ts b/packages/openai-whisper/src/index.tsindex 1aac297143..11916012c8 100644--- a/packages/openai-whisper/src/index.ts+++ b/packages/openai-whisper/src/index.ts@@ -1,7 +1,7 @@export {OpenAiToCaptionsInput,OpenAiToCaptionsOutput,- openAiWhisperApiToCaptions as openAiToCaptions,+ openAiWhisperApiToCaptions,} from './openai-whisper-api-to-captions';export {OpenAiVerboseTranscription} from './openai-format';diff --git a/packages/openai-whisper/src/openai-whisper-api-to-captions.ts b/packages/openai-whisper/src/openai-whisper-api-to-captions.tsindex cffc1d91ea..8d46f4fa78 100644--- a/packages/openai-whisper/src/openai-whisper-api-to-captions.ts+++ b/packages/openai-whisper/src/openai-whisper-api-to-captions.ts@@ -13,9 +13,9 @@ export const openAiWhisperApiToCaptions = ({transcription,}: OpenAiToCaptionsInput): OpenAiToCaptionsOutput => {let captions: Caption[] = [];- if (!transcription.words || !transcription.segments) {+ if (!transcription.words) {throw new Error(- 'The transcription does need to be been generated with `timestamp_granularities: ["word", "segment"]`',+ 'The transcription does need to be been generated with `timestamp_granularities: ["word"]`',);}diff --git a/packages/openai-whisper/src/test/get-and-convert.test.ts b/packages/openai-whisper/src/test/get-and-convert.test.tsindex a85a317931..2ffaa36f92 100644--- a/packages/openai-whisper/src/test/get-and-convert.test.ts+++ b/packages/openai-whisper/src/test/get-and-convert.test.ts@@ -101,7 +101,7 @@ if (!process.env.CI) {model: 'whisper-1',response_format: 'verbose_json',prompt: 'Hello, welcome to my lecture.',- timestamp_granularities: ['word', 'segment'],+ timestamp_granularities: ['word'],});const {captions} = openAiWhisperApiToCaptions({transcription});expect(captions.length).toBeGreaterThan(400);diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 2858d23397..99636f550c 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -52,8 +52,8 @@ importers:packages/ai-improvements:dependencies:openai:- specifier: ^4.18.0- version: 4.38.2+ specifier: 4.67.1+ version: 4.67.1([email protected])packages/animated-emoji:dependencies:@@ -564,6 +564,9 @@ importers:'@remotion/noise':specifier: workspace:*version: link:../noise+ '@remotion/openai-whisper':+ specifier: workspace:*+ version: link:../openai-whisper'@remotion/paths':specifier: workspace:*version: link:../paths@@ -688,6 +691,9 @@ importers:lodash.sortby:specifier: ^4.7.0version: 4.7.0+ openai:+ specifier: 4.67.1+ version: 4.67.1([email protected])url-loader:specifier: ^4.1.1version: 4.1.1([email protected])([email protected])@@ -1295,7 +1301,7 @@ importers:devDependencies:openai:specifier: 4.67.1- version: 4.67.1+ version: 4.67.1([email protected])packages/paths: {}@@ -22666,23 +22672,7 @@ packages:is-wsl: 2.2.0dev: false- resolution: {integrity: sha512-M16ehj0D84Gjq5cjvBzXRb5X+UvtWlxPDRAWAWMC0EN+6nHqnULIn5fWWeiexDPup25FeSZYv/ldp8KefcZVJQ==}- hasBin: true- dependencies:- '@types/node': 18.14.6- '@types/node-fetch': 2.6.11- abort-controller: 3.0.0- agentkeepalive: 4.5.0- form-data-encoder: 1.7.2- formdata-node: 4.4.1- node-fetch: 2.6.9- web-streams-polyfill: 3.3.3- transitivePeerDependencies:- - encoding- dev: false-resolution: {integrity: sha512-2YbRFy6qaYRJabK2zLMn4txrB2xBy0KP5g/eoqeSPTT31mIJMnkT75toagvfE555IKa2RdrzJrZwdDsUipsAMw==}hasBin: truepeerDependencies:@@ -22698,9 +22688,9 @@ packages:form-data-encoder: 1.7.2formdata-node: 4.4.1node-fetch: 2.6.9+ zod: 3.22.3transitivePeerDependencies:- encoding- dev: trueresolution: {integrity: sha512-ur5UIdyw5Y7yEj9wLzhqXiy6GZ3Mwx0yGI+5sMn2r0N0v3cKJvUmFH5yPP+WXh9e0xfyzyJX95D8l088DNFj7A==}