使用FFmpeg为视频添加文字时出错:无空格文字可正常添加
Hey there! This is such a common pitfall when working with FFmpeg's drawtext filter—let's get this sorted out quickly.
The Root Cause
When you include spaces in your text without proper quoting or escaping, your shell (like bash, zsh, or Windows Command Prompt) treats the space as a parameter separator. That means FFmpeg sees something like Hello as the value for text=, and then World gets interpreted as an unexpected extra parameter for the filter, which triggers the error.
Solutions
Here are the two most reliable ways to fix this:
Wrap the text value in single quotes (preferred for Linux/macOS)
Enclose the entire text string inside single quotes within the drawtext filter's argument. This tells the shell to pass the whole string as a single value.
Example correct command:ffmpeg -i input.mp4 -vf "drawtext=text='Hello World':fontsize=24:x=50:y=50" output.mp4Escape spaces with a backslash
If you prefer not to use quotes, you can add a backslash before each space to tell the shell to treat it as part of the string instead of a separator.
Example:ffmpeg -i input.mp4 -vf "drawtext=text=Hello\ World:fontsize=24:x=50:y=50" output.mp4Double quotes for Windows Command Prompt/PowerShell
On Windows, single quotes might not work as expected—use double quotes instead for the text value (note the double double-quotes to escape inner quotes for the shell):ffmpeg -i input.mp4 -vf "drawtext=text=""Hello World"":fontsize=24:x=50:y=50" output.mp4
Script Variable Tip
If you're using variables in a script (like a bash script), make sure you quote the variable when passing it to drawtext too. For example:
MY_TEXT="Hello Stack Overflow" ffmpeg -i input.mp4 -vf "drawtext=text='$MY_TEXT':fontsize=24" output.mp4
This ensures that even if the variable has spaces, it's passed as a single, intact value to the filter.
内容的提问来源于stack exchange,提问作者robert tamunoemi

