0

So I am trying to pickle.dump() a dictionary in python. Here is the general structure of my dictionary:

{"key": [tkinter.Button, moviepy.VideoFileClip, int]}

etc. Now I am not able to run the pickle.dump() on the moviepy.VideoFileClip object - it comes up with this error:

cannot pickle '_thread.lock' object

does anyone know how I can save a dictionary with a moviepy videofileclip to a file (I might try numpy arrays soon)

1 Answers1

0

You cannot pickle a MoviePy VideoFileClip because it doesn't store all the video data inside it, it lazily loads the video data from an FFmpeg process when it is requested.

You'll have to convert the VideoFileClip to a numpy array with np.array(clip.iter_frames()), which converts the returned generator into a numpy array.

Tom Burrows
  • 2,225
  • 2
  • 29
  • 46
  • Ok! Does this also include the audio or will I also have to do VideoFileClip.audio.to_soundarray()? – Mini Minnow Mar 20 '21 at 17:08
  • This is only the video, you will have to handle audio and/or mask separately, yes. – Tom Burrows Mar 20 '21 at 17:17
  • When I try the `np.array(clip.iter_frames())` instead of going through the generator it just stores the object? – Mini Minnow Mar 20 '21 at 20:26
  • Oh ok, you can just do `list(clip.iter_frames())` or `np.array(list(clip.iter_frames()))` then? – Tom Burrows Mar 20 '21 at 20:31
  • Ok - only slight issue is that this takes a long time (lots longer than audio.to_soundarray()) - is there any way to speed this up? It lags out my computer a lot – Mini Minnow Mar 20 '21 at 20:39
  • Well yes, you’re loading the entire video into memory, which is a bad idea. Apparently I forgot to mention that bit in my answer. You shouldn’t really be doing this at all. Just keep the video file and read it in via another VideoFileClip when you need it. – Tom Burrows Mar 20 '21 at 22:03
  • But I am making a video editor and afiak the one I tested out auto saved the video file? is there some way I can save it with the lazy ffmpeg saving you mentioned? – Mini Minnow Mar 21 '21 at 07:56
  • Just save a reference to the file name and create a new VideoFileClip when you need it (presumably on project load?). If you want to ensure that the video is not deleted then you could copy the file into somewhere that your program controls and then use that copy instead. Idk if video editors do this, I suspect they they don’t, and that they just rely on video files still being in their original location. – Tom Burrows Mar 21 '21 at 11:28
  • Yes I think they rely on them being in the same place. Problem is the users can cut/trim the clips (so they aren't the same as the original video file) – Mini Minnow Mar 21 '21 at 13:06
  • Oh right, I see the issue. You'd have similar issues if you just saved `clip.iter_frames` though, as not everything is baked into the frames (such as fps). I guess you need to write an alternative to `VideoFileClip` that does that allows you to close the reader before pickling and reopen it when you unpickle. – Tom Burrows Mar 21 '21 at 14:28
  • Sounds very complicated - I think I'll just make it easy to do stuff fast so people won't have to save it – Mini Minnow Mar 21 '21 at 14:47