How to Use the FFmpeg sendcmd Filter
The sendcmd filter in FFmpeg is a powerful tool that
allows you to change filter parameters dynamically during video
processing without having to split your video into multiple segments.
This article provides a straightforward guide on how to use the
sendcmd (video) and asendcmd (audio) filters,
detailing their syntax, command file formatting, and practical examples
for real-time parameter manipulation.
Understanding sendcmd Syntax
The sendcmd filter works by reading a list of commands
and dispatching them to other filters in the filtergraph at specific
timestamps. You can pass these commands either through an external text
file or directly as an inline string.
The basic parameters for the filter are: * f: The path
to the text file containing the commands. * c: The inline
command string (used if you do not want to use an external file).
Command Format
Whether in a file or inline, commands must follow a specific syntax:
time [target_filter_name] command_name command_arguments;
- time: The timestamp when the command should
trigger. You can write this in seconds (e.g.,
5.0) or inHH:MM:SS.msecformat (e.g.,00:01:15.500). You can also use intervals likestart-end. - target_filter_name: The name of the filter you want to control (or its instance label).
- command_name: The specific command supported by the
target filter (usually
reinitfor changing parameters). - command_arguments: The new key-value pairs for the filter parameters.
Example 1: Changing Text Dynamically with drawtext
A common use case for sendcmd is changing the text
displayed by the drawtext filter at specific intervals.
Step 1: Create the Command File
Create a text file named commands.txt with the following
content:
2.0 drawtext reinit text='First Subtitle';
5.0 drawtext reinit text='Second Subtitle';
8.0 drawtext reinit text='Final Subtitle';
Step 2: Run the FFmpeg Command
Run FFmpeg, linking the command file to the sendcmd
filter and passing the commands to a labeled drawtext
filter:
ffmpeg -i input.mp4 -vf "sendcmd=f=commands.txt,drawtext=fontfile=Arial.ttf:text='Initial Text':fontsize=24:fontcolor=white:x=(w-tw)/2:y=h-100" -c:a copy output.mp4Example 2: Inline Commands (No File Required)
If you only have one or two simple commands, you can write them
inline using the c parameter. This eliminates the need for
an external text file.
The following command changes the brightness of a video using the
eq filter at the 3-second mark:
ffmpeg -i input.mp4 -vf "sendcmd=c='3.0 eq reinit brightness=0.5',eq=brightness=0" -c:a copy output.mp4Note: When writing inline commands, pay close attention to nested quotation marks to avoid command-line parsing errors.
Example 3: Dynamic Audio Volume Control (asendcmd)
To change audio parameters, use the audio equivalent:
asendcmd. This example changes the volume of an audio
stream dynamically.
Step 1: Create the Audio Command File
Create a file named audio_commands.txt:
0.0 volume reinit volume=1.0;
4.0 volume reinit volume=0.2;
8.0 volume reinit volume=1.0;
Step 2: Run the FFmpeg Command
Apply the commands to the volume filter:
ffmpeg -i input.mp4 -af "asendcmd=f=audio_commands.txt,volume=1.0" -c:v copy output.mp4Supported Filters
Not all FFmpeg filters support dynamic parameter modification via
sendcmd. To check if a filter supports commands, you can
look for the Timeline support and
Command support flags in the official FFmpeg documentation
for that specific filter. Some of the most commonly used compatible
filters include: * drawtext (for text properties) *
eq (for brightness, contrast, saturation) *
volume (for audio level adjustments) * hue
(for hue and saturation adjustments)