How to Use asendcmd Filter in FFmpeg

This guide explains how to use the asendcmd filter in FFmpeg to send commands to other audio filters at specific timestamps during processing. You will learn the basic syntax of the filter, how to format command files, and how to execute FFmpeg commands to dynamically change audio parameters like volume and panning.

Understanding asendcmd

The asendcmd (audio send command) filter allows you to pass commands to other filters in your filtergraph at precise intervals. This is highly useful for automated audio editing, such as fading volume up or down at specific times, changing panning, or adjusting equalizer settings dynamically without splitting the audio file into multiple segments.

To use asendcmd, you must:

  1. Define the commands and their execution times (either in an external text file or inline).
  2. Assign a unique instance name to the target filter so asendcmd knows where to send the instructions.

Command File Syntax

If you are using an external file to specify commands, the syntax for each line is:

time [enter|leave] target command arg;
  • time: The timestamp when the command should trigger (e.g., 5.0 for 5 seconds, or 00:01:15 for 1 minute 15 seconds).
  • target: The instance name of the target filter (defined in your filtergraph using @name).
  • command: The specific parameter of the target filter you want to change.
  • arg: The new value for the parameter.
  • semicolon ;: Each command line must end with a semicolon.

Example Command File (commands.txt)

Create a text file named commands.txt with the following contents to dynamically alter volume:

2.0 volume volume 0.2;
5.0 volume volume 1.0;
8.0 volume volume 0.0;

In this example:

  • At 2.0 seconds, the filter named volume will reduce the gain to 0.2.
  • At 5.0 seconds, the volume will restore to full (1.0).
  • At 8.0 seconds, the volume will mute (0.0).

Running the FFmpeg Command

Once you have your command file, run FFmpeg using the asendcmd filter. You must link asendcmd to the target filter in your audio filtergraph (-af).

ffmpeg -i input.mp3 -af "asendcmd=f=commands.txt,volume@volume=1.0" output.mp3

Breakdown of the Command:

  • -i input.mp3: Specifies your input audio file.
  • -af: Begins the audio filtergraph.
  • asendcmd=f=commands.txt: Loads the asendcmd filter and points it to your external text file.
  • ,: Connects the filters in a chain.
  • volume@volume=1.0: Initializes the volume filter with a default value of 1.0. The @volume syntax assigns the unique instance name "volume" to this filter, matching the target name used in commands.txt.

Using Inline Commands

If you only have a few simple commands, you can write them directly inside the FFmpeg command line using the c (commands) parameter instead of an external file. This avoids the need to manage extra text files.

ffmpeg -i input.mp3 -af "asendcmd=c='3.0 volume volume 0.5; 6.0 volume volume 1.0',volume@volume=1.0" output.mp3

Note: When writing inline commands, ensure you wrap the commands in single quotes to prevent your command line shell from misinterpreting the spaces and semicolons.