Search code examples
variablesffmpegframe

How to display a frame number on each frame of a video using ffmpeg?


The test command I have is as follows:

ffmpeg -i in.mov -vf "drawtext=fontfile=/usr/share/fonts/ttf-bitstream-vera/Vera.ttf: x=(w-tw)/2: y=h-(2*lh)-n: fontcolor=white: fontsize=40: box=1: boxcolor=0x00000000@1: text=" -an -y out.mov

I want to display the frame number on each frame. However, setting the "text" variable to "n" does not help (it displays n) and %n seems to be the new line symbol. The thing that confuses me most is that this part

y=h-(2*lh)-n

works well, meaning it gets the right value of the current frame and moves the text up accordingly.


Solution

  • Ok, this feature was not there in ffmpeg, but now it is. And this is how you do it:

    ...drawtext=text=%{n}:expansion=normal:fontfile=/usr/share/fonts/ttf-bitstream-vera/Vera.ttf:other_options...
    

    Note that expansion should be set to normal, which is not the default now, but should be default in the future (according to the ffmpeg documentation).