0

Having a issue with my code. I'm getting a list index out of range index error

import os
import moviepy.video.io.ImageSequenceClip
image_folder= r'C:\Users\Porsche\OneDrive - Imperial College London\Documents\plates'
fps=1

image_files = [image_folder+'/'+img for img in os.listdir(image_folder) if img.endswith(".jpeg")]
clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(image_files, fps=fps)
clip.write_videofile('my_video.mp4')

I'm new to Python and I can't seem to see where the index is and the documentation I found for moviepy was not clear.

the error is on this line clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(image_files, fps=fps)

1 Answers1

0

I just used ImageSequenceClip 5 minutes ago to successfully make a small video so maybe I can assist.

Below are issues related to what I saw in your code and related to problems I had with ImageSequenceClip also. I didn't experience an index error similar to what you've mentioned, but that may be due to your list comprehension line.

A few general suggestions--maybe this will be enough or helpful in other projects you work on:

  • Careful with your '/' and '\'; keep these uniform to avoid any unwanted issues popping up. I typically use / in all cases for Windows filesystems and it seems to work fine. Also, when manually combining path+filename variables don't forget to include a final '/' at the end of the path variable.
  • Print out and check the length of the image_files variable you create to make sure you are actually adding the files you wanted and there are no other obvious issues with your list comprehension line.
  • If you can't locate the issue causing the index error, you can try adding just the folder with the image files only (instead of a list of individual image file locations). In this case, you might need to make a new folder with the files you want included only.
  • the fps argument was counterintuitive, for me at least. The lower the value, the longer the duration of the individual images in the video.
  • Finally, the directory you provide to the ImageSequenceClip function will sort the files in alphanumeric order based on the filenames. Keep this in mind as, for example, a1, a2, a11 will be reordered into a1, a11, a2.
Dharman
  • 30,962
  • 25
  • 85
  • 135
user70711
  • 131
  • 1
  • 9