Menu

1 / 2

How to Fade In and Out a Video With FFmpeg (Anchor the Fade to the Last Frame, Fade the Audio Too, and Test It With pytest)

DEV Community: tutorial·Voor AI·2 days ago
#LbyL245j
#devtotutorial#devto#dev#fade#frame
Reading 0:00
15s threshold

To fade a video in and out with FFmpeg, re-encode it with two fade filters and two afade filters, and set the fade-out start from the timestamp of the last frame , not from the file's duration:

ffmpeg -i in.mp4 \ -vf "fade=t=in:st=0:d=1,fade=t=out:st=11.0:d=1" \ -af "afade=t=in:st=0:d=1,afade=t=out:st=11.064:d=1" \ -c:v libx264 -crf 18 -pix_fmt yuv420p -c:a aac -b:a 128k out.mp4

Enter fullscreen mode

Exit fullscreen mode

Here 11.0 is "last frame at 12.000 s minus a 1 s fade" and 11.064 is "audio stream ends at 12.064 s minus 1 s". The rest of this post shows where those numbers come from, the three ways the obvious command goes wrong, and a five-test pytest file that proves the result instead of eyeballing it.

The test clip

Everything below was measured on one real file: a 12-second vertical example from a video generator, 720x1280, H.264 at 24 fps with 289 frames, plus AAC stereo audio at 32 kHz with people talking from the first frame.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.