Add 1 second of silence to audio through FFmpeg
I have an AC3 5.1 audio file to which I would like to insert x seconds of silent audio at the beginning. This has nothing to do with video muxing, so itsoffset is useless since it seems to only work with an audio stream accompanying a video one. I would like to achieve this with ffmpeg. Any ideas?
Solution 1:
Using the concat demuxer (add silence to beginning and/or end)
Use the concat demuxer if you want to avoid re-encoding the main segment. This methods works for adding silence to the beginning or end (or both).
-
Use the anullsrc audio source filter in to create the silent audio. You'll need to match the format, channel layout, and sample rate of the main audio file. Example to make a 5.1 channel, 48000 Hz sample rate, 1 second silent AC3 audio file (as this was what the format in the question):
ffmpeg -f lavfi -i anullsrc=channel_layout=5.1:sample_rate=48000 -t 1 silence.ac3
-
Now make a text file named
input.txt
that lists the files to be concatenated:file 'silent.ac3' file 'main.ac3'
If you want the silence at the end instead just switch the order of the files in
input.txt
. -
Now you can concatenate the files using the concat demuxer:
ffmpeg -f concat -i input.txt -codec copy output.ac3
Using the concat filter (add silence to the beginning and/or end)
Use the concat filter if you want to do everything in one command, or if you want to output to a different format than the input (since this method re-encodes anyway). This methods works for adding silence to the beginning or end or both.
Example to add 1 second of silence to the beginning of a stereo, 44100 Hz sample rate input:
ffmpeg -f lavfi -t 1 -i anullsrc=channel_layout=stereo:sample_rate=44100 -i audio.oga -filter_complex "[0:a][1:a]concat=n=2:v=0:a=1" output.m4a
Filtering will result in re-encoding while the concat demuxer will not.
In anullsrc it is recommended to match the channel layout and sample rate of the input.
If you want to add silence to the end instead just switch the order of the inputs given to the concat filter:
[1:a][0:a]concat=n=2:v=0:a=1
.
Using the adelay filter (add silence to beginning)
Use the adelay audio filter if you want to do everything in one command, or if you want to output to a different format than the input (since this method re-encodes anyway). This only works to add silence to the beginning of a file.
This example will add 1 second of silence to the beginning of a stereo input:
ffmpeg -i input.flac -af "adelay=1s:all=true" output.opus
If your ffmpeg
is old (version ≤4.2) you will need to declare milliseconds instead of seconds, and provide a delay for each channel:
ffmpeg -i input.flac -af "adelay=1000|1000" output.opus
- Filtering will result in re-encoding while the concat demuxer will not.
Using the apad filter (add silence to the end)
Use the apad audio filter if you want to do everything in one command, or if you want to output to a different format than the input (since this method re-encodes anyway). This only works to add silence to the end of a file.
This example will add 1 second of silence to the end:
ffmpeg -i input.wav -af "apad=pad_dur=1" output.m4a
Filtering will result in re-encoding while the concat demuxer will not.
If you get an error
Option 'pad_dur' not found
then yourffmpeg
is too old. Use version ≥4.2.