Fade video in and out with FFmpeg

Use fade for the picture and afade for the sound, with d for the length and st for the start time. A fade out needs st set to the clip length minus the fade, so read the length with ffprobe first. To crossfade two clips, use xfade and acrossfade with offset set to the first clip's length minus the transition.

Fade in Re-encode

The clip should start from black. Nothing to work out.

ffmpeg -i in.mp4 -vf fade=t=in:d=1 -af afade=t=in:d=1 out.mp4

Frame 0 is black. The picture reaches full brightness at 1.0 s and the sound rises with it over the same second.

Brightness at 0.5 s
40.6, source 65.6
Brightness at 1.0 s
64.4, source 64.3
Run time
1.0 s
Output
1.6 MB
In and out Re-encode

Both ends fade. You need the clip length for the fade out.

ffmpeg -i in.mp4 -vf "fade=t=in:d=1,fade=t=out:st=9:d=1" -af "afade=t=in:d=1,afade=t=out:st=9:d=1" out.mp4

In from black over 0 to 1 s, out to black over 9 to 10 s, sound faded in step. 240 frames, same as the input.

Brightness at 9.5 s
64.7, source 112.6
Sound at 9.9 s
-78.5 dB, source -54.4 dB
Run time
1.0 s
Output
1.6 MB

Proof

Frames and sound from the outputs

Grids read left to right, top to bottom. The waveforms are drawn on a log scale because the clip's sound is quiet.
fade=t=in:d=1, at 0.2, 0.5, 0.8 and 1.2 s Black to full over 1 s
Four frames of the girl at a window, going from almost black to full brightness
1 s At 0.2 s the frame is still dark. By 1.2 s it matches the source.
fade=t=out:st=9:d=1, at 8.8, 9.2, 9.5 and 9.8 s Full to black over the last second
Four frames of the same shot getting darker toward black
st=9 8.8 s is untouched. The fade starts at 9 s and the last frame, at 9.958 s, is close to black.
xfade=transition=fade:duration=1:offset=3 A 1 s crossfade from 3 s
Four frames: the city, the city blending into the girl and dragon, then the girl and dragon
7.0 s At 2.9 s only a.mp4. At 3.3 s and 3.7 s both clips mix. At 4.1 s only b.mp4. Two 4 s clips make 7 s.
xfade=transition=wipeleft Same timing, a wipe
Four frames: the city, then the girl and dragon sliding in from the right as a wipe
Only the transition name changed. The edge moves right to left over the same second.
in.mp4, sound
Waveform of the input's sound, full level from start to end
The source starts and ends at full level.
afade=t=in:d=1, afade=t=out:st=9:d=1 Sound faded with the picture
Waveform of the faded output's sound, rising over the first second and falling over the last
At 0.1 s the level was -56.0 dB against -39.9 dB in the source. At 9.9 s, -78.5 dB against -54.4 dB.

Footage: Sintel, © Blender Foundation, CC BY 3.0.

Run it

Run it on Rendobar

Send the same command from your code. The job fetches each input from its URL and returns the output file's URL when it finishes.

job.ts
import { createClient } from "@rendobar/sdk";
const rb = createClient({ apiKey: process.env.RENDOBAR_API_KEY });
const job = await rb.jobs.run({
type: "ffmpeg",
inputs: {
"in.mp4": "https://cdn.rendobar.com/examples/fade-in-out/in.mp4",
},
params: {
command: "ffmpeg -i in.mp4 -vf \"fade=t=in:d=1,fade=t=out:st=9:d=1\" -af \"afade=t=in:d=1,afade=t=out:st=9:d=1\" out.mp4",
},
});
console.log(job.output.file.url);

Install with npm i @rendobar/sdk. jobs.run() submits and waits, so it returns the finished job in one call.

terminal
curl https://api.rendobar.com/jobs \
-H "Authorization: Bearer $RENDOBAR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"type": "ffmpeg",
"inputs": {
"in.mp4": "https://cdn.rendobar.com/examples/fade-in-out/in.mp4"
},
"params": {
"command": "ffmpeg -i in.mp4 -vf \"fade=t=in:d=1,fade=t=out:st=9:d=1\" -af \"afade=t=in:d=1,afade=t=out:st=9:d=1\" out.mp4"
}
}'

Returns immediately with a job id. Poll GET /jobs/{id} or register a webhook rather than blocking on the request.

Every variant

Pick the command for your case

