Skip to content

Open-source AI podcast clipper. Generate vertical clips with face tracking and burned-in captions. CLI, MCP server, web app.

3.0 KB
Raw Blame
import React from "react";
import {
  useCurrentFrame,
  useVideoConfig,
} from "remotion";
import type { Word, CaptionStyle } from "../types";
import { captionScale } from "../types";
import { buildChunks, activeChunkAt } from "../chunks";
import { MOTION, motionAt } from "../motion";
import type { Motion } from "../motion";
import { safeUpper } from "../text";

interface Props {
  words: Word[];
  style: CaptionStyle;
  singleLine?: boolean;
  motion?: Motion;
}

export const HormoziCaptions: React.FC<Props> = ({
  words, style, singleLine = false, motion,
}) => {
  const frame = useCurrentFrame();
  const { fps, height, durationInFrames } = useVideoConfig();
  const s = captionScale(height);
  const currentTime = frame / fps;

  const chunks = buildChunks(words, {
    perChunk: style.wordsPerChunk,
    absorbTail: 1,
    clipEnd: durationInFrames / fps,
  });

  const activeChunk = activeChunkAt(chunks, currentTime);

  if (!activeChunk) return null;

  const { opacity, scale } = motionAt({
    frame, fps,
    start: activeChunk.start,
    end: activeChunk.end,
    motion: motion ?? MOTION.hormozi,
  });

  return (
    <div
      style={{
        position: "absolute",
        bottom: style.marginBottom * s,
        left: 0,
        right: 0,
        display: "flex",
        justifyContent: "center",
        opacity,
        transform: `scale(${scale})`,
      }}
    >
      <div
        style={{
          backgroundColor: style.background ?? "rgba(0, 0, 0, 0.8)",
          borderRadius: 16 * s,
          padding: `${14 * s}px ${32 * s}px`,
          maxWidth: `calc(100% - ${120 * s}px)`,
          boxSizing: "border-box",
          overflowWrap: "anywhere",
          whiteSpace: singleLine ? "nowrap" : undefined,
          textAlign: "center",
          fontFamily: style.fontFamily,
          fontSize: style.fontSize * s,
          fontWeight: 800,
          lineHeight: 1.2,
          textShadow: "0 0 20px rgba(0, 0, 0, 0.5)",
        }}
      >
        {activeChunk.words.map((word, i) => {
          const isActive = currentTime >= word.start && currentTime < word.end;

          const text = style.uppercase
            ? safeUpper(word.word)
            : word.word;

          // The word being spoken still wins: the sweep is what this style is.
          // Emphasis colours it for the rest of the chunk, which is the part
          // somebody reads with the sound off.
          const emphasis = word.emphasis
            ? style.emphasisColor ?? style.activeColor
            : null;

          return (
            <React.Fragment key={i}>
              {i > 0 ? " " : ""}
              <span
                style={{
                  color: isActive ? style.activeColor : emphasis ?? style.color,
                  textShadow: isActive
                    ? `0 0 30px ${style.activeColor}60, 0 0 60px ${style.activeColor}30`
                    : "none",
                }}
              >
                {text}
              </span>
            </React.Fragment>
          );
        })}
      </div>
    </div>
  );
};