0

I am having trouble adding text masks to a video that will be added at different times with different durations. I am using moviepy

def text_on_video():
    clip = VideoFileClip("C:/Users/14col/PycharmProjects/RedditVideos/output.mp4")

    # clipping of the video
    # getting video for only starting 10 seconds
    clip = clip.subclip(0, 30)

    starts = [3, 10, 18]  # or whatever
    durations = [2, 5, 2]

    texts = ['first','second','third']
    txt_clips = []

    for text, t, duration in zip(texts, starts, durations):
        txt_clip = TextClip(text, fontsize=40, color='white')
        txt_clip = txt_clip.set_start(t)
        txt_clip = txt_clip.set_pos('center').set_duration(duration)
        txt_clips.append(txt_clip)

    lin = [0, 1, 2, 3, 4, 5, 6, 7, 8]
    string = ''
    count = 0
    for i in range(len(texts)):
        string += 'txt_clips[' + str(count) + ']' + ','
        count += 1
    print(string)

    video = CompositeVideoClip([clip,string])
    video.write_videofile('video.mp4')

I have found that if I create for loop to add text masks over a video then only the last one is returned like this:

for text in txt_clips:
video = CompositeVideoClip([clip,text])
    video.write_videofile('video.mp4')

with text clips being a list of texts to add to the video.

I know the video will produce correctly is the code is like so:

video = CompositeVideoClip([clip,txt_clips[0],txt_clips[1],txt_clips[2],])

with each text written separately. My response to this was to produce a string with the correct number of txt_clips like so:

lin = [0,1,2,3,4,5,6,7,8]
string = ''
count = 0
for i in range(len(lin)):
    string += 'txt_clips[' + str(count)+']' +','
    count+=1
print(string)

which gives me txt_clips[0],txt_clips[1],txt_clips[2],txt_clips[3],txt_clips[4],txt_clips[5],txt_clips[6],txt_clips[7],txt_clips[8],

but does not work when i use video = CompositeVideoClip([clip,string]) because it gives me AttributeError: 'str' object has no attribute 'end'

colem
  • 15
  • 4

0 Answers0