问题
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