Generate Constant Tone with FFmpeg Sine Filter
Generating audio tones programmatically is a fast and efficient task
using FFmpeg's built-in sine audio filter. This article
provides a straightforward guide on how to configure the
sine filter to generate a constant tone, adjust its
frequency, define its duration, and specify a custom audio sample
rate.
The Basic Command
To generate a tone using the sine filter, use the
lavfi (Libavfilter) input format with the -f
flag. The parameters for frequency, sample rate, and duration are passed
directly to the filter.
ffmpeg -f lavfi -i "sine=frequency=1000:sample_rate=48000:duration=5" output.wavParameter Breakdown
-f lavfi: Specifies Libavfilter as the virtual input device.-i "sine=...": Calls thesinesource filter and passes its options:frequency(orf): Sets the tone's pitch in Hertz (Hz). The default value is 440 Hz (standard A4 pitch).sample_rate(orr): Sets the audio sampling frequency in Hz. The default is 44100 Hz. In the example above, it is explicitly set to 48000 Hz.duration(ord): Specifies the duration of the audio in seconds. If omitted, the tone will generate indefinitely until stopped manually.
output.wav: The destination audio file. You can output to formats such as.wav,.mp3, or.flac.
Alternative Method: Using
the -ar Flag
You can also control the sample rate using FFmpeg’s standard audio
rate option (-ar) rather than defining it inside the filter
string.
ffmpeg -f lavfi -i "sine=f=440:d=10" -ar 96000 output.wavIn this command, the sine filter generates the tone at
its default internal rate, and FFmpeg resamples the final stream to
96,000 Hz before encoding the output file. However, setting
sample_rate directly inside the filter is generally
preferred to avoid unnecessary resampling steps.