10

I understand there is a module built in FFmpeg which allows you to fade your clips in and out, however, no one really explains how to use the module with your own input. How do I simply fade both the audio and video in and out without re-encoding? (If that's possible)

5 Answers 5

14

Fading in is no problem, because you start from the timestamp 0 (or from the other known timestamp or frame number), e.g. use:

ffmpeg -i input.mp4 -vf fade=in:0:d=5 -af afade=in:0:d=5 output.mp4 

for 5-second fading in from the start of your clip.

Ignore the error

Only '-vf fade=in:0:d=5' read, ignoring remaining -vf options: Use ',' to separate filters 

— see the answer of the question FFmpeg read, ignoring remaining -vf options: Use ',' to separate filters.


To fade out, you have to provide either

  • the start position (the default parameter), i.e.

    • the start frame (s=) for video, or
    • the start sample (ss=) for audio,

    or

  • the start time (st=) (this is the same parameter name for both audio and video)

of the fade out.

Let's use the second option — the start time of fading out.

First, figure out the total duration of video or audio (you may find it with FFprobe), then subtract the desired fade-out length from the length of the video.

For example, if you have a 10-minute video (= 600 seconds) and desire a 5-second fade out, you obtain 600 - 5 = 595.

Add filters for fading out to the previous command:

ffmpeg -i input.mp4 -vf fade=in:0:d=5,fade=out:st=595:d=5 -af afade=in:0:d=5,afade=out:st=595:d=5 output.mp4 

Notes:

  1. Filtering always require re-encoding.

  2. As in and 0 are defaults, you may shorten the command to

     ffmpeg -i input.mp4 -vf fade=d=5,fade=out:st=595:d=5 -af afade=d=5,afade=out:st=595:d=5 output.mp4 
3

As the topic asks for ffmpeg in general, an additional info to the answer of MarianD if you have only audio; there are no frames, so you have to use the start time option and for that you have to use key/value pairs (at least for fading out):

ffmpeg -i input.m4a -af afade=in:st=0:d=5,afade=out:st=595:d=5 output.m4a 
3
  • I by mistake rejected your edit of my answer, in spite that your proposal was correct. Please excuse me and repeat your edit (there is no way for me how to approve it if I already rejected it). Then I may approve it. Commented Jun 19, 2023 at 16:14
  • @MarianD, sorry, I remember it was only a small formatting change, but I don't remember it ... and I don't see it in the history ... Commented Jul 6, 2023 at 13:42
  • It was almost at the end, in the Note 2: afade=out=595:5. You corrected the second = into :. Commented Jul 6, 2023 at 15:55
1

Re-encoding is required in these cases because filters are being used.

Fading in video (optionally from a start time other than 0 seconds; in this example, 10 seconds) requires specifying the start time - if you want to start somewhere other than the very beginning of the input - as well as the fade duration in the filters:

ffmpeg -ss 00:00:10.0 -i input.mp4 -vf "fade=type=in:start_time=0:duration=5" -af "afade=type=in:start_time=0:duration=5" fadein.mp4 

Fading out video (unless you actually want to fade out at the existing end of the video) requires both applying the audio/video fade filters at the right time, and trimming the video, or the end result will fade out but remain at the original length. Let's say you want to fade out starting at 20 seconds, for a duration of 5 seconds. The video will end up being 25 seconds, so it needs trimming to that, as well as the filters being applied:

ffmpeg -t 00:00:25 -i input.mp4 -vf "fade=type=out:start_time=20:duration=5" -af "afade=type=out:start_time=20:duration=5" fadeout.mp4 
0

You can not do fade out/in without reencoding.

3
  • 1
    Ah damn, how would I do with re-encoding? Commented Aug 14, 2019 at 17:24
  • @Mint Incorrect. Commented Aug 28, 2023 at 15:59
  • 1
    @lmat-ReinstateMonica Yeah sorry, thanks for calling me out. idk what I was thinking that day. It's totally possible to fade in/out when you re-encode. (deleted my old comment) Commented Aug 30, 2023 at 1:14
0

I might be a bit later here, but to apply seamless fade-in / fade-out, you need just to apply that ordered filters in the end of filters chain:

  • fade-in audio/video
  • reverse audio/video
  • fade-in audio/video
  • reverse audio/video

Using audio processing as example:

ffmpeg ... -af '...,afade=t=in:st=0:d=5,areverse,afade=t=in:st=0:d=5,areverse' ... 

That thing applies 5s fade effects on both start and end of audio. It should be noted that on large input files, the reverse filter can yield higher memory consumption, so in such scenarios it would be better to inspect/calculate the duration of audio/video file (and embed that on command).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.