50

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.

llogan
  • 121,796
  • 28
  • 232
  • 243
Kobi Versano
  • 537
  • 1
  • 5
  • 9

1 Answers1

78

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.

llogan
  • 121,796
  • 28
  • 232
  • 243
  • It works! Is there any way to make it start the count from 1? – Kobi Versano Mar 13 '13 at 10:23
  • 1
    there may be an "eval" option you can throw in there to add it together... – rogerdpack Jan 21 '14 at 17:55
  • 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 – Thomas Mar 27 '14 at 14:59
  • 4
    Add `fontsize=200:` to actually see something ;) – Atcold Feb 19 '17 at 23:32
  • I meant that one can increase the default font size, if needed. – Atcold Feb 20 '17 at 01:39
  • 1
    I had to explicitly give the entire path of my ttf file which was /usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf – user27665 Aug 29 '17 at 13:21
  • I noticed that this significantly degrades the quality. Is there a way to avoid that? – Sia Rezaei Mar 26 '19 at 20:12
  • @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 Mar 28 '19 at 14:41
  • @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 Mar 29 '19 at 18:17
  • Input videos format is avi and codec is MJPG. Output video format is also avi, bu the codec comes out as FMP4 – Sia Rezaei Apr 01 '19 at 13:47
  • what version of ffmpeg is this drawfilter available? Seem to not be working in the one I have. 2.5.3 – Zak44 May 22 '19 at 19:17
  • @Zak44 Probably since at least FFmpeg 0.8. [drawtext requires ffmpeg to be built with `--enable-libfreetype`](https://ffmpeg.org/ffmpeg-filters.html#drawtext) so yours is probably missing that. 2.5.3 is ancient so you should update regardless. See [FFmpeg Download](https://ffmpeg.org/download.html) page for links to builds for Linux, macOS, and Windows. Most of these should have drawtext included. – llogan May 22 '19 at 19:22
  • I see... well I tried the latest, and it seems to have just made the entire video black. Really odd. Something is amiss. – Zak44 May 23 '19 at 00:30
  • 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! – Zak44 May 23 '19 at 00:37
  • 1
    @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. – llogan May 23 '19 at 22:19
  • you can use black@0.5 for a half transparent box color – JSBach Oct 21 '20 at 17:50
  • 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. – tobiasBora Mar 25 '22 at 08:24
  • 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. – Anuj Saxena May 11 '22 at 06:59