Skip to content

🎥 Make videos programmatically with React

fix(studio): respect trims in caption timeline labels

JonnyBurgerda3dd12e5c6b
diff --git a/packages/studio/src/components/Timeline/TimelineSequence.tsx b/packages/studio/src/components/Timeline/TimelineSequence.tsx
index 64e9f936b6..690ad89d75 100644
--- a/packages/studio/src/components/Timeline/TimelineSequence.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequence.tsx
@@ -369,6 +369,10 @@ const TimelineSequenceBar: React.FC<{
readonly left: number;
readonly width: number;
} | null;
+ readonly captionRange: {
+ readonly startMs: number;
+ readonly endMs: number;
+ } | null;
readonly hasCaptionsSchema: boolean;
readonly connectedComposition: _InternalTypes['AnyComposition'] | null;
readonly activeTrimEdge: 'left' | 'right' | null;
@@ -413,6 +417,7 @@ const TimelineSequenceBar: React.FC<{
s,
labelOffset,
captionLabelBounds,
+ captionRange,
hasCaptionsSchema,
connectedComposition,
activeTrimEdge,
@@ -781,8 +786,12 @@ const TimelineSequenceBar: React.FC<{
}}
/>
) : null}
- {hasCaptionsSchema ? (
- <TimelineSequenceCaptions s={s} />
+ {captionRange ? (
+ <TimelineSequenceCaptions
+ s={s}
+ startMs={captionRange.startMs}
+ endMs={captionRange.endMs}
+ />
) : (
<TimelineSequenceLabel
label={
@@ -2417,6 +2426,24 @@ const TimelineSequenceLayoutInner: React.FC<{
<TimelineSequenceBar
s={s}
hasCaptionsSchema={hasCaptionsSchema}
+ captionRange={
+ hasCaptionsSchema
+ ? {
+ // The source offset already includes this layer's trim and
+ // clipping by ancestors, independent of viewport scrolling.
+ startMs: (sequenceFrameOffset / video.fps) * 1000,
+ endMs:
+ ((sequenceFrameOffset +
+ Math.max(
+ 0,
+ Math.min(s.duration, video.durationInFrames - s.from),
+ ) *
+ s.sequencePlaybackRate) /
+ video.fps) *
+ 1000,
+ }
+ : null
+ }
labelOffset={
labelStartFrame === null
? 0
diff --git a/packages/studio/src/components/Timeline/TimelineSequenceCaptions.tsx b/packages/studio/src/components/Timeline/TimelineSequenceCaptions.tsx
index c586578fbf..6a8ece9986 100644
--- a/packages/studio/src/components/Timeline/TimelineSequenceCaptions.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequenceCaptions.tsx
@@ -6,7 +6,9 @@ import {TimelineSequenceLabel} from './timeline-sequence-label';
export const TimelineSequenceCaptions: React.FC<{
readonly s: TSequence;
-}> = ({s}) => {
+ readonly startMs: number;
+ readonly endMs: number;
+}> = ({s, startMs, endMs}) => {
const captionKey = Object.entries(s.controls?.schema ?? {}).find(
([, field]) => field.type === 'remotion-captions',
)?.[0];
@@ -15,11 +17,17 @@ export const TimelineSequenceCaptions: React.FC<{
const value = values[captionKey ?? 'captions'];
const captions = Array.isArray(value) ? (value as Caption[]) : [];
return captions
+ .filter(
+ (caption) =>
+ endMs > startMs &&
+ caption.endMs > startMs &&
+ caption.startMs < endMs,
+ )
.map((caption) => caption.text)
.join('')
.trim();
},
- [captionKey],
+ [captionKey, endMs, startMs],
);
const transcript = useRuntimeValueSelector({
controls: s.controls ?? null,