I understand this has been issued many times but I thought this has been solved altogether. Following is my code ..
from moviepy.editor import *
from random import randint
import os
import subprocess
files = os.listdir('.')
for sourceVideo in files:
if (sourceVideo[-4:] == ".mp4") or (sourceVideo[-4:] == ".wmv"):
framerate=10
filename=sourceVideo
print(filename)
clip = VideoFileClip(filename)
print( clip.duration )
if clip.duration < 600:
for x in range(1,int(clip.duration/60)):
#print( int(clip.duration/60) )
fi = randint(1,int(clip.duration/60))
if x == 1:
suclip=VideoFileClip(filename).subclip(fi,fi+3).resize(height=182, width=325)
else:
suclip =concatenate_videoclips([suclip,VideoFileClip(filename).subclip(fi+(60*x),fi+3+(60*x))]).resize(height=182, width=325)
if clip.duration > 600:
for x in range(1,int(int(clip.duration/60)/2)):
#print( int(int(clip.duration/60)/2) )
fi = randint(1,118)
if x == 1:
suclip=VideoFileClip(filename).subclip(fi,fi+2).resize(height=182, width=325)
else:
suclip =concatenate_videoclips([suclip,VideoFileClip(filename).subclip(fi+(120*x),fi+2+(120*x))]).resize(height=182, width=325)
suclip.write_gif(filename+".gif",fps=framerate)
Above code looks for all mp4 or wmv format files and then for each file gets 2 or 3 sec duration subclips and merges them and then convert it to gifs.
Code works fine for first mp4 file in folder. But when it reaches to second one it throws error as below.
Now, I have seen following links for solutions. Getting "OSError: [WinError 6] The handle is invalid" in VideoFileClip function
https://github.com/Zulko/moviepy/issues/73
and many more similars.
All of them suggesting to add one or another of below solutions like adding following..
clip.reader.close()
clip.audio.reader.close_proc()
clip.__del__()
del suclip
del clip
subprocess._cleanup()
I have tried putting all of them at the end of my code , after it writes gif file. But error still persists.
It works and creates gif of first video of folder without any problem or error at all. File size of each mp4 is around 100MB.
I also tried on using this
adition=VideoFileClip(filename).subclip(fi+(120*x),fi+2+(120*x))
suclip = concatenate_videoclips([suclip,adition]).resize(height=182, width=325)
del adition
instead of this
suclip =concatenate_videoclips([suclip,VideoFileClip(filename).subclip(fi+(120*x),fi+2+(120*x))]).resize(height=182, width=325)
but it didn't help.
Any solutions will be appreciated. Thank you. :)