28

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?

4
  • ffmpeg will append or join same codec same rate things?
    – Psycogeek
    Apr 6, 2013 at 19:14
  • I suppose... @Psycogeek
    – Makaveli84
    Apr 6, 2013 at 19:21
  • And is it capable of generating a silent audio file of the exact same codec rate and channel? ffmpeg.org/ffmpeg-filters.html#Examples-34 ? Myself I use GUI methods, but doing what your asking should be fairly easy and very fast, as long as the parameters are all the same. Here is where it is shown adding it to a Mov. stackoverflow.com/questions/12368151/… in the answers
    – Psycogeek
    Apr 6, 2013 at 20:47
  • Use Ardour to add anything you want. If you want effects as well download the Calf plugins..
    – dashboard
    Apr 6, 2013 at 21:25

1 Answer 1

44

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).

  1. 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
    
  2. 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.

  3. 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 your ffmpeg is too old. Use version ≥4.2.

4
  • 5
    [Parsed_aevalsrc_0 @ 000000000195fec0] This syntax is deprecated. Use '|' to sep arate the list items. New syntax is -i "aevalsrc=0|0|0|0|0|0:d=1"
    – Zombo
    Mar 2, 2014 at 19:24
  • Can I do this for multiple files at once? Mar 19, 2019 at 23:47
  • @AaronFranke Yes, but ffmpeg does not have such a feature by itself. You would have to use your shell or a scripting language to loop the command. Your OS is unknown so I can't give you a turnkey solution, but see How do you convert an entire directory with ffmpeg? for some general info (skip the accepted answer as it is unnecessarily complex and inefficient).
    – llogan
    Mar 20, 2019 at 17:53
  • adelay also have usefull all option to delay (pad from beginning) all channels by some amount e.g. adelay=delays=1000:all=1 Apr 3, 2020 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.