Frame Number Overlay With FFmpeg

33,888

You can use the drawtext filter with the n or frame_num function:

enter image description here
Looping 5 fps example

Example command:

ffmpeg -i input -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
  • You may have to provide the full path to the font file, such as fontfile=/usr/share/fonts/TTF/Vera.ttf.
  • n/frame_num starts at 0, but you can make the frame count start from 1 with the start_number option as shown in the example.

You could add additional text if desired, but be aware that you have to escape some special characters:

text='Frame\: %{frame_num}'

enter image description here

See the drawtext filter documentation for more info.

Share:
33,888

Related videos on Youtube

Kobi Versano
Author by

Kobi Versano

Updated on July 09, 2022

Comments

  • Kobi Versano
    Kobi Versano almost 2 years

    I need to overlay the frame number to each frame of a video file using ffmpeg for windows.

    I succeeded in overlaying a timecode stamp with the drawtext filter using this code:

    ffmpeg -i video.mov -vcodec r210 -vf "drawtext=fontfile=Arial.ttf: timecode='01\:00\:00\:00': r=25: x=(w-tw)/2: y=h-(2*lh): fontcolor=white: box=1: boxcolor=0x00000099" -y output.mov
    

    However, I need a frame number overlay and not a timecode one. Any help would be appreciated.

  • Kobi Versano
    Kobi Versano over 11 years
    It works! Is there any way to make it start the count from 1?
  • rogerdpack
    rogerdpack over 10 years
    there may be an "eval" option you can throw in there to add it together...
  • Thomas
    Thomas over 10 years
    on Win: if error >>Could not load font "Arial.ttf"<< occur you can copy the font e.g. "Arial" from the Windows\fonts-folder to the Image-File-Folder
  • Atcold
    Atcold over 7 years
    Add fontsize=200: to actually see something ;)
  • Atcold
    Atcold over 7 years
    I meant that one can increase the default font size, if needed.
  • user27665
    user27665 almost 7 years
    I had to explicitly give the entire path of my ttf file which was /usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf
  • Sia Rezaei
    Sia Rezaei over 5 years
    I noticed that this significantly degrades the quality. Is there a way to avoid that?
  • Sia Rezaei
    Sia Rezaei over 5 years
    @llogan I was using your command exactly as you have it here. I solved quality degradation by adding the -qscale 1 option to your command. It still degrades the quality noticeably (and reduces file size by ~30%) , but was good enough for my purposes. Thanks!
  • Sia Rezaei
    Sia Rezaei over 5 years
    @llogan here it is: ffmpeg -i input -qscale 1 -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
  • Sia Rezaei
    Sia Rezaei over 5 years
    Input videos format is avi and codec is MJPG. Output video format is also avi, bu the codec comes out as FMP4
  • Zak44
    Zak44 about 5 years
    what version of ffmpeg is this drawfilter available? Seem to not be working in the one I have. 2.5.3
  • llogan
    llogan about 5 years
    @Zak44 Probably since at least FFmpeg 0.8. drawtext requires ffmpeg to be built with --enable-libfreetype so yours is probably missing that. 2.5.3 is ancient so you should update regardless. See FFmpeg Download page for links to builds for Linux, macOS, and Windows. Most of these should have drawtext included.
  • Zak44
    Zak44 about 5 years
    I see... well I tried the latest, and it seems to have just made the entire video black. Really odd. Something is amiss.
  • Zak44
    Zak44 about 5 years
    Actually, I take that back. It seems to work, but has made the video unplayable in Quicktime. I can open it in VLC and see the frame counter, but the video is no longer recognized as legit in Quicktime, odd!
  • llogan
    llogan about 5 years
    @Zak44 Try appending format=yuv420p to the filterchain (join the filters with a comma). But this is just a guess without seeing the command and log.
  • JSBach
    JSBach over 3 years
    you can use [email protected] for a half transparent box color
  • tobiasBora
    tobiasBora over 2 years
    Note that if the video is made from a concatenation of video (tested with mp4), it will restart the counter from zero when the concatenated video change. The only solution I found to this problem was to re-encode the video.
  • Anuj Saxena
    Anuj Saxena about 2 years
    this code only works in terminal but in IDE (Pycharm) is not executed properly. it treated frame_num as a variable when we replame frame_num with 0 or 1 it is executed make a video but this video has no watermark of current frame it is only simple video with no watermark.