用ffmpeg-python链接`drawtext` [英] Chain `drawtext` with ffmpeg-python

查看:61
本文介绍了用ffmpeg-python链接`drawtext`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的python脚本中有一个函数,可以通过API遍历某些给定的文本,但是在绘制多个文本值时遇到问题:-

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()

如果我将单个测试值传递到循环中,则此方法有效,但是如果我传递2个或更多值,则仅将最后一个值绘制到视频上.

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.

我如何遍历 text_lines 并建立输出以将所有值包括在输出文件中.

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.

text_lines的数组如下:-

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.

任何帮助将不胜感激.

谢谢.

推荐答案

您需要重新分配输入(或使用中间变量).每个循环中发生的事情是您正在使用新鲜 输入并在其上绘制文本.

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.

您可以做类似的事情

...
        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.

这篇关于用ffmpeg-python链接`drawtext`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