Each one ran on the same input. The line under it is what came out. Run times are the best of three runs with FFmpeg 8.0 on one desktop machine, so read them as relative.

  1. Fade the picture only and keep the audio as it is

    Re-encode
    ffmpeg -i in.mp4 -vf "fade=t=in:d=1,fade=t=out:st=9:d=1" -c:a copy out.mp4

    Same picture as the in-and-out command. The audio stream is copied untouched.

    1.0 s run 1.6 MB 10.00 s long

  2. Fade the audio only

    Re-encode
    ffmpeg -i in.mp4 -af "afade=t=in:d=2,afade=t=out:st=8:d=2" -c:v copy out.mp4

    Two-second fades on the sound. The video is copied, so the picture is identical to the input.

    325 ms run 2.4 MB 10.01 s long

  3. Fade from and to white

    Re-encode
    ffmpeg -i in.mp4 -vf "fade=t=in:d=1:c=white,fade=t=out:st=9:d=1:c=white" -c:a copy out.mp4

    Frame 0 is white (brightness 235, the top of the range). The last frame is 229.7.

    1.3 s run 1.6 MB 10.00 s long

  4. Crossfade two clips

    Re-encode
    ffmpeg -i a.mp4 -i b.mp4 -filter_complex "[0:v][1:v]xfade=transition=fade:duration=1:offset=3[v];[0:a][1:a]acrossfade=d=1[a]" -map "[v]" -map "[a]" out.mp4

    7.0 s of video (168 frames) and 7.02 s of audio. The clips overlap for 1 s from 3 s, picture and sound together.

    984 ms run 1.6 MB 7.02 s long

  5. Use a wipe or slide instead of a fade

    Re-encode
    ffmpeg -i a.mp4 -i b.mp4 -filter_complex "[0:v][1:v]xfade=transition=wipeleft:duration=1:offset=3[v];[0:a][1:a]acrossfade=d=1[a]" -map "[v]" -map "[a]" out.mp4

    Same length and timing as the crossfade. wipeleft is one of the names xfade takes, along with dissolve, slideleft, circleopen and others.

    948 ms run 1.4 MB 7.02 s long

  6. Crossfade three clips in a row

    Re-encode
    ffmpeg -i a.mp4 -i b.mp4 -i in.mp4 -filter_complex "[0:v][1:v]xfade=transition=fade:duration=1:offset=3[ab];[ab][2:v]xfade=transition=fade:duration=1:offset=6[v];[0:a][1:a]acrossfade=d=1[aab];[aab][2:a]acrossfade=d=1[a]" -map "[v]" -map "[a]" out.mp4

    4 + 4 + 10 s minus two 1 s overlaps: 16.0 s of video, 384 frames. The second offset is 6, the 7 s the first pair makes minus 1.

    2.0 s run 3.2 MB 16.03 s long

Gotchas

Where it goes wrong

How do I fade out at the end of a video?

Give fade=t=out a start time. st defaults to 0, so without it the fade out runs over the first second and the rest of the video stays black:

ffmpeg -i in.mp4 -vf fade=t=out:d=1 -c:a copy out.mp4

Brightness 69.5 on frame 0, 41.1 at 0.5 s, then 16 (black) from 1 s to the end of the 10 s file.

fade cannot count back from the end, so read the length first and set st to the length minus the fade:

ffprobe -v error -show_entries format=duration -of csv=p=0 in.mp4

10.000000, so a 1 s fade out takes st=9.

Why does xfade fail with a size or timebase error?

xfade needs both clips at the same size, frame rate and timebase. Clips from different cameras or exports rarely match. With the second clip scaled down, FFmpeg stopped with:

First input link main parameters (size 1280x546) do not match the corresponding second input link xfade parameters (size 640x274)

With the second clip at 30 fps:

First input link main timebase (1/12288) do not match the corresponding second input link xfade timebase (1/30)

Bring both inputs to one size, frame rate, pixel aspect and timebase before xfade:

ffmpeg -i a.mp4 -i b.mp4 -filter_complex "[0:v]fps=24,scale=1280:546,setsar=1,settb=AVTB[x];[1:v]fps=24,scale=1280:546,setsar=1,settb=AVTB[y];[x][y]xfade=transition=fade:duration=1:offset=3[v];[0:a][1:a]acrossfade=d=1[a]" -map "[v]" -map "[a]" out.mp4

7.0 s, 168 frames at 24 fps. We also ran it with the second clip turned into 30 fps first, and it worked. settb alone was not enough there: FFmpeg then stopped on the frame rate.

Why are picture and sound out of step after a crossfade?

xfade starts the transition at offset. acrossfade always overlaps the end of the first clip. They only line up when offset is the first clip's length minus the transition. With two 4 s clips and offset=2:

ffmpeg -i a.mp4 -i b.mp4 -filter_complex "[0:v][1:v]xfade=transition=fade:duration=1:offset=2[v];[0:a][1:a]acrossfade=d=1[a]" -map "[v]" -map "[a]" out.mp4

6.0 s of video against 7.02 s of audio. The picture cut a second out of a.mp4 and the sound did not.

For a chain, each offset is the length of everything joined so far minus the transition. In the three-clip command the first pair makes 7 s, so the second offset is 6.

Related: Merge videos, Trim a video, Mute a section, Loop a video.

Your command. Our infrastructure.

Bring your own commands. Run them on scalable CPU and GPU infrastructure, and pay only for compute time.