In a Python-based TTS (Text-to-Speech) video generation pipeline using WhisperX for character-level audio alignment, characters at segment boundaries (e.g., end of paragraphs or code blocks) were delayed in rendering. This occurred because WhisperX distributed text timestamps uniformly across the entire audio duration, including silent pauses between segments, causing a visual delay of up to 1.5 seconds.
Issue
Characters at the end of audio segments (e.g., the last few characters of a paragraph or code block) would not render visually until the subsequent audio segment began. For example:
- The word “response structu” would appear at 2.45s, but “re.” only rendered at 4.08s (during the next segment).
- This delay occurred consistently at segment boundaries, disrupting visual synchronization with audio.
Culprit
WhisperX’s proportional alignment algorithm treated silent pauses between segments as valid text regions, assigning timestamps to characters during these pauses. This caused:
- Characters to receive timestamps in regions where no audio was being rendered.
- A delay in visual rendering until the audio playback reached those timestamps.
- Inconsistent synchronization between text and audio, particularly at segment boundaries.
Solution
Post-Processing Boundary Clipping
Implemented a post-processing clipping mechanism to adjust character timestamps at segment boundaries:
- Boundary Detection: Identified the end times of each audio segment.
- Character Realignment: Adjusted the start and end times of characters near segment boundaries to ensure they rendered within the correct audio segment.
- Safety Offset: Introduced a small offset (e.g., 50ms) to prevent zero-duration characters and ensure smooth rendering.
Design Principles
- Boundary Awareness: Ensured characters aligned with the actual audio flow rather than silent pauses.
- Non-Destructive Adjustments: Maintained the integrity of character-level timing while resolving boundary misalignments.
- Simplicity: Avoided complex preprocessing by relying on post-processing adjustments.
Impact: Characters now render synchronously with audio across segment boundaries, eliminating visual delays and improving the overall alignment between text and speech.