Chain `drawtext` with ffmpeg-python

℡╲_俬逩灬. 提交于 2021-01-29 15:03:16

问题


I have a function within my python script to loop through some given text via an API but I am having issues drawing multiple text values:-

def add_text(title, text_lines):
    input = ffmpeg.input(f'output/{title}.mp4')
    for text in text_lines:
        output = input.drawtext(
            fontsize='24',
            start_number=0,
            text=text['text'],
            fontcolor='white',
            escape_text=True
        )
    ffmpeg.output(output, f'output/{title}-final.mp4').run()

This works if I have a single test value passed into the loop, but if I pass 2 or more values only the last value is drawn onto the video.

How am I able to loop through the text_lines and build up the output to include all of the values in the outputted file.

the array for text_lines looks like:-

[{text: 'Value 1'}, {text: 'Value 2'}, {text: 'Value 3',}]

I know they will be overlapping each other but that is not the issue at the moment, just the values apart from the last is not being shown on the video.

Any help would be greatly appreciated.

Thank you in advance.


回答1:


You need to reassign input (or use an intermediate variable). What's happening in each loop is you are taking the fresh input and drawing the text over that.

You could do something like

...
        input = input.drawtext(
...

I believe, though I haven't worked with videos much. Like, you might need to do more to make sure the text doesn't all just layer over itself.



来源:https://stackoverflow.com/questions/57841173/chain-drawtext-with-ffmpeg-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!