How to Use the FFmpeg anullsrc Filter
This article provides a practical guide on how to use the
anullsrc filter in FFmpeg to generate silent audio streams.
You will learn the basic syntax of this audio source filter, how to
customize its sample rate and channel layout, and how to combine it with
video files to create silent audio tracks.
The anullsrc (audio null source) filter is a virtual
audio device in FFmpeg’s lavfi (Libavfilter) input format.
It is primarily used to generate a continuous stream of silent audio,
which is useful when you need to pad audio, create placeholder tracks,
or add a silent audio channel to a video file that lacks sound.
Basic Syntax
To use anullsrc, you must specify the virtual input
format -f lavfi before the input flag -i.
The simplest command to generate an infinite stream of silence is:
ffmpeg -f lavfi -i anullsrc output.wavNote: Since this command generates an infinite stream, you must
limit the duration using the -t option (for duration in
seconds) or -to option.
Generating a Silence File with a Specific Duration
To create a silent audio file of a specific duration, use the
-t parameter. The following command generates a 10-second
silent MP3 file:
ffmpeg -f lavfi -i anullsrc -t 10 silence.mp3Configuring Sample Rate and Channel Layout
By default, anullsrc generates a stereo stream at a
sample rate of 44100 Hz. You can customize these parameters using the
sample_rate (or r) and
channel_layout (or cl) options.
To generate a 5-second mono silent WAV file at 48000 Hz:
ffmpeg -f lavfi -i anullsrc=channel_layout=mono:sample_rate=48000 -t 5 silence.wavCommon channel layouts include mono,
stereo, 5.1, and 7.1.
Adding a Silent Audio Track to a Video
Many video platforms and players require an audio track to play files
correctly. If you have a silent video (input.mp4) and want
to add a silent audio track to it, you can map the video and the
anullsrc filter together.
Use the -shortest flag to ensure the output file stops
encoding as soon as the video stream ends:
ffmpeg -i input.mp4 -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=48000 -c:v copy -c:a aac -shortest output.mp4In this command: * -i input.mp4 loads the original
video. * -f lavfi -i anullsrc=... generates the silent
audio. * -c:v copy copies the video stream without
re-encoding to save time and quality. * -c:a aac encodes
the generated silence into the standard AAC audio format. *
-shortest terminates the output file when the shortest
input (the video) ends.