kkroening / ffmpeg-python

Python bindings for FFmpeg - with complex filtering support
Apache License 2.0
9.88k stars 884 forks source link

drawtext filter: using text expansion with pts #276

Open ljwobker opened 4 years ago

ljwobker commented 4 years ago

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 commented 4 years ago

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}

mkudlacek commented 1 year ago

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.

ljwobker commented 1 year ago

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

mkudlacek commented 1 year ago

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.