Search code examples
ffmpeg

format string in text of ffmpeg.drawtext(),i want the frame number to be 0001 , currently it is 1


i want the frame number to be 0001 , currently it is 1

ff = ffmpeg.drawtext(ff_drawtext,text ='%{n}',start_number=1,fontfile='C:/Windows/Fonts/Arial.ttf',fontcolor="white",x='w-(w/1.8)',y='h-(h/30)',fontsize="28",escape_text=False)

i use : text ="{:04n}".format(%'{n}) not unsuccessful

enter image description here


Solution

  • You will need to use eif or expr_int_format to achieve that.

    eif will give you a formatted (or padded) integer

    Bizarrely, on my box anyway, it does not work with frame_num so you have to substitute the frame number variable as n

    i.e.

    ffmpeg -i input.mp4 -vf "drawtext=text='%{expr_int_format\:n\:u\:5}': start_number=1: x=(w-tw)/2: y=h-(2*lh): fontcolor=black: fontsize=20: box=1: boxcolor=white: boxborderw=5" -c:a copy -f matroska - | ffplay -autoexit -i -

    u stands for unsigned integer (d would be signed, x would be hex)
    5 is the number of characters to use

    Caveat this is on Linux

    Which gives:

    enter image description here