brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ffmpeg fade and afade add a fade in and fade out

Ffmpeg
~/tips/tip-ffmpeg-fade-in-out

ffmpeg -i in.mp4 \

Fades the picture and the sound in at the start and out at the end of a clip.

A clip that starts and ends with a hard cut feels abrupt. FFmpeg has one filter for the picture and one for the sound, and each takes a start time and a duration.

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

fade works on video and afade on audio. t is the type, in or out, st is the start time in seconds and d is how long the fade lasts. The example fades in over the first second and fades out over the last second of a 10 second clip.

The fade-out start has to be calculated by you: clip length minus fade length. Get the length with ffprobe -v error -show_entries format=duration -of csv=p=0 in.mp4. FFmpeg does not know “the last second” for this filter, and a wrong st fades out too early or never.

Fading the picture and not the sound, or the other way round, sounds odd, so I’d do both. Filters mean re-encoding, so -c copy cannot be combined with this, and the video is encoded again with the default settings unless you pick a codec and a -crf.

Further reading: FFmpeg filters: fade, FFmpeg filters: afade

More Ffmpeg tips ffmpeg -stream_loop 2 -i in.mp4 -c copy out.mp4 ffmpeg -i video.mp4 -i video.en.srt -map 0 -map 1 -c copy -c:s mov_text -metadata:s:s:0 language=eng video-subs.mp4 ffmpeg -i film.mkv -map 0:v -map 0:a:0 -c:v copy -c:a aac -b:a 192k -ac 2 film-stereo.mkv ffmpeg -i talk.mp4 -vn -c:a copy talk.m4a ffmpeg -n -i "$f" -c copy "${f%.mkv}.mp4" All Ffmpeg tips (38) All topics

Link