问题
I'm trying to apply a caption using drawtext that should disappear one second before the video end:
ffmpeg -i input.mp4 -vf "drawtext=enable='between(t,0,5)':fontfile=font.ttf:text='Some caption':x=60:y=640:fontsize=40:fontcolor=#f0f0f0@0.9" -vcodec libx264 -crf 27 -preset ultrafast -strict -2 -acodec copy output.mp4
The problem is that I don't know the video length beforehand. I've tried using 'between(t,0,-1)' but it doesn't work, the caption never shows up. Anyone knows if is there a way to do this without having to open the video first to check length and only after that draw the caption? Thanks in advance!
回答1:
FFmpeg does not convey the stream duration to filters, so this has to be done in a roundabout manner.
FFmpeg has a sseof function that can seek from the end of a file. It also has a copyts option to maintain timestamps. So, we load the input twice, once the full input, and the other, just the last second. Then we draw the text on the entire 1st input but overlay the last second from the 2nd input, which because of the retained timestamps, will be burnt in place.
ffmpeg -copyts -i input.mp4 -sseof -1 -i input.mp4 -filter_complex "[0]drawtext=fontfile=font.ttf:text='Some caption':x=60:y=640:fontsize=40:fontcolor=#f0f0f0@0.9[txt];[txt][1]overlay" -vcodec libx264 -crf 27 -preset ultrafast -acodec copy output.mp4
来源:https://stackoverflow.com/questions/56113372/ffmpeg-how-to-use-between-to-select-last-second