<Subtitles /> component renders animated subtitles based on timing data and styling configuration. It supports API-based configuration fetching and custom JSON styling.
Installation
Install the package in your React application:Basic Usage
The component fetches styling configuration automatically when you provide
apiKey, companyId, and clipId. You can also override with a custom styleConfig prop if needed.Required Props
| Prop | Type | Description |
|---|---|---|
words | Array<Word> | Array of word objects with timing data. Included in Clip object |
currentTime | number | Current playback time in seconds |
width | number | Canvas width in pixels - use standard dimensions (see below) |
height | number | Canvas height in pixels - use standard dimensions (see below) |
fps | number | Frames per second for animation |
How Width and Height Work
Thewidth and height props control how subtitle font sizes are calculated. Do not change these based on your video’s actual dimensions. Instead, use these standard values:
Standard Dimensions to Use:
- Horizontal videos (16:9):
width={1920}height={1080} - Vertical videos (9:16):
width={1080}height={1920}
- Always use standard dimensions: 1920x1080 for horizontal, 1080x1920 for vertical
- Don’t match actual video size: The dimensions are for font scaling, not video matching
- Component positioning: Place the Subtitles component over the video in your HTML structure with the same size
- Transparent overlay: The component is fully transparent except for the subtitle text
- Pointer passthrough: Mouse clicks and interactions pass through to the video below
The font size calculations are optimized for 1920x1080 (horizontal) and 1080x1920 (vertical). Using these standard dimensions means you don’t need to adjust fontSize values based on your video’s actual resolution.
Word Object Structure
Pass through the word array returned in the Clip object viatranscriptUrl. Each word object in the words array should follow this structure:
Optional Props
| Prop | Type | Default | Description |
|---|---|---|---|
styleConfig | StyleConfig | null | Styling configuration object |
x | number | width/2 | Horizontal position |
y | number | height/2 | Vertical position |
maxCharsPerLine | number | 12 | Maximum characters per line |
lineCount | number | 1 | Number of subtitle lines |
highlightWordsColor | string | - | Color for highlighted words |
currentFrame | number | - | Current frame number |
API Configuration
The component can fetch styling configuration from your API endpoint. Provide these three props to enable API mode:| Prop | Type | Description |
|---|---|---|
apiKey | string | Your API authentication key |
companyId | string | Your company identifier |
clipId | string | The clip identifier |
When all three API props are provided, the component will automatically fetch the styleConfig from your server, overriding any manually provided styleConfig.
StyleConfig Object
ThestyleConfig prop controls the visual appearance of subtitles. For detailed documentation on all available properties, see:
StyleConfig Properties
Complete reference for all styling options including text properties, effects, backgrounds, animations, and more.
Hooks
The package includes two powerful hooks for managing subtitle configurations programmatically.useUpdatePersistentSubtitleConfig
Update subtitle configuration in the database. Import:apiKey(string, required) - Your API authentication keycompanyId(string, required) - Your company identifierclipId(string, required) - The clip identifier
updateConfig(function) - Async function that accepts a partialStyleConfigobject and updates the database
- Merges with existing config (only updates specified fields)
- Preserves all other configuration values
- Returns a promise that resolves when the update is complete
useRefreshSubtitlesComponent
Refresh subtitle configuration from the database. Import:apiKey(string, required) - Your API authentication keycompanyId(string, required) - Your company identifierclipId(string, required) - The clip identifieronConfigRefreshed(function, optional) - Callback fired when config is successfully refreshed
refresh(function) - Async function that fetches the latest config from the databaseisRefreshing(boolean) - Loading state indicatorlastRefreshedConfig(StyleConfig | null) - The most recently fetched configuration
Configuration Priority
The component follows this priority order for configuration:1
API Config (Highest Priority)
If
apiKey, companyId, and clipId are all provided, the component fetches configuration from the API endpoint.2
Custom StyleConfig
If
styleConfig prop is provided and no API credentials are given.3
No Display
If neither API credentials nor styleConfig is provided, the component will not render anything.
Complete TypeScript Example
Troubleshooting
Subtitles Not Showing
No configuration provided:- Ensure either API credentials (all three: apiKey, companyId, clipId) OR a custom styleConfig is provided
- Component will not render without valid configuration
- Check that
currentTimeis within the range of your words’ start/end times - Verify words array has proper start/end values in seconds
API Errors
401 Unauthorized:- Verify your API key is correct
- Check that the API key has proper permissions
- Verify the companyId and clipId exist
- Check for typos in the IDs
Styling Issues
Text size wrong:- Always use standard dimensions:
width={1920} height={1080}for horizontal videos,width={1080} height={1920}for vertical videos - Adjust the
fontSizevalue in your styleConfig (e.g., change from"24"to"32")
- Adjust
xandyprops for precise positioning - Verify the Subtitles component is positioned over the video in your HTML/CSS
- Make sure you’re using standard dimensions
- Ensure
fpsmatches your expected framerate - Verify
currentTimeupdates smoothly (typically 30-60 times per second)
API Reference Summary
For production use, consider implementing error boundaries around the Subtitles component to gracefully handle API failures or invalid configurations.