How to use ffmpeg to fade in/out a veriable frame rate video clip with unknown duration I have been looking for a way to use ffmpeg to downscale some MPEG-4 (Base Media / Version 2) video files recorded with a variable frame rate, adding fade in/out effects at the head and tail of each clip.

Based on the answer provided here, I've been using the following command line to add a 2 second fade/out effect on to my clips:

ffmpeg -i VID_1.mp4 -sseof -2 -copyts -i VID_1.mp4 -lavfi "[1]fade=out:s=0:d=2[tail];[0][tail]overlay,fade=in:s=0:d=2[overlay]; anullsrc,atrim=0:4[audiotail];[0][audiotail]acrossfade=d=2,afade=d=2[audio];[overlay]scale=640:-1[video]" -map "[video]" -map "[audio]" -vsync 2 -sws_flags gauss -c:v libx264 -crf 18 -preset slow -shortest -report -hide_banner VID_1_out.mp4


My questions:


I seem to get a lot of the following error lines in the report


cur_dts is invalid (this is harmless if it occurs once at the start per stream)

Is it normal?


In the resulting output video clip, the fade-in comes out less than 2 seconds (about 1 second), and there appears no fade out, just a black-out for the last four seconds. Where is my command line wrong?
The duration of the original clip is 24s640ms, the output file is longer by 2s022ms. Why additional 22 ms? 


If I drop the -vsync 2 option, the output clip frame rate becomes exactly the same as that of the source video file. This, however doesn't deal away with the above issues.
This works here

ffmpeg -i VID_1.mp4 -sseof -2 -copyts -i VID_1.mp4 -lavfi "[1]fade=out:s=1:d=2[tail];[0][tail]overlay,fade=in:s=0:d=2,scale=640:-2[video];anullsrc,atrim=0:2.1[audiotail];[0][audiotail]acrossfade=d=2,afade=d=2[audio]" -map "[video]" -map "[audio]" -vsync 2 -sws_flags gauss -c:v libx264 -crf 18 -preset slow -shortest -report VID_1_out.mp4


With some videos, usually those with variable FPS, the fade filter only works with start frame as non-zero. Also, the anullsrc needs to be only slightly greater than the crossfade duration.