ffmpeg-python icon indicating copy to clipboard operation
ffmpeg-python copied to clipboard

drawtext filter: using text expansion with pts

Open ljwobker opened this issue 6 years ago • 4 comments

I'm working on a filter graph where (among other things) I'm using drawtext to write a timer onto a stream. I'm just building up a dictionary of the different drawtext parameters/values and then **passing that dict into the actual ().drawtext function call. Example:

timer1Params['text'] = '%%{pts}'           #### here be dragons ####
timer1Params['fontsize'] = relativeSize(md, 'h', 12)
timer1Params['x'] = roundUpToBase( (md['crop_w']  * .50), 20)
timer1Params['y'] = roundUpToBase( (md['crop_h'] * .03), 20)
timer1Params['enable'] = 'between(t,0,5)'

# and so on to build up the drawtext parameters...  then... 

    (
        ffmpeg
        .input(infile, ss=leadInFromFileStart, t=outputDuration)
        .drawtext(**timer1Params)
        .output(outfile)
        .run(capture_stdout=False, capture_stderr=False)
    )  

The problem is that I cannot for the life of me figure out how to properly encode/escape the "text" part, specifically when using the pts function.

For the dictionary value assigned to ['text'] I've tried all of the following and none behave the way I want ... help?!?? I have to imagine this is "just" my not knowing how to properly escape this, but I can't figure it out....

timer1Params['text'] = '"%"{pts}'    # prints as literal
timer1Params['text'] = '%{pts}'   # also prints as literal
timer1Params['text'] = '\\%{pts}'  # prints only the first slash
timer1Params['text'] = '%\{pts\}'   # prints \%{pts}

ljwobker avatar Oct 08 '19 19:10 ljwobker

So I was able to get this to work by using escape_test=False, but found in other issues that there was preference not to use this and instead fix the underlying issue (if we can find it).

The output of the compile() method ended up being:

text=%{eif\\\\:trunc(abs(150-n)/30)\\\\:d\\\\:2}.%{eif\\\\:mod(abs(150-n) \\,30) * 100 / 30\\\\:d\\\\:2}

ljwobker avatar Oct 10 '19 21:10 ljwobker

Hi @ljwobker, I'm superlate to the party, but for the life of me I cannot get it working. Do you happen to have a working example?

Thanks in advance.

mkudlacek avatar Aug 29 '23 14:08 mkudlacek

the closest I got is what's implemented here: https://github.com/ljwobker/jumpstamper/blob/master/jumpstamper.py

ljwobker avatar Sep 06 '23 19:09 ljwobker

That's an excellent source of information, thank you very much. It looks like we both do very similar thing. Mine are personalized videos from running competitions stitched from different sources, overlayed with images, text and other media, all created dynamically based on CSV data.

mkudlacek avatar Sep 06 '23 19:09 mkudlacek