1

I use FFMPEG to print a frame counter on my videos, but I have two issues:

demo GIF

  1. The text stutters
  2. I'd like to have the possibility to have the number zero-padded (I.E. write 001 002 003 instead of 1 2 3).

Code:

@echo off
:again

cd /D %~p1

ffmpeg ^
    -i "%~nx1" ^
    -vf "drawtext=fontfile=arialbd.ttf: text='Frame \: %%{n}': start_number=1: x=(w-tw)/2: y=h-(2*lh): fontcolor=white: fontsize=40: box=1: boxcolor=black@0.4: boxborderw=8" ^
    -c:a copy ^
    "%~p1%~n1_framenumbered.mov"
if NOT ["%errorlevel%"]==["0"] goto:error
echo [92m%~n1 Done![0m

shift
if "%~1" == "" goto:end
goto:again

:error

echo [93mThere was an error. Please check your input file or report an issue on github.com/L0Lock/FFmpeg-bat-collection/issues.[0m
pause
exit 0

:end

cls
echo [92mEncoding succesful. This window will close after 10 seconds.[0m
timeout /t 10

Solutions:

  • use `text='Frame : %{eif:n:d:3}' to get the zero-padded frame count (thanks to this answer)
  • use a monospace font (courrier new is common on Windows)
  • the script was failing to load the font, use the full path instead but without the drive (thanks to this answer):
    • Do: /Windows/Fonts/courbd.ttf
    • Don't C:/Windows/Fonts/courbd.ttf nor use \
L0Lock
  • 147
  • 13
  • I'm unsure what's wrong. It's about using ffmpeg via batch file, so I guess the solutions to my issues can be either ffmpeg's arguments or additional batch scripts, if not both, and I hope people knowing better than me can help me find what can be done. – L0Lock Oct 02 '20 at 10:27

1 Answers1

2

Use the eif function.

Below is how it is used on the command line. You'll want to escape it for use in a batch file. 3 is the field width. For best results, use a fixed-width font.

text='Frame \: %{eif\:n\:d\:3}'
Gyan
  • 85,394
  • 9
  • 169
  • 201
  • Indeed, that does the trick, thank you! I also noticed my script also failed to load the font file, fixed using [this answer](https://stackoverflow.com/a/54923686/10705720). If I understand well, [eif aka expr_int_format](https://ffmpeg.org/ffmpeg-filters.html#Text-expansion) basically outputs an integer, `\:n` feeds eif with the current frame value (as in my initial script) and `\:d\:3` makes eif output a 3 **d**igits integer. – L0Lock Oct 02 '20 at 10:52