Skip to content

🎥 Make videos programmatically with React

Simplify caption style selection and URL validation

Igor Samokhovetse2f3ab363797
diff --git a/packages/cli/src/config/element-libraries.ts b/packages/cli/src/config/element-libraries.ts
index c74bf6834e..2c9ce8d90f 100644
--- a/packages/cli/src/config/element-libraries.ts
+++ b/packages/cli/src/config/element-libraries.ts
@@ -1,4 +1,7 @@
-import type {StudioElementLibrary} from '@remotion/studio-shared';
+import {
+ normalizeHttpUrl,
+ type StudioElementLibrary,
+} from '@remotion/studio-shared';
export type AddElementLibraryOptions = {
readonly url: string;
@@ -42,24 +45,12 @@ export const addElementLibrary = (options: AddElementLibraryOptions) => {
);
}
- let normalizedCaptionStylesUrl: string | null = null;
- if (captionStylesUrl !== undefined) {
- try {
- if (typeof captionStylesUrl !== 'string') {
- throw new Error('Invalid URL');
- }
-
- const parsed = new URL(captionStylesUrl);
- if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
- throw new Error('Unsupported protocol');
- }
-
- normalizedCaptionStylesUrl = parsed.href;
- } catch {
- throw new Error(
- 'Config.addElementLibrary() expects "captionStylesUrl" to be an absolute HTTP or HTTPS URL',
- );
- }
+ const normalizedCaptionStylesUrl =
+ captionStylesUrl === undefined ? null : normalizeHttpUrl(captionStylesUrl);
+ if (captionStylesUrl !== undefined && normalizedCaptionStylesUrl === null) {
+ throw new Error(
+ 'Config.addElementLibrary() expects "captionStylesUrl" to be an absolute HTTP or HTTPS URL',
+ );
}
if (displayName !== undefined && typeof displayName !== 'string') {
diff --git a/packages/codemods/src/insert-basic-captions.ts b/packages/codemods/src/insert-basic-captions.ts
index 42e541da00..903ab58776 100644
--- a/packages/codemods/src/insert-basic-captions.ts
+++ b/packages/codemods/src/insert-basic-captions.ts
@@ -109,13 +109,6 @@ export const insertBasicCaptions = ({
throw new Error('The selected element is not a Remotion Video or Audio');
}
- const componentName = element?.componentName ?? 'BasicCaptions';
- const captionsLocalName = ensureOfficialNamedImport({
- ast,
- importedName: componentName,
- preferredLocalName: componentName,
- sourcePath: importPath,
- });
const timingProps = [
'from',
'durationInFrames',
@@ -133,25 +126,31 @@ export const insertBasicCaptions = ({
const copiedAttributes = mediaAttributes.map(
(attribute) => recast.print(attribute).code,
);
- if (
- Number.isFinite(durationInFrames) &&
+ const fallbackTimingProps = [
+ ...(Number.isFinite(durationInFrames) &&
durationInFrames !== null &&
!copiedAttributes.some((attribute) =>
attribute.startsWith('durationInFrames'),
)
- ) {
- copiedAttributes.push(`durationInFrames={${durationInFrames}}`);
- }
-
- if (
- premountFor !== null &&
+ ? [{name: 'durationInFrames', value: durationInFrames}]
+ : []),
+ ...(premountFor !== null &&
!copiedAttributes.some((attribute) => attribute.startsWith('premountFor'))
- ) {
- copiedAttributes.push(`premountFor={${premountFor}}`);
- }
+ ? [{name: 'premountFor', value: premountFor}]
+ : []),
+ ];
let elementSource: string;
if (element === null) {
+ const captionsLocalName = ensureOfficialNamedImport({
+ ast,
+ importedName: 'BasicCaptions',
+ preferredLocalName: 'BasicCaptions',
+ sourcePath: importPath,
+ });
+ copiedAttributes.push(
+ ...fallbackTimingProps.map(({name, value}) => `${name}={${value}}`),
+ );
const formattingConfig = getSourceFormattingConfig({
input,
prettierConfigOverride,
@@ -233,8 +232,8 @@ export const insertBasicCaptions = ({
element: createElementFromInsertable({
element: {
type: 'component',
- componentName: captionsLocalName,
- importName: componentName,
+ componentName: element.componentName,
+ importName: element.componentName,
importPath,
position: null,
props: [
@@ -244,33 +243,18 @@ export const insertBasicCaptions = ({
)
.map(([name, value]) => ({name, value})),
{name: 'captions', value: captions},
- ...(durationInFrames !== null && Number.isFinite(durationInFrames)
- ? [{name: 'durationInFrames', value: durationInFrames}]
- : []),
+ ...fallbackTimingProps,
],
},
from: null,
- premountFor,
+ premountFor: null,
wrapInSequence: null,
}),
});
- const copiedNames = new Set(
- mediaAttributes.map((attribute) =>
- attribute.type === 'JSXAttribute' &&
- attribute.name.type === 'JSXIdentifier'
- ? attribute.name.name
- : null,
- ),
- );
- jsx.openingElement.attributes = (
- jsx.openingElement.attributes ?? []
- ).filter(
- (attribute) =>
- attribute.type !== 'JSXAttribute' ||
- attribute.name.type !== 'JSXIdentifier' ||
- !copiedNames.has(attribute.name.name),
- );
- jsx.openingElement.attributes.push(...(mediaAttributes as never[]));
+ jsx.openingElement.attributes = [
+ ...(jsx.openingElement.attributes ?? []),
+ ...(mediaAttributes as never[]),
+ ];
elementSource = printInsertedJsx({
element: jsx,
input,
diff --git a/packages/docs/src/components/Elements/element-definitions.ts b/packages/docs/src/components/Elements/element-definitions.ts
index d2d62349b7..a8d3964100 100644
--- a/packages/docs/src/components/Elements/element-definitions.ts
+++ b/packages/docs/src/components/Elements/element-definitions.ts
@@ -105,7 +105,6 @@ export type ElementDefinition = {
readonly safeArea: number;
readonly slug: string;
readonly installationMode: ElementInstallationMode;
- readonly isCaptionStyle: boolean;
readonly width: number;
};
@@ -131,7 +130,6 @@ const youtubeSubscribeNudgeAssets = {
const elementImplementations = [
{
slug: 'audio/oscilloscope',
- isCaptionStyle: false,
assets: [audioOscilloscopeAudio],
installationProps: {
src: staticFileRef(audioOscilloscopeAudio.path),
@@ -164,7 +162,6 @@ const elementImplementations = [
},
{
slug: 'audio/waveform-progress',
- isCaptionStyle: false,
assets: [audioOscilloscopeAudio],
installationProps: {
src: staticFileRef(audioOscilloscopeAudio.path),
@@ -197,7 +194,6 @@ const elementImplementations = [
},
{
slug: 'audio/mirrored-spectrum',
- isCaptionStyle: false,
assets: [audioOscilloscopeAudio],
installationProps: {
src: staticFileRef(audioOscilloscopeAudio.path),
@@ -230,7 +226,6 @@ const elementImplementations = [
},
{
slug: 'backgrounds/notebook-paper',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: NotebookPaper,
@@ -258,7 +253,6 @@ const elementImplementations = [
},
{
slug: 'backgrounds/paper-texture',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: PaperTexture,
@@ -287,7 +281,6 @@ const elementImplementations = [
},
{
slug: 'backgrounds/rotating-starburst',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: RotatingStarburst,
@@ -315,7 +308,6 @@ const elementImplementations = [
},
{
slug: 'backgrounds/moving-waves',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: MovingWaves,
@@ -343,7 +335,6 @@ const elementImplementations = [
},
{
slug: 'backgrounds/moving-zigzags',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: MovingZigzags,
@@ -371,7 +362,6 @@ const elementImplementations = [
},
{
slug: 'backgrounds/liquid-contours',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: LiquidContours,
@@ -400,7 +390,6 @@ const elementImplementations = [
},
{
slug: 'captions/basic-captions',
- isCaptionStyle: true,
assets: [],
installationProps: null,
component: BasicCaptions,
@@ -429,7 +418,6 @@ const elementImplementations = [
},
{
slug: 'captions/rounded-captions',
- isCaptionStyle: true,
assets: [],
installationProps: null,
component: RoundedCaptions,
@@ -463,7 +451,6 @@ const elementImplementations = [
},
{
slug: 'captions/moving-pill-captions',
- isCaptionStyle: true,
assets: [],
installationProps: null,
component: MovingPillCaptions,
@@ -496,7 +483,6 @@ const elementImplementations = [
},
{
slug: 'captions/popping-word-captions',
- isCaptionStyle: true,
assets: [],
installationProps: null,
component: PoppingWordCaptions,
@@ -528,7 +514,6 @@ const elementImplementations = [
},
{
slug: 'captions/word-highlight-captions',
- isCaptionStyle: true,
assets: [],
installationProps: null,
component: WordHighlightCaptions,
@@ -560,7 +545,6 @@ const elementImplementations = [
},
{
slug: 'commerce/product-collection',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: ProductCollection,
@@ -588,7 +572,6 @@ const elementImplementations = [
},
{
slug: 'commerce/product-discount-callout',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: ProductDiscountCallout,
@@ -619,7 +602,6 @@ const elementImplementations = [
},
{
slug: 'commerce/shine',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: Shine,
@@ -645,7 +627,6 @@ const elementImplementations = [
},
{
slug: 'commerce/tear',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: Tear,
@@ -671,7 +652,6 @@ const elementImplementations = [
},
{
slug: 'data/horizontal-bar-chart',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: HorizontalBarChart,
@@ -699,7 +679,6 @@ const elementImplementations = [
},
{
slug: 'data/line-chart',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: LineChart,
@@ -725,7 +704,6 @@ const elementImplementations = [
},
{
slug: 'data/number-counter',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: NumberCounter,
@@ -759,7 +737,6 @@ const elementImplementations = [
},
{
slug: 'data/pie-chart',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: PieChart,
@@ -786,7 +763,6 @@ const elementImplementations = [
},
{
slug: 'data/vertical-bar-chart',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: VerticalBarChart,
@@ -815,7 +791,6 @@ const elementImplementations = [
},
{
slug: 'layouts/picture-in-picture-transition',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: PictureInPictureTransition,
@@ -844,7 +819,6 @@ const elementImplementations = [
},
{
slug: 'layouts/slide-to-split-screen',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: SlideToSplitScreen,
@@ -873,7 +847,6 @@ const elementImplementations = [
},
{
slug: 'maps/map-flyover',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: MapFlyover,
@@ -903,7 +876,6 @@ const elementImplementations = [
},
{
slug: 'maps/watercolor-map',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: WatercolorMap,
@@ -935,7 +907,6 @@ const elementImplementations = [
},
{
slug: 'overlays/location-lower-third',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: LocationLowerThird,
@@ -963,7 +934,6 @@ const elementImplementations = [
},
{
slug: 'overlays/name-lower-third',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: NameLowerThird,
@@ -992,7 +962,6 @@ const elementImplementations = [
},
{
slug: 'overlays/social-safe-zones',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: SocialSafeZones,
@@ -1021,7 +990,6 @@ const elementImplementations = [
},
{
slug: 'text/news-article-highlight',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: NewsArticleHighlight,
@@ -1049,7 +1017,6 @@ const elementImplementations = [
},
{
slug: 'storytelling/speed-lines',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: SpeedLines,
@@ -1077,7 +1044,6 @@ const elementImplementations = [
},
{
slug: 'storytelling/on-screen-messages',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: OnScreenMessages,
@@ -1106,7 +1072,6 @@ const elementImplementations = [
},
{
slug: 'storytelling/polaroid-pictures',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: PolaroidPictures,
@@ -1135,7 +1100,6 @@ const elementImplementations = [
},
{
slug: 'text/circle-marker',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: CircleMarker,
@@ -1167,7 +1131,6 @@ const elementImplementations = [
},
{
slug: 'text/crossed-off',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: CrossedOffText,
@@ -1197,7 +1160,6 @@ const elementImplementations = [
},
{
slug: 'text/spinning-text-wheel',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: SpinningTextWheel,
@@ -1226,7 +1188,6 @@ const elementImplementations = [
},
{
slug: 'text/strike-through',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: StrikeThroughText,
@@ -1258,7 +1219,6 @@ const elementImplementations = [
},
{
slug: 'text/text-marker',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: TextMarker,
@@ -1288,7 +1248,6 @@ const elementImplementations = [
},
{
slug: 'youtube/youtube-comment-highlight',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: YouTubeCommentHighlight,
@@ -1316,7 +1275,6 @@ const elementImplementations = [
},
{
slug: 'youtube/youtube-end-card',
- isCaptionStyle: false,
assets: [],
installationProps: null,
component: YouTubeEndCard,
@@ -1345,7 +1303,6 @@ const elementImplementations = [
},
{
slug: 'youtube/youtube-subscribe-nudge',
- isCaptionStyle: false,
assets: [
youtubeSubscribeNudgeAssets.click,
youtubeSubscribeNudgeAssets.ding,
diff --git a/packages/docs/src/components/Elements/element-drag-data.ts b/packages/docs/src/components/Elements/element-drag-data.ts
index 2dd099bd12..acb7c96064 100644
--- a/packages/docs/src/components/Elements/element-drag-data.ts
+++ b/packages/docs/src/components/Elements/element-drag-data.ts
@@ -33,7 +33,7 @@ export const createElementPayloadFromDefinition = ({
? definition.initialProps
: {...definition.initialProps, ...definition.installationProps},
installationMode: definition.installationMode,
- isCaptionStyle: definition.isCaptionStyle,
+ isCaptionStyle: definition.category === 'captions',
slug: definition.slug,
sourceCode,
});
diff --git a/packages/docs/src/theme/Layout/index.tsx b/packages/docs/src/theme/Layout/index.tsx
index 51082ba495..e6daabe4ed 100644
--- a/packages/docs/src/theme/Layout/index.tsx
+++ b/packages/docs/src/theme/Layout/index.tsx
@@ -8,7 +8,6 @@ import {useCrawlChatSidePanel} from 'crawlchat-client';
import React, {
useEffect,
useLayoutEffect,
- useRef,
useState,
type ReactNode,
} from 'react';
@@ -52,10 +51,7 @@ const LayoutWithCrawlChat = (props: Props): ReactNode => {
};
const LayoutWrapper = (props: Props): ReactNode => {
- const location = useLocation();
- const {pathname, search} = location;
- const history = useHistory();
- const captionContext = useRef(false);
+ const {pathname, search} = useLocation();
const [isStudioElementsEmbed, setIsStudioElementsEmbed] = useState(false);
useLayoutEffect(() => {
@@ -63,25 +59,9 @@ const LayoutWrapper = (props: Props): ReactNode => {
const isEmbedded =
isInsideStudio() &&
(pathname === '/elements' || pathname.startsWith('/elements/'));
- const params = new URLSearchParams(search);
- const context = params.get('remotion-studio-context');
- if (context !== null) {
- captionContext.current = context === 'captions';
- }
-
- const isCaptionPicker = isEmbedded && captionContext.current;
- if (
- isCaptionPicker &&
- (context === null ||
- params.get('remotion-studio') !== 'true' ||
- params.get('docusaurus-theme') !== 'dark')
- ) {
- params.set('remotion-studio', 'true');
- params.set('remotion-studio-context', 'captions');
- params.set('docusaurus-theme', 'dark');
- history.replace({...location, search: `?${params}`});
- }
-
+ const isCaptionPicker =
+ isEmbedded &&
+ new URLSearchParams(search).get('remotion-studio-context') === 'captions';
document.body.classList.toggle('studio-elements-embed', isEmbedded);
document.body.classList.toggle('studio-caption-picker', isCaptionPicker);
setIsStudioElementsEmbed(isEmbedded);
@@ -92,7 +72,7 @@ const LayoutWrapper = (props: Props): ReactNode => {
'studio-caption-picker',
);
};
- }, [history, location, pathname, search]);
+ }, [pathname, search]);
if (isStudioElementsEmbed) {
return <Layout {...props} noFooter />;
diff --git a/packages/studio-protocol/src/add-element-library-to-studio.ts b/packages/studio-protocol/src/add-element-library-to-studio.ts
index b54253a5e6..35c5075a33 100644
--- a/packages/studio-protocol/src/add-element-library-to-studio.ts
+++ b/packages/studio-protocol/src/add-element-library-to-studio.ts
@@ -400,9 +400,8 @@ export const addElementLibraryToStudio = async ({
if (
(parsedUrl.protocol === 'http:' || parsedUrl.protocol === 'https:') &&
(parsedCaptionStylesUrl === null ||
- (typeof captionStylesUrl === 'string' &&
- (parsedCaptionStylesUrl.protocol === 'http:' ||
- parsedCaptionStylesUrl.protocol === 'https:'))) &&
+ parsedCaptionStylesUrl.protocol === 'http:' ||
+ parsedCaptionStylesUrl.protocol === 'https:') &&
normalizedDisplayName !== ''
) {
const parentResult = await addElementLibraryToParentStudio({
@@ -424,8 +423,7 @@ export const addElementLibraryToStudio = async ({
return addElementLibraryToStudioWithDependencies(
{
- captionStylesUrl:
- captionStylesUrl === undefined ? null : captionStylesUrl,
+ captionStylesUrl: captionStylesUrl ?? null,
displayName: displayName ?? null,
url,
},
diff --git a/packages/studio-server/src/preview-server/routes/update-config.ts b/packages/studio-server/src/preview-server/routes/update-config.ts
index 583a61109c..52419c888a 100644
--- a/packages/studio-server/src/preview-server/routes/update-config.ts
+++ b/packages/studio-server/src/preview-server/routes/update-config.ts
@@ -4,6 +4,7 @@ import {
configMethodLifecycles,
type ConfigUpdate,
type ConfigValue,
+ normalizeHttpUrl,
type StudioRuntimeConfig,
validateStudioKeyboardShortcuts,
type UpdateConfigRequest,
@@ -119,22 +120,11 @@ const validateUpdates = (updates: unknown): string | null => {
return 'Config.addElementLibrary() expects an HTTP or HTTPS URL.';
}
- if (elementLibrary.captionStylesUrl !== undefined) {
- try {
- if (typeof elementLibrary.captionStylesUrl !== 'string') {
- throw new Error('Invalid URL');
- }
-
- const parsedUrl = new URL(elementLibrary.captionStylesUrl);
- if (
- parsedUrl.protocol !== 'http:' &&
- parsedUrl.protocol !== 'https:'
- ) {
- throw new Error('Unsupported protocol');
- }
- } catch {
- return 'Config.addElementLibrary() expects "captionStylesUrl" to be an absolute HTTP or HTTPS URL.';
- }
+ if (
+ elementLibrary.captionStylesUrl !== undefined &&
+ normalizeHttpUrl(elementLibrary.captionStylesUrl) === null
+ ) {
+ return 'Config.addElementLibrary() expects "captionStylesUrl" to be an absolute HTTP or HTTPS URL.';
}
if (
diff --git a/packages/studio-server/src/preview-server/studio-protocol/handle-element-library.ts b/packages/studio-server/src/preview-server/studio-protocol/handle-element-library.ts
index b395d35b93..ca8a459899 100644
--- a/packages/studio-server/src/preview-server/studio-protocol/handle-element-library.ts
+++ b/packages/studio-server/src/preview-server/studio-protocol/handle-element-library.ts
@@ -1,5 +1,6 @@
import type {IncomingMessage, ServerResponse} from 'node:http';
import {StudioProtocolInternals} from '@remotion/studio-protocol';
+import {normalizeHttpUrl} from '@remotion/studio-shared';
import {consumeStudioProtocolTarget} from '../element-install-state';
import type {LiveEventsServer} from '../live-events';
import {parseRequestBody, RequestBodyTooLargeError} from '../parse-body';
@@ -103,25 +104,18 @@ export const handleStudioProtocolElementLibrary = async ({
return;
}
- let captionStylesUrl: string | null = null;
- if (parsedRequest.captionStylesUrl !== null) {
- try {
- const parsedUrl = new URL(parsedRequest.captionStylesUrl);
- if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') {
- throw new Error('Unsupported protocol');
- }
-
- captionStylesUrl = parsedUrl.href;
- } catch {
- writeStudioProtocolError({
- code: 'invalid-url',
- message:
- 'The caption styles URL must be an absolute HTTP or HTTPS URL.',
- response,
- status: 400,
- });
- return;
- }
+ const captionStylesUrl =
+ parsedRequest.captionStylesUrl === null
+ ? null
+ : normalizeHttpUrl(parsedRequest.captionStylesUrl);
+ if (parsedRequest.captionStylesUrl !== null && captionStylesUrl === null) {
+ writeStudioProtocolError({
+ code: 'invalid-url',
+ message: 'The caption styles URL must be an absolute HTTP or HTTPS URL.',
+ response,
+ status: 400,
+ });
+ return;
}
const displayName = parsedRequest.displayName?.trim() ?? null;
diff --git a/packages/studio-server/src/test/update-config.test.ts b/packages/studio-server/src/test/update-config.test.ts
index 26b004db33..26959a5e83 100644
--- a/packages/studio-server/src/test/update-config.test.ts
+++ b/packages/studio-server/src/test/update-config.test.ts
@@ -22,6 +22,7 @@ const studioRuntimeConfig = (elementLibraryUrls: readonly string[]) => ({
elementLibraries: elementLibraryUrls.map((url) => ({
displayName: null,
url,
+ captionStylesUrl: null,
})),
interactivityEnabled: true,
keyboardShortcutsEnabled: true,
diff --git a/packages/studio-shared/src/index.ts b/packages/studio-shared/src/index.ts
index 544f79b352..fec98289d5 100644
--- a/packages/studio-shared/src/index.ts
+++ b/packages/studio-shared/src/index.ts
@@ -450,7 +450,7 @@ export {
stringifySequenceExpandedRowKey,
stringifySequenceSubscriptionKey,
} from './stringify-sequence-subscription-key';
-export {isUrl} from './url';
+export {isUrl, normalizeHttpUrl} from './url';
export {emptyCompositionComponent} from './empty-composition-component';
export {assetCompositionComponent} from './asset-composition-component';
diff --git a/packages/studio-shared/src/studio-runtime-config.ts b/packages/studio-shared/src/studio-runtime-config.ts
index e06e8650be..2d62a3114d 100644
--- a/packages/studio-shared/src/studio-runtime-config.ts
+++ b/packages/studio-shared/src/studio-runtime-config.ts
@@ -23,7 +23,7 @@ export type ConfigFileStudioSettings = {
export type StudioElementLibrary = {
readonly displayName: string | null;
readonly url: string;
- readonly captionStylesUrl?: string | null;
+ readonly captionStylesUrl: string | null;
};
export type StudioRuntimeConfig = {
diff --git a/packages/studio-shared/src/url.ts b/packages/studio-shared/src/url.ts
index 25a41e8722..cad88c1157 100644
--- a/packages/studio-shared/src/url.ts
+++ b/packages/studio-shared/src/url.ts
@@ -6,3 +6,18 @@ export const isUrl = (value: string): boolean => {
return false;
}
};
+
+export const normalizeHttpUrl = (value: unknown): string | null => {
+ if (typeof value !== 'string') {
+ return null;
+ }
+
+ try {
+ const parsed = new URL(value);
+ return parsed.protocol === 'http:' || parsed.protocol === 'https:'
+ ? parsed.href
+ : null;
+ } catch {
+ return null;
+ }
+};
diff --git a/packages/studio/src/components/ElementInstallRequestHandler.tsx b/packages/studio/src/components/ElementInstallRequestHandler.tsx
index 22a34ce483..34c35a6200 100644
--- a/packages/studio/src/components/ElementInstallRequestHandler.tsx
+++ b/packages/studio/src/components/ElementInstallRequestHandler.tsx
@@ -166,9 +166,10 @@ export const ElementInstallRequestHandler: FC = () => {
}
const modal = selectedModalRef.current;
+ const selectsCaptionStyle =
+ modal?.type === 'transcribe' && modal.target !== null;
if (
- modal?.type === 'transcribe' &&
- modal.target !== null &&
+ selectsCaptionStyle &&
(payload.element.isCaptionStyle !== true ||
payload.element.installationMode !== 'component-owned-sequence')
) {
@@ -222,9 +223,20 @@ export const ElementInstallRequestHandler: FC = () => {
responsePort.onmessage = () => {
window.clearTimeout(timeout);
responsePort.close();
- if (request !== null) {
- enqueueElementInstallRequest(request);
+ if (request === null) {
+ return;
+ }
+
+ if (selectsCaptionStyle) {
+ setSelectedModal((current) =>
+ current?.type === 'transcribe' && current.target !== null
+ ? {...current, captionStyle: request}
+ : current,
+ );
+ return;
}
+
+ enqueueElementInstallRequest(request);
};
responsePort.postMessage(result);
@@ -237,28 +249,11 @@ export const ElementInstallRequestHandler: FC = () => {
compositionFile,
currentCompositionId,
previewServerClientId,
+ setSelectedModal,
]);
useEffect(() => {
return subscribeToElementInstallRequests((request) => {
- const modal = selectedModalRef.current;
- if (modal?.type === 'transcribe' && modal.target !== null) {
- if (
- request.element.isCaptionStyle !== true ||
- request.element.installationMode !== 'component-owned-sequence'
- ) {
- showNotification('This Element is not a caption style', 4000);
- return;
- }
-
- setSelectedModal((current) =>
- current?.type === 'transcribe' && current.target !== null
- ? {...current, captionStyle: request}
- : current,
- );
- return;
- }
-
const shouldAddCompositionDefaults =
request.source.type !== 'drag-and-drop' &&
request.compositionFile !== null &&
@@ -283,7 +278,7 @@ export const ElementInstallRequestHandler: FC = () => {
: request;
setPendingRequests((requests) => [...requests, requestWithDefaults]);
});
- }, [config, setSelectedModal]);
+ }, [config]);
useEffect(() => {
if (!canInstallElement || showingExperimentalNotice) {
diff --git a/packages/studio/src/components/Modals.tsx b/packages/studio/src/components/Modals.tsx
index 457f41f225..d7cfc770c9 100644
--- a/packages/studio/src/components/Modals.tsx
+++ b/packages/studio/src/components/Modals.tsx
@@ -2,6 +2,7 @@ import {
StudioProtocolInternals,
type AddElementLibraryToStudioResult,
} from '@remotion/studio-protocol';
+import {normalizeHttpUrl} from '@remotion/studio-shared';
import React, {useCallback, useContext, useEffect} from 'react';
import {getBrowserStudioOperations} from '../helpers/browser-studio-operations';
import {StudioServerConnectionCtx} from '../helpers/client-id';
@@ -194,23 +195,12 @@ export const Modals: React.FC<{
// The response below explains that the URL is invalid.
}
- let captionStylesUrl: string | null = null;
- let invalidCaptionStylesUrl = false;
- if (request.captionStylesUrl !== null) {
- try {
- const parsedUrl = new URL(request.captionStylesUrl);
- if (
- parsedUrl.protocol !== 'http:' &&
- parsedUrl.protocol !== 'https:'
- ) {
- throw new Error('Unsupported protocol');
- }
-
- captionStylesUrl = parsedUrl.href;
- } catch {
- invalidCaptionStylesUrl = true;
- }
- }
+ const captionStylesUrl =
+ request.captionStylesUrl === null
+ ? null
+ : normalizeHttpUrl(request.captionStylesUrl);
+ const invalidCaptionStylesUrl =
+ request.captionStylesUrl !== null && captionStylesUrl === null;
const displayName = request.displayName?.trim() ?? null;
const canAddLibrary =
diff --git a/packages/studio/src/components/RenderQueue/CaptionQueueProcessor.tsx b/packages/studio/src/components/RenderQueue/CaptionQueueProcessor.tsx
index 167459456b..0dde68d659 100644
--- a/packages/studio/src/components/RenderQueue/CaptionQueueProcessor.tsx
+++ b/packages/studio/src/components/RenderQueue/CaptionQueueProcessor.tsx
@@ -139,7 +139,7 @@ export const CaptionQueueProcessor: React.FC = () => {
let installed = false;
for (let index = 0; index < 1000; index++) {
signal.throwIfAborted();
- const installationName = `${baseName}${index === 0 ? '' : `-${index + 1}`}`;
+ const installationName = `${baseName}${index === 0 ? '' : `-copy${index === 1 ? '' : `-${index}`}`}`;
const preflight = await prepareElementInstall({
installationName,
destination: {
diff --git a/packages/studio/src/components/Transcription/TranscriptionModal.tsx b/packages/studio/src/components/Transcription/TranscriptionModal.tsx
index 8409d94d4b..3b437c9fce 100644
--- a/packages/studio/src/components/Transcription/TranscriptionModal.tsx
+++ b/packages/studio/src/components/Transcription/TranscriptionModal.tsx
@@ -84,6 +84,8 @@ const DEFAULT_TOP_K = 50;
const DEFAULT_REPETITION_PENALTY = 1;
const DEFAULT_NO_REPEAT_NGRAM_SIZE = 0;
const MAX_CHUNK_LENGTH_IN_SECONDS = 30;
+const REMOTION_CAPTION_STYLES_URL =
+ 'https://www.remotion.dev/elements/captions';
type Tab = 'transcribe' | 'advanced' | 'models' | 'styles';
@@ -713,20 +715,14 @@ export const TranscriptionModal: React.FC<TranscriptionModalState> = ({
src,
target,
}) => {
- const [libraryUrl, setLibraryUrl] = useState(
- 'https://www.remotion.dev/elements/captions',
- );
+ const [libraryUrl, setLibraryUrl] = useState(REMOTION_CAPTION_STYLES_URL);
const {studioRuntimeConfig} = useSettings();
const libraryOptions: SegmentedControlItem[] = [
- {
- url: 'https://www.remotion.dev/elements/captions',
- displayName: 'Remotion captions',
- },
+ {url: REMOTION_CAPTION_STYLES_URL, displayName: 'Remotion captions'},
...(studioRuntimeConfig?.elementLibraries ?? []).flatMap(
({captionStylesUrl, displayName: libraryName}) =>
captionStylesUrl === null ||
- captionStylesUrl === undefined ||
- captionStylesUrl === 'https://www.remotion.dev/elements/captions'
+ captionStylesUrl === REMOTION_CAPTION_STYLES_URL
? []
: [{url: captionStylesUrl, displayName: libraryName}],
),
@@ -875,7 +871,7 @@ export const TranscriptionModal: React.FC<TranscriptionModalState> = ({
outName:
target === null
? outName
- : (captionStyle?.element.displayName ?? 'Basic Captions'),
+ : (captionStyle?.element.displayName ?? 'Basic captions'),
target,
model: selectedModel,
language: modelInfo.multilingual ? selectedLanguage : null,
@@ -933,7 +929,9 @@ export const TranscriptionModal: React.FC<TranscriptionModalState> = ({
gap: 8,
}}
>
- {target === null || captionStyle === null ? null : (
+ {target === null || captionStyle === null ? (
+ <div style={flexer} />
+ ) : (
<div
style={{
display: 'flex',
@@ -1011,9 +1009,6 @@ export const TranscriptionModal: React.FC<TranscriptionModalState> = ({
</div>
</div>
)}
- {captionStyle === null || target === null ? (
- <div style={flexer} />
- ) : null}
<Button
onClick={onAddToQueue}
disabled={!canTranscribe}