Frame Number Overlay With FFmpeg
33,888
You can use the drawtext filter with the n
or frame_num
function:
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 thestart_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}'
See the drawtext filter documentation for more info.
Related videos on Youtube
Author by
Kobi Versano
Updated on July 09, 2022Comments
-
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 over 11 yearsIt works! Is there any way to make it start the count from 1?
-
rogerdpack over 10 yearsthere may be an "eval" option you can throw in there to add it together...
-
Thomas over 10 yearson 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 over 7 yearsAdd
fontsize=200:
to actually see something ;) -
Atcold over 7 yearsI meant that one can increase the default font size, if needed.
-
user27665 almost 7 yearsI had to explicitly give the entire path of my ttf file which was /usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf
-
Sia Rezaei over 5 yearsI noticed that this significantly degrades the quality. Is there a way to avoid that?
-
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 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 over 5 yearsInput videos format is avi and codec is MJPG. Output video format is also avi, bu the codec comes out as FMP4
-
Zak44 about 5 yearswhat version of ffmpeg is this drawfilter available? Seem to not be working in the one I have. 2.5.3
-
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 about 5 yearsI see... well I tried the latest, and it seems to have just made the entire video black. Really odd. Something is amiss.
-
Zak44 about 5 yearsActually, 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 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 over 3 yearsyou can use [email protected] for a half transparent box color
-
tobiasBora over 2 yearsNote 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 about 2 yearsthis 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.