Enable CTL and Shaping Using libfribidi in FFmpeg
This article provides a quick guide on how to enable Complex Text
Layout (CTL) and bidirectional (BiDi) text shaping in FFmpeg using the
libfribidi library. You will learn how to configure and
compile FFmpeg with the necessary dependencies and how to apply the
drawtext filter to correctly render languages like Arabic,
Hebrew, Persian, and Urdu on your videos.
Step 1: Compile FFmpeg with libfribidi Support
To use libfribidi for text shaping and CTL, you must
compile FFmpeg from source with the library enabled. FFmpeg does not
enable this by default.
First, ensure you have the development libraries for
fribidi, freetype (required for drawing text),
and optionally harfbuzz (for advanced glyph shaping)
installed on your system.
On Debian/Ubuntu-based systems, run:
sudo apt-get install libfribidi-dev libfreetype6-dev libharfbuzz-devWhen configuring your FFmpeg build, you must explicitly pass the compile flags to enable these libraries. Run the configure script with the following options:
./configure --enable-gpl --enable-libfreetype --enable-libfribidi --enable-libharfbuzzAfter configuring, compile and install FFmpeg:
make -j$(nproc)
sudo make installStep 2: Verify libfribidi Integration
To confirm that your FFmpeg installation successfully includes
libfribidi, run the following command in your terminal:
ffmpeg -versionLook at the configuration section in the output. You should see
--enable-libfribidi in the list of enabled configuration
options.
Step 3: Render CTL and BiDi Text Using drawtext
Once FFmpeg is compiled with libfribidi support, the
drawtext video filter will automatically detect and process
bidirectional and complex text layouts. You do not need to pass extra
parameters to force shaping; libfribidi handles the text
reordering and shaping seamlessly in the background.
Use the following basic command structure to overlay right-to-left (RTL) or complex text onto a video:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/supported-font.ttf:text='שלום עולם':fontcolor=white:fontsize=24:x=(w-tw)/2:y=(h-th)/2" -codec:a copy output.mp4Key Considerations for Success:
- Font Selection: Ensure the font specified in
fontfileactually supports the Unicode character set of the target language (e.g., using Arial, Amiri, or Noto Sans). If the font lacks the required glyphs, the text will not render correctly despitelibfribidibeing active. - UTF-8 Encoding: Ensure your terminal, script, or input text file is saved in UTF-8 encoding so FFmpeg can correctly parse the complex characters.