-2

[This is a screenshot ofHeres a screenshot of the error code block]2Heres the code:

import os
from moviepy.editor import *

Memes = []
numLabel = 0

for pictures in os.listdir("memes"):
    ImageClip("memes/" + pictures).set_duration(5)
    Memes.append(pictures)

video = concatenate(Memes, method="compose")
video.write_videofile('test.mp4', fps=24)

I get this error:

'str' object has no attribute 'duration'

as you can see there is a duration assigned to the list of memes please help me I wasted 3 days on this error

Matthew
  • 1
  • 1
  • Hey, could you edit your question to include your full imports. Seems like line 2 is cut off. Also the full error log would be nice to see where the error exactly occurs. – Juhuja Feb 04 '23 at 12:53
  • i just uploaded screenshots of the error and code block although the code on the origanal post is the whole code but i uploaded a screenshot just in case – Matthew Feb 04 '23 at 12:59
  • The line `ImageClip("memes/" + pictures).set_duration(5)` creates an `ImageClip` object..... but is never assigned to anything or stored anywhere. Did you perhaps mean to append it to the `Memes` list? Because the `Memes` passed to the concatenate call are not "list of images" but are just the list of filenames from `os.listdir("memes")`,.. which are plain strings. – Gino Mempin Feb 04 '23 at 13:40

2 Answers2

0

First of all, you shouldn't write from moviepy.editor import *.
Write from moviepy.editor import ImageClip, concatenate to import only the class and function you need. Plus concatenate is deprecated. Use concatenate_videoclips instead: from moviepy.editor import ImageClip, concatenate_videoclips.

The line ImageClip("memes/" + pictures).set_duration(5) will also not work. You have to assign the ImageClip to a variable to append it to the list. ImageClip has the parameter duration in it's constructure to use. The line should look like this: clip = ImageClip("memes/" + pictures, duration=5)
And now append the clip to the list: Memes.append(clip).

This should work and generate the expected result.

Keanu Hie
  • 297
  • 2
  • 8
  • 12
0

I can see what you're trying to do and put my spin on it. Firstly you need to put all the images into a list. Then you need to change all the images in the list to imageclips with durations and fps which is probably what is causing your error message. Then you can concatenate it however you wish. Remember, Never put concatenate of any kind inside a loop, Moviepy bugs out.

import os
import moviepy.editor as mp
from moviepy.editor import * #As the other guy said, importing everything from moviepy editor will slow it down substantially so it's best to just import what you need instead of *

Memes = [] #this is gonna be images
slides = [] #this is gonna be imageclips
size = (1920,1080) #Size of the images must be the same or you'll have to reformat them to be the same
image_dir = "Z:blablabl/blabla/blabla" #This is the folder with all the images

#This is the correct way to put whatever image into a list
for filename in os.listdir(image_dir):
    if filename.endswith(".jpg") or filename.endswith(".png"):
        Memes.append(os.path.join(image_dir, filename))

for n, url in enumerate(Memes): #All Images in image_dir is now in slideshow
    slides.append(
    mp.ImageClip(url).set_fps(25).set_duration(5).resize(size) #Change 5 into whatever you want duration to be and fps too
)

video = mp.concatenate_videoclips(slides)
video.write_videofile("test4.mp4")
Electro
  • 17
  • 8
  • You can merge the two for loops for the slightest amounts of efficiency, but I purposely typed it this way so it's step by step instead because I have a strong feeling this is just a small slice of what you're making. – Electro Feb 05 '23 at 05:50