How to Use FFmpeg sendcmd and asendcmd Filters

This article explains how to use FFmpeg's sendcmd (for video) and asendcmd (for audio) filters to dynamically modify filter parameters during playback or rendering. You will learn the command syntax, how to format command files, and how to execute these commands in real-time or at specific timestamps using practical code examples.

Understanding sendcmd and asendcmd

The sendcmd and asendcmd filters read a list of commands and pass them to other filters in the filtergraph at specified timestamps. This allows you to change parameters—such as volume levels, video cropping, overlays, or text content—without restarting the FFmpeg process or splitting the media into multiple segments.

The primary difference between the two is their target medium:

  • sendcmd is used within video filtergraphs (-vf).
  • asendcmd is used within audio filtergraphs (-af).

Command Syntax and Structure

Commands can be read from an external text file or passed as an inline string. Each command must follow this specific interval-based syntax:

[time] [target_filter] [command] [arguments];
  • [time]: The timestamp when the command triggers (e.g., 2.5 for 2.5 seconds, or 00:01:15 for 1 minute and 15 seconds).
  • [target_filter]: The name or instance label of the filter you want to target (e.g., volume, drawtext, hue).
  • [command]: The specific action. For most filters, this is reinit to reinitialize parameters.
  • [arguments]: The new key-value parameters for the target filter.

Example 1: Dynamic Audio Volume with asendcmd

To change the audio volume at specific timestamps, you can use the volume filter alongside asendcmd.

1. Create a command file (audio_commands.txt):

2.0 volume reinit volume=0.1;
5.0 volume reinit volume=1.0;
8.0 volume reinit volume=0.0;

At 2 seconds, the volume drops to 10%. At 5 seconds, it restores to 100%. At 8 seconds, it mutes completely.

2. Run the FFmpeg command:

ffmpeg -i input.mp3 -af "asendcmd=f=audio_commands.txt,volume=1" output.mp3

Example 2: Dynamic Text Overlay with sendcmd

You can dynamically change the text displayed on a video using the drawtext filter.

1. Create a command file (video_commands.txt):

3.0 drawtext reinit text='First Message';
7.0 drawtext reinit text='Second Message';

2. Run the FFmpeg command:

ffmpeg -i input.mp4 -vf "sendcmd=f=video_commands.txt,drawtext=text='Initial Text':x=50:y=50:fontsize=24:fontcolor=white" -c:a copy output.mp4

Using Inline Commands

If you prefer not to use an external text file, you can pass commands directly inside the filter chain using the c (command) parameter. You must properly escape the characters.

Here is how to change the brightness using the hue filter at 3 seconds without an external file:

ffmpeg -i input.mp4 -vf "sendcmd=c='3.0 hue reinit b=1.0',hue=b=0" -c:a copy output.mp4

Important Considerations

  • Filter Support: Not all FFmpeg filters support runtime parameter changes. The target filter must support the reinit command or specific custom commands. Refer to the specific FFmpeg filter documentation to verify runtime support.
  • Order in the Filtergraph: The sendcmd/asendcmd filter must be placed before the target filter in the filtergraph chain so that it can intercept and pass the parameters down the stream.