运行 当我使用 MoviePy 时进入 OSError
Running into OSError when I use MoviePy
但是,我 运行 在使用 MoviePy 处理一些视频时遇到了一些错误。 :
Traceback (most recent call last):
File "C:\Users\...\Desktop\Python\Testsecondsgifs.py", line 75, in <module>
main()
File "C:\Users\...\Desktop\Python\Testsecondsgifs.py", line 69, in main
short_clip = make_clips(vid,clip_dur,i,parts)
File "C:\Users\...\Desktop\Python\Testsecondsgifs.py", line 30, in make_clips
clip = (vid.subclip(t1, t2))
File "<decorator-gen-39>", line 2, in subclip
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\decorators.py", line 89, in wrapper
return f(*new_a, **new_kw)
File "<decorator-gen-38>", line 2, in subclip
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\decorators.py", line 29, in apply_to_mask
newclip = f(clip, *a, **k)
File "<decorator-gen-37>", line 2, in subclip
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\decorators.py", line 41, in apply_to_audio
newclip = f(clip, *a, **k)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\Clip.py", line 391, in subclip
newclip = self.fl_time(lambda t: t + t_start, apply_to=[])
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\Clip.py", line 189, in fl_time
keep_duration=keep_duration)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\Clip.py", line 136, in fl
newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
File "<decorator-gen-57>", line 2, in set_make_frame
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\decorators.py", line 14, in outplace
f(newclip, *a, **k)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\video\VideoClip.py", line 694, in set_make_frame
self.size = self.get_frame(0).shape[:2][::-1]
File "<decorator-gen-14>", line 2, in get_frame
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\decorators.py", line 89, in wrapper
return f(*new_a, **new_kw)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\Clip.py", line 95, in get_frame
return self.make_frame(t)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\Clip.py", line 136, in <lambda>
newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\Clip.py", line 188, in <lambda>
return self.fl(lambda gf, t: gf(t_func(t)), apply_to,
File "<decorator-gen-14>", line 2, in get_frame
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\decorators.py", line 89, in wrapper
return f(*new_a, **new_kw)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\Clip.py", line 95, in get_frame
return self.make_frame(t)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\video\io\VideoFileClip.py", line 103, in <lambda>
self.make_frame = lambda t: self.reader.get_frame(t)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 175, in get_frame
self.initialize(t)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 103, in initialize
self.proc = sp.Popen(cmd, **popen_params)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 842, in __init__
_cleanup()
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 505, in _cleanup
res = inst._internal_poll(_deadstate=sys.maxsize)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 1259, in _internal_poll
if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
OSError: [WinError 6] The handle is invalid
我该如何解决这个问题?
这只会在我尝试循环访问 VideoFileClip 函数时发生。我可以在 windows 上使用并行多处理解决这个问题。
例如:
from multiprocessing.dummy import Pool as ThreadPool
# The number 5 is the number of iterations you need in parallel,
# for example if you want to process 5 videos with your function.
pool = ThreadPool(5)
#var1 and var2 or any number of vars should be a list
#pool.starmap will iterate these vars through your function in parallel
def myfunction(var1, var2):
clip = VideoFileClip('path')
#Do something
clip.reader.close()
clip.close()
pool.starmap(myfunction, zip(var1, var2))
显然,当你完成迭代后,如果你再次尝试使用 VideoFileClip 执行你的函数,你将得到 Handle is invalid 错误,你将不得不重置你的 jupyter 笔记本,但至少你可以在没有的情况下使用你的函数收到任何 os 或 python 错误,您需要的次数,使用 pool.starmap
但是,我 运行 在使用 MoviePy 处理一些视频时遇到了一些错误。 :
Traceback (most recent call last):
File "C:\Users\...\Desktop\Python\Testsecondsgifs.py", line 75, in <module>
main()
File "C:\Users\...\Desktop\Python\Testsecondsgifs.py", line 69, in main
short_clip = make_clips(vid,clip_dur,i,parts)
File "C:\Users\...\Desktop\Python\Testsecondsgifs.py", line 30, in make_clips
clip = (vid.subclip(t1, t2))
File "<decorator-gen-39>", line 2, in subclip
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\decorators.py", line 89, in wrapper
return f(*new_a, **new_kw)
File "<decorator-gen-38>", line 2, in subclip
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\decorators.py", line 29, in apply_to_mask
newclip = f(clip, *a, **k)
File "<decorator-gen-37>", line 2, in subclip
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\decorators.py", line 41, in apply_to_audio
newclip = f(clip, *a, **k)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\Clip.py", line 391, in subclip
newclip = self.fl_time(lambda t: t + t_start, apply_to=[])
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\Clip.py", line 189, in fl_time
keep_duration=keep_duration)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\Clip.py", line 136, in fl
newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
File "<decorator-gen-57>", line 2, in set_make_frame
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\decorators.py", line 14, in outplace
f(newclip, *a, **k)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\video\VideoClip.py", line 694, in set_make_frame
self.size = self.get_frame(0).shape[:2][::-1]
File "<decorator-gen-14>", line 2, in get_frame
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\decorators.py", line 89, in wrapper
return f(*new_a, **new_kw)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\Clip.py", line 95, in get_frame
return self.make_frame(t)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\Clip.py", line 136, in <lambda>
newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\Clip.py", line 188, in <lambda>
return self.fl(lambda gf, t: gf(t_func(t)), apply_to,
File "<decorator-gen-14>", line 2, in get_frame
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\decorators.py", line 89, in wrapper
return f(*new_a, **new_kw)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\Clip.py", line 95, in get_frame
return self.make_frame(t)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\video\io\VideoFileClip.py", line 103, in <lambda>
self.make_frame = lambda t: self.reader.get_frame(t)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 175, in get_frame
self.initialize(t)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 103, in initialize
self.proc = sp.Popen(cmd, **popen_params)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 842, in __init__
_cleanup()
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 505, in _cleanup
res = inst._internal_poll(_deadstate=sys.maxsize)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 1259, in _internal_poll
if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
OSError: [WinError 6] The handle is invalid
我该如何解决这个问题?
这只会在我尝试循环访问 VideoFileClip 函数时发生。我可以在 windows 上使用并行多处理解决这个问题。
例如:
from multiprocessing.dummy import Pool as ThreadPool
# The number 5 is the number of iterations you need in parallel,
# for example if you want to process 5 videos with your function.
pool = ThreadPool(5)
#var1 and var2 or any number of vars should be a list
#pool.starmap will iterate these vars through your function in parallel
def myfunction(var1, var2):
clip = VideoFileClip('path')
#Do something
clip.reader.close()
clip.close()
pool.starmap(myfunction, zip(var1, var2))
显然,当你完成迭代后,如果你再次尝试使用 VideoFileClip 执行你的函数,你将得到 Handle is invalid 错误,你将不得不重置你的 jupyter 笔记本,但至少你可以在没有的情况下使用你的函数收到任何 os 或 python 错误,您需要的次数,使用 pool.starmap