Drawtext, drawbox or overlay on only a single fram

2019-06-22 08:23发布

I'm using the drawtext and drawbox avfilters on FFmpeg, two of the most poorly documented functions known to man.

I'm struggling to work out if and how I can use them on only a single frame, i.e., drawtext on frame 22.

Current command:

ffmpeg -i test.wmv -y -b 800k -f flv -vcodec libx264 -vpre default -s 768x432 \
  -g 250 -vf drawtext="fontfile=/home/Cyberbit.ttf:fontsize=24:text=testical:\
  fontcolor=green:x=100:y=200" -qscale 8 -acodec libfaac -sn -vstats out.flv

Two elements mentioned in the documentation are n and t. However, I only seem to be able to use them in x and y. Not in text or even as other parameters.

Any help or FFmpeg guidance would be gratefully received.

1条回答
倾城 Initia
2楼-- · 2019-06-22 08:58

In a great example of FFmpeg always keeping you on your toes, this is trivial to do with drawtext and extremely painful with drawbox.

The key is that drawtext includes the draw parameter:

draw
Set an expression which specifies if the text should be drawn. If the expression evaluates to 0, the text is not drawn. This is useful for specifying that the text should be drawn only when specific conditions are met.

So to only show text on frame 22:

ffmpeg -i in.wmv -vf drawtext="fontfile=font.ttf:text='blah':draw='eq(n,22)'" out.flv

drawbox has no draw parameter, and there's no general way to emulate it, so you're left doing something like extracting the portion of video that you want to put the box on and then overlaying it with an offset:

ffmpeg -i in.wmv -t 1 -ss 10 -vf drawbox=10:10:20:20:red boxed.flv
ffmpeg -i in.wmv -itsoffset 10 -i boxed.flv -filter_complex overlay out.flv

(though this will leave the last frame of boxed.flv visible forever) or breaking up the video into multiple pieces, drawing on the proper pieces, and then recombining.

查看更多
登录 后发表回答