'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 1:[1]

ffmpeg -i input.mp4 -vf "drawtext=fontfile=Arial.ttf: text='%{frame_num}': start_number=1: x=(w-tw)/2: y=h-(2*lh): fontcolor=black: fontsize=20: box=1: boxcolor=white: boxborderw=5" -c:a copy output.mp4

You can use this command in the terminal or Powershell. when you use this command with the f string in IDE so you can use this format

import os

cmd =f'''ffmpeg -i input.mp4 -vf "drawtext=fontfile=Arial.ttf: text='%{{frame_num}}': start_number=1: x=(w-tw)/2: y=h-(2*lh): fontcolor=black: fontsize=20: box=1: boxcolor=white: boxborderw=5" -c:a copy output.mp4'''

print(cmd)

os.system(cmd)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anuj Saxena