Resolved

WhisperX whitespace normalization breaks character alignment in code blocks

Python
AI/ML
WhisperX
Debugging
Text Processing
Video Generation
Syntax Highlighting
Whitespace

WhisperX collapsing 4 spaces to 1 space caused 3-character offset in character timing alignment | Resulted in text rendering as code after Python blocks due to mismatched character indices

Issue

Text segments incorrectly adopted code formatting and styling after Python code blocks, leading to visual corruption where normal text was rendered as code. Analysis revealed a 3-character offset between WhisperX’s output (3425 characters) and the original text (3428 characters), caused by WhisperX normalizing 4-space Python indentation into a single space. This discrepancy disrupted character mapping and syntax highlighting, causing subsequent characters to be misaligned with their intended segments.

Culprit

The root cause was WhisperX’s internal whitespace normalization, which converted 4-space indentation into a single space in its character output. While this normalization was applied to preserve alignment accuracy, it introduced a discrepancy between the original text’s formatting and WhisperX’s output. This offset propagated through all subsequent character mappings, leading to incorrect syntax highlighting and visual corruption.

Solution

To resolve the issue, a dual-source approach was implemented:

  1. Character Source: The original text was used to preserve formatting, including 4-space indentation.
  2. Timing Source: WhisperX’s timing data was retained for accurate character alignment.

By mapping original characters to WhisperX’s timing data, the solution ensured that syntax highlighting and visual rendering remained consistent with the original text’s formatting while preserving the accuracy of WhisperX’s alignment. This approach eliminated the 3-character offset without altering WhisperX’s normalization behavior, thus maintaining alignment accuracy and correct visual representation.