Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/DATADOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ format, stall reason.

**Feature usage** (`feature_used`, `src/lib/featureTelemetry.ts`). One event
per editing tool an export uses (`action: exported`): trim, rotate, speed,
mute, volume, normalize_audio, text_overlay, image_overlay, background_music,
mute, volume, normalize_audio, text_overlay, timed_text, image_overlay, background_music,
color_adjustments, sharpen, denoise, stabilization, fill_framing,
custom_size, crop. Interactive features: `history` (undo, redo), `project_file`
(save, open), `preview_tool` (compare, grid, framing_guide, save_frame),
`safe_area`, `export_profile`, `trim_timeline` (seek, drag_handle), `crop_tool` (aspect, drag, reset), `batch_export` (start, done, done_with_failures, cancel, save_folder). Only these names are ever sent; never values,
`safe_area`, `export_profile`, `trim_timeline` (seek, drag_handle), `crop_tool` (aspect, drag, reset), `batch_export` (start, done, done_with_failures, cancel, save_folder), `text_tool` (import_captions, set_timing, caption_box). Only these names are ever sent; never values,
names or text.

**Synthetic check.** A Datadog Synthetic browser test opens production every
Expand Down
17 changes: 17 additions & 0 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ the trim are dimmed. Click the strip to move the preview there, and drag the
handles (or use the arrow keys on them) to set the start and end. While an
export runs, making frames pauses and carries on afterwards.

## Text and captions

**Add Text** in the Text section puts a line of text on the video. Drag it
in the preview to place it; the preview shows it where, and at the size,
the export will draw it. By default text stays on for the whole video. To
show it for part of it, set **From** and **Until** in seconds of the video,
or play to a moment and press **Start at playhead** or **End at playhead**.
**Whole video** clears the times. Times follow the source video, so they
stay right when you trim or change the speed, and text outside the trim is
left out. **Dark box behind the text** gives it a caption-style background.

**Import captions** reads an `.srt` or `.vtt` subtitle file and adds each
cue as a timed caption near the bottom of the frame, sized for the output.
An edit holds up to 500 text items; a longer file adds its first 500 cues.
**Remove all** clears them. Exported text uses the bundled Geist font
whatever font the preview shows.

## Batch export

**Batch export** in the Edit panel applies this edit to other videos. Add
Expand Down
73 changes: 67 additions & 6 deletions src/components/DraggableTextOverlays.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
"use client";

import { TextOverlay, EditRecipe } from "@/lib/types";
import { useRef, useState, useCallback, useEffect, useMemo } from "react";
import { getTextPixelPosition, getTextPercentPosition } from "@/lib/text-overlay";
import { useRef, useState, useCallback, useEffect, useMemo, type RefObject } from "react";
import { getTextPixelPosition, getTextPercentPosition, isTextVisibleAt } from "@/lib/text-overlay";
import { getFontFamily, ensureFontLoaded } from "@/utils/fontLoader";

interface DraggableTextOverlaysProps {
recipe?: EditRecipe;
containerWidth: number;
containerHeight: number;
/** Preview pixels per output pixel, so text is the size it will be in the export. */
scale?: number;
/** The preview video, to show timed text only while it is on. */
videoRef?: RefObject<HTMLVideoElement | null>;
selectedTextId: string | null;
onUpdateText: (id: string, updates: Partial<TextOverlay>) => void;
onSelectText: (id: string | null) => void;
Expand All @@ -22,6 +26,8 @@ export default function DraggableTextOverlays({
recipe,
containerWidth,
containerHeight,
scale = 1,
videoRef,
selectedTextId,
onUpdateText,
onSelectText,
Expand All @@ -43,6 +49,50 @@ export default function DraggableTextOverlays({
return Array.isArray(overlays) ? overlays : [];
}, [recipe?.textOverlays]);

// The preview's playhead, followed only while some text is timed.
const [currentTime, setCurrentTime] = useState(0);
const hasTimedText = textOverlays.some((o) => o.start !== undefined || o.end !== undefined);
useEffect(() => {
const video = videoRef?.current;
if (!video || !hasTimedText) return;
let frame = 0;
// Re-render only when which text is on changes, not on every frame.
let shown = "";
const update = () => {
const t = video.currentTime;
const next = textOverlays.map((o) => (isTextVisibleAt(o, t) ? "1" : "0")).join("");
if (next === shown) return;
shown = next;
setCurrentTime(t);
};
// timeupdate fires only about 4 times a second; follow frames while playing.
const tick = () => {
update();
frame = requestAnimationFrame(tick);
};
const play = () => {
cancelAnimationFrame(frame);
frame = requestAnimationFrame(tick);
};
const stop = () => {
cancelAnimationFrame(frame);
update();
};
update();
if (!video.paused) play();
video.addEventListener("play", play);
video.addEventListener("pause", stop);
video.addEventListener("seeked", update);
video.addEventListener("timeupdate", update);
return () => {
cancelAnimationFrame(frame);
video.removeEventListener("play", play);
video.removeEventListener("pause", stop);
video.removeEventListener("seeked", update);
video.removeEventListener("timeupdate", update);
};
}, [videoRef, hasTimedText, textOverlays]);

/**
* Handles the start of a drag operation.
*/
Expand Down Expand Up @@ -229,6 +279,10 @@ export default function DraggableTextOverlays({

const isSelected = selectedTextId === overlay.id;
const isDragging = draggingId === overlay.id;
// Text outside its time is hidden, except the one being edited, which stays faint.
const visible = !hasTimedText || isTextVisibleAt(overlay, currentTime);
if (!visible && !isSelected) return null;
const fontSize = overlay.fontSize * scale;

return (
<div
Expand Down Expand Up @@ -258,7 +312,8 @@ export default function DraggableTextOverlays({
left: `${left}px`,
top: `${top}px`,
transform: "translate(-50%, -50%)",
fontSize: `${overlay.fontSize}px`,
fontSize: `${fontSize}px`,
opacity: visible ? 1 : 0.4,
color: overlay.color,
fontFamily: getFontFamily(overlay.fontFamily),
fontWeight:
Expand All @@ -267,9 +322,15 @@ export default function DraggableTextOverlays({
: overlay.fontWeight === "bold"
? 700
: 400,
textShadow: "0 2px 8px rgba(0, 0, 0, 0.8)",
whiteSpace: "nowrap",
textShadow: overlay.box ? undefined : "0 2px 8px rgba(0, 0, 0, 0.8)",
background: overlay.box ? "rgba(0, 0, 0, 0.55)" : undefined,
padding: overlay.box ? `${Math.max(2, fontSize * 0.3)}px` : undefined,
lineHeight: 1.2,
textAlign: "center",
whiteSpace: "pre",
}}
data-testid="text-overlay"
data-visible={visible}
aria-label={`Text overlay: ${overlay.text}`}
aria-pressed={isSelected}
>
Expand All @@ -283,7 +344,7 @@ export default function DraggableTextOverlays({
className="outline-none bg-black/30 px-1 py-0.5 rounded"
style={{
color: overlay.color,
fontSize: `${overlay.fontSize}px`,
fontSize: `${fontSize}px`,
fontFamily: getFontFamily(overlay.fontFamily),
fontWeight:
overlay.fontWeight === "900"
Expand Down
Loading
Loading