Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Restrict caption import picker to transcript files (#11766)

Igor Samokhovets9e13615bd9ea
diff --git a/packages/example/e2e/captions-inspector.test.mts b/packages/example/e2e/captions-inspector.test.mts
index c90d9d52c3..a9e4c61df4 100644
--- a/packages/example/e2e/captions-inspector.test.mts
+++ b/packages/example/e2e/captions-inspector.test.mts
@@ -116,7 +116,7 @@ test.describe('captions inspector', () => {
await expect(defaultCaption).toBeEnabled();
const importCaptionsButton = page.getByRole('button', {
- name: 'Import captions',
+ name: 'Import transcript',
exact: true,
});
const importCaptionsInput = page.getByLabel('Import captions file');
@@ -212,7 +212,7 @@ test.describe('captions inspector', () => {
.getByText('<MissingCaptions>', {exact: true})
.first();
const importCaptionsButton = page.getByRole('button', {
- name: 'Import captions',
+ name: 'Import transcript',
exact: true,
});
await expect(async () => {
diff --git a/packages/studio/src/components/CaptionInspector.tsx b/packages/studio/src/components/CaptionInspector.tsx
index 7e635f5821..616440e34f 100644
--- a/packages/studio/src/components/CaptionInspector.tsx
+++ b/packages/studio/src/components/CaptionInspector.tsx
@@ -4,6 +4,7 @@ import {LIGHT_TEXT} from '../helpers/colors';
import {FOCUS_VISIBLE_ONLY_CLASS_NAME} from '../helpers/hoverable';
import {UploadIcon} from '../icons/upload';
import {SetSelectedModalContext} from '../state/modals';
+import {ActionTooltip} from './ActionTooltip';
import {CaptionTextEditor} from './CaptionTextEditor';
import {InlineAction} from './InlineAction';
import {CollapsibleInspectorSectionHeader} from './InspectorPanel/CollapsibleInspectorSectionHeader';
@@ -12,9 +13,12 @@ import {sectionHeaderEnd} from './InspectorPanel/styles';
import {showNotification} from './Notifications/NotificationCenter';
import {parseCaptionFile} from './parse-caption-file';
-const importTooltip = `Import captions
-
-Supports Remotion Caption[] JSON, SRT, ElevenLabs Speech-to-Text and segmented JSON, and OpenAI Whisper verbose JSON. Files are processed locally.`;
+const transcriptFileTypes = {
+ extensions: ['.json', '.srt'],
+ description:
+ 'Supports SRT, Remotion Caption[] JSON, ElevenLabs Speech-to-Text and segmented JSON, and OpenAI Whisper verbose JSON. Files are processed locally.',
+ importLabel: 'Import transcript file...',
+};
const readOnlyStatus: React.CSSProperties = {
color: LIGHT_TEXT,
@@ -108,6 +112,7 @@ export const CaptionInspector: React.FC<{
invocationTimestamp: Date.now(),
assetSelection: {
initialQuery: '',
+ fileTypes: transcriptFileTypes,
onSelectFile: () => fileInput.current?.click(),
onSelected: (asset) => {
return importCaptions({
@@ -144,22 +149,28 @@ export const CaptionInspector: React.FC<{
onChange={onFileSelected}
type="file"
/>
- <InlineAction
- aria-label="Import captions"
- aria-description={importTooltip}
- className={FOCUS_VISIBLE_ONLY_CLASS_NAME}
- disabled={isImporting}
- onClick={openCaptionSelection}
- renderAction={(color) => (
- <UploadIcon
- aria-hidden="true"
- color={color}
- focusable="false"
- style={{height: 14, width: 14}}
- />
- )}
- variant={null}
- />
+ <ActionTooltip
+ label="Import transcript"
+ shortcut={null}
+ delay={800}
+ dismissOnClick
+ >
+ <InlineAction
+ aria-label="Import transcript"
+ className={FOCUS_VISIBLE_ONLY_CLASS_NAME}
+ disabled={isImporting}
+ onClick={openCaptionSelection}
+ renderAction={(color) => (
+ <UploadIcon
+ aria-hidden="true"
+ color={color}
+ focusable="false"
+ style={{height: 14, width: 14}}
+ />
+ )}
+ variant={null}
+ />
+ </ActionTooltip>
</>
)}
{readOnly ? (
diff --git a/packages/studio/src/components/CaptionTextEditor.tsx b/packages/studio/src/components/CaptionTextEditor.tsx
index 1a05615c11..8808955868 100644
--- a/packages/studio/src/components/CaptionTextEditor.tsx
+++ b/packages/studio/src/components/CaptionTextEditor.tsx
@@ -9,6 +9,7 @@ import {
} from '../helpers/colors';
import {FOCUS_VISIBLE_ONLY_CLASS_NAME} from '../helpers/hoverable';
import {EnterIcon} from '../icons/enter';
+import {ActionTooltip} from './ActionTooltip';
import {InlineAction} from './InlineAction';
import {RemotionInput} from './NewComposition/RemInput';
@@ -215,25 +216,36 @@ export const CaptionTextEditor: React.FC<{
}}
value={caption.text}
/>
- <InlineAction
- aria-pressed={hasPageBreakAfter}
- className={FOCUS_VISIBLE_ONLY_CLASS_NAME}
- disabled={readOnly}
- onClick={() => {
- updatePageBreakAfter(index, !hasPageBreakAfter);
- commitPending();
- }}
- renderAction={(color) => (
- <EnterIcon
- aria-hidden="true"
- color={hasPageBreakAfter ? BLUE : color}
- focusable="false"
- style={{height: 16, width: 16}}
- />
- )}
- aria-label={pageBreakTitle}
- variant={null}
- />
+ <ActionTooltip
+ label={
+ hasPageBreakAfter
+ ? 'Remove break after this'
+ : 'Break after this'
+ }
+ shortcut={null}
+ delay={800}
+ dismissOnClick
+ >
+ <InlineAction
+ aria-pressed={hasPageBreakAfter}
+ className={FOCUS_VISIBLE_ONLY_CLASS_NAME}
+ disabled={readOnly}
+ onClick={() => {
+ updatePageBreakAfter(index, !hasPageBreakAfter);
+ commitPending();
+ }}
+ renderAction={(color) => (
+ <EnterIcon
+ aria-hidden="true"
+ color={hasPageBreakAfter ? BLUE : color}
+ focusable="false"
+ style={{height: 16, width: 16}}
+ />
+ )}
+ aria-label={pageBreakTitle}
+ variant={null}
+ />
+ </ActionTooltip>
</div>
);
})}
diff --git a/packages/studio/src/components/InspectorPanel/use-composition-actions.ts b/packages/studio/src/components/InspectorPanel/use-composition-actions.ts
index 1084062487..8dd8d28eca 100644
--- a/packages/studio/src/components/InspectorPanel/use-composition-actions.ts
+++ b/packages/studio/src/components/InspectorPanel/use-composition-actions.ts
@@ -203,6 +203,7 @@ export const useCompositionActions = () => {
invocationTimestamp: Date.now(),
assetSelection: {
initialQuery: '',
+ fileTypes: null,
onSelectFile: () => {
onFilesSelected().catch(() => undefined);
},
diff --git a/packages/studio/src/components/QuickSwitcher/QuickSwitcher.tsx b/packages/studio/src/components/QuickSwitcher/QuickSwitcher.tsx
index f5b5953c63..b1c2e855ff 100644
--- a/packages/studio/src/components/QuickSwitcher/QuickSwitcher.tsx
+++ b/packages/studio/src/components/QuickSwitcher/QuickSwitcher.tsx
@@ -16,6 +16,11 @@ const QuickSwitcher: React.FC<{
readonly readOnlyStudio: boolean;
readonly assetSelection: {
readonly initialQuery: string;
+ readonly fileTypes: {
+ readonly extensions: string[];
+ readonly description: string;
+ readonly importLabel: string;
+ } | null;
readonly onSelectFile: () => void;
readonly onSelected: (asset: StaticFile) => void;
} | null;
diff --git a/packages/studio/src/components/QuickSwitcher/QuickSwitcherContent.tsx b/packages/studio/src/components/QuickSwitcher/QuickSwitcherContent.tsx
index 322f397851..978242c002 100644
--- a/packages/studio/src/components/QuickSwitcher/QuickSwitcherContent.tsx
+++ b/packages/studio/src/components/QuickSwitcher/QuickSwitcherContent.tsx
@@ -6,8 +6,8 @@ import React, {
useRef,
useState,
} from 'react';
-import {Internals} from 'remotion';
import type {_InternalTypes} from 'remotion';
+import {Internals} from 'remotion';
import type {StaticFile} from '../../api/get-static-files';
import {LIGHT_TEXT, WHITE} from '../../helpers/colors';
import {createFolderTree} from '../../helpers/create-folder-tree';
@@ -104,6 +104,13 @@ const results: React.CSSProperties = {
height: 300,
};
+const fileTypeDescription: React.CSSProperties = {
+ color: LIGHT_TEXT,
+ fontSize: 12,
+ lineHeight: '16px',
+ padding: '0 16px 12px',
+};
+
const stripQuery = (query: string) => {
if (query.startsWith('$') || query.startsWith('>') || query.startsWith('?')) {
return query.substring(1).trim();
@@ -164,6 +171,11 @@ export const QuickSwitcherContent: React.FC<{
readonly readOnlyStudio: boolean;
readonly assetSelection: {
readonly initialQuery: string;
+ readonly fileTypes: {
+ readonly extensions: string[];
+ readonly description: string;
+ readonly importLabel: string;
+ } | null;
readonly onSelectFile: () => void;
readonly onSelected: (asset: StaticFile) => void;
} | null;
@@ -257,24 +269,34 @@ export const QuickSwitcherContent: React.FC<{
if (mode === 'assets') {
const assetResults = fuzzySearch(
assetSearch.query,
- assetSearch.assets.map((asset) => {
- return {
- id: 'asset-' + asset.name,
- title: asset.name,
- type: 'asset',
- fileType: getPreviewFileType(asset.name),
- onSelected: () => {
- if (assetSelection !== null) {
- assetSelection.onSelected(asset);
- } else {
- selectAsset(asset.name);
- pushUrl(`/assets/${asset.name}`);
- }
-
- setSelectedModal(null);
- },
- };
- }),
+ assetSearch.assets
+ .filter((asset) => {
+ return (
+ assetSelection === null ||
+ assetSelection.fileTypes === null ||
+ assetSelection.fileTypes.extensions.some((extension) =>
+ asset.name.toLowerCase().endsWith(extension),
+ )
+ );
+ })
+ .map((asset) => {
+ return {
+ id: 'asset-' + asset.name,
+ title: asset.name,
+ type: 'asset',
+ fileType: getPreviewFileType(asset.name),
+ onSelected: () => {
+ if (assetSelection !== null) {
+ assetSelection.onSelected(asset);
+ } else {
+ selectAsset(asset.name);
+ pushUrl(`/assets/${asset.name}`);
+ }
+
+ setSelectedModal(null);
+ },
+ };
+ }),
);
if (assetSelection === null) {
@@ -284,7 +306,7 @@ export const QuickSwitcherContent: React.FC<{
return [
{
id: 'select-file',
- title: 'Import file...',
+ title: assetSelection.fileTypes?.importLabel ?? 'Import file...',
type: 'select-file',
onSelected: () => {
assetSelection.onSelectFile();
@@ -505,7 +527,9 @@ export const QuickSwitcherContent: React.FC<{
const showEmptyDocSearch = mode === 'docs' && actualQuery.trim() === '';
const placeholder =
mode === 'assets'
- ? 'Search assets...'
+ ? assetSelection?.fileTypes
+ ? 'Search transcripts...'
+ : 'Search assets...'
: mode === 'commands'
? 'Search actions...'
: mode === 'docs'
@@ -568,6 +592,11 @@ export const QuickSwitcherContent: React.FC<{
rightAlign={false}
/>
</div>
+ {assetSelection?.fileTypes ? (
+ <div style={fileTypeDescription}>
+ {assetSelection.fileTypes.description}
+ </div>
+ ) : null}
<div style={results} className={VERTICAL_SCROLLBAR_CLASSNAME}>
{showEmptyDocSearch ||
showSearchLoadingState ? null : resultsArray.length === 0 ? (
diff --git a/packages/studio/src/components/Timeline/TimelineAssetField.tsx b/packages/studio/src/components/Timeline/TimelineAssetField.tsx
index e227c580f9..13a066e3ea 100644
--- a/packages/studio/src/components/Timeline/TimelineAssetField.tsx
+++ b/packages/studio/src/components/Timeline/TimelineAssetField.tsx
@@ -272,6 +272,7 @@ export const TimelineAssetField: React.FC<TimelineAssetFieldProps> = ({
const openAssetSelection = useCallback(() => {
const assetSelection = {
initialQuery,
+ fileTypes: null,
onSelectFile: () => {
selectFile().catch(() => undefined);
},
diff --git a/packages/studio/src/state/modals.ts b/packages/studio/src/state/modals.ts
index 44049e647c..af318a91bb 100644
--- a/packages/studio/src/state/modals.ts
+++ b/packages/studio/src/state/modals.ts
@@ -151,6 +151,7 @@ export type AssetSelectionModalState = {
readonly invocationTimestamp: number;
readonly assetSelection: {
readonly initialQuery: string;
+ readonly fileTypes: null;
readonly onSelectFile: () => void;
readonly onSelected: (asset: StaticFile) => void;
};
@@ -323,6 +324,11 @@ export type ModalState =
invocationTimestamp: number;
assetSelection: {
initialQuery: string;
+ fileTypes: {
+ extensions: string[];
+ description: string;
+ importLabel: string;
+ } | null;
onSelectFile: () => void;
onSelected: (asset: StaticFile) => void;
} | null;
diff --git a/packages/studio/src/test/caption-inspector-import.test.tsx b/packages/studio/src/test/caption-inspector-import.test.tsx
index fd9d95a328..a2584f3d5d 100644
--- a/packages/studio/src/test/caption-inspector-import.test.tsx
+++ b/packages/studio/src/test/caption-inspector-import.test.tsx
@@ -53,7 +53,7 @@ test('imports captions from a JSON file in the public folder', async () => {
</SetSelectedModalContext.Provider>,
);
- fireEvent.click(screen.getByRole('button', {name: 'Import captions'}));
+ fireEvent.click(screen.getByRole('button', {name: 'Import transcript'}));
const modal = selectedModal as ModalState | null;
if (modal?.type !== 'quick-switcher' || modal.assetSelection === null) {
throw new Error('Expected asset Quick Switcher to open');
@@ -62,6 +62,11 @@ test('imports captions from a JSON file in the public folder', async () => {
const {assetSelection} = modal;
expect(assetSelection.initialQuery).toBe('');
+ expect(assetSelection.fileTypes?.extensions).toEqual(['.json', '.srt']);
+ expect(assetSelection.fileTypes?.description).toContain('ElevenLabs');
+ expect(
+ screen.getByLabelText('Import captions file').getAttribute('accept'),
+ ).toBe('.json,.srt');
await act(async () => {
await (assetSelection.onSelected({
lastModified: 0,