Python 在 VideoFileClip 函数中获取“OSError: [WinError 6] The handle is invalid”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43966523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:34:40  来源:igfitidea点击:

Getting "OSError: [WinError 6] The handle is invalid" in VideoFileClip function

pythonjupyter-notebookmoviepy

提问by Deepak Tekchandani

I am creating a program using pythonby importing moviepylibrary, but getting following error:

我正在python通过导入moviepy库创建程序,但出现以下错误:

from moviepy.editor import VideoFileClip

white_output = 'videos/testVideo.mp4'
clip1 = VideoFileClip("videos/testVideo.mp4")

OSError                                   Traceback (most recent call last)
<ipython-input-40-f49638833528> in <module>()
      1 white_output = 'videos/testVideo.mp4'
----> 2 clip1 = VideoFileClip("videos/testVideo.mp4")
      3 white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!
      4 get_ipython().magic('time white_clip.write_videofile(white_output, audio=False)')

C:\Users\hp pc\Anaconda3\envs\lib\site-packages\moviepy\video\io\VideoFileClip.py in __init__(self, filename, has_mask, audio, audio_buffersize, audio_fps, audio_nbytes, verbose)
     53         # Make a reader
     54         pix_fmt= "rgba" if has_mask else "rgb24"
---> 55         reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt)
     56         self.reader = reader
     57         # Make some of the reader's attributes accessible from the clip

C:\Users\hp pc\Anaconda3\envs\lib\site-packages\moviepy\video\io\ffmpeg_reader.py in __init__(self, filename, print_infos, bufsize, pix_fmt, check_duration)
     30 
     31         self.filename = filename
---> 32         infos = ffmpeg_parse_infos(filename, print_infos, check_duration)
     33         self.fps = infos['video_fps']
     34         self.size = infos['video_size']

C:\Users\hp pc\Anaconda3\envs\lib\site-packages\moviepy\video\io\ffmpeg_reader.py in ffmpeg_parse_infos(filename, print_infos, check_duration)
    236         popen_params["creationflags"] = 0x08000000
    237 
--> 238     proc = sp.Popen(cmd, **popen_params)
    239 
    240     proc.stdout.readline()

C:\Users\hp pc\Anaconda3\envs\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds)
    840                  pass_fds=()):
    841         """Create new Popen instance."""
--> 842         _cleanup()
    843         # Held while anything is calling waitpid before returncode has been
    844         # updated to prevent clobbering returncode if wait() or poll() are

C:\Users\hp pc\Anaconda3\envs\lib\subprocess.py in _cleanup()
    503 def _cleanup():
    504     for inst in _active[:]:
--> 505         res = inst._internal_poll(_deadstate=sys.maxsize)
    506         if res is not None:
    507             try:

C:\Users\hp pc\Anaconda3\envs\lib\subprocess.py in _internal_poll(self, _deadstate, _WaitForSingleObject, _WAIT_OBJECT_0, _GetExitCodeProcess)
   1257             """
   1258             if self.returncode is None:
-> 1259                 if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
   1260                     self.returncode = _GetExitCodeProcess(self._handle)
   1261             return self.returncode

OSError: [WinError 6] The handle is invalid

Please let me know a workaround.

请告诉我一个解决方法。

采纳答案by Deepak Tekchandani

Somehow python.exewas failing and because of that internal function like VideoFileClipwas giving error in jupyter-notebook.

不知何故python.exe失败了,因为内部函数就像VideoFileClipjupyter-notebook.

Then I closed everything and restarted back and voila, error was gone and it's working fine now.

然后我关闭了所有东西并重新启动,,错误消失了,现在工作正常。

回答by Diomidis Spinellis

I solved the issue by running the following commands after reading the video.

我在阅读视频后通过运行以下命令解决了这个问题。

video_clip.reader.close()
video_clip.audio.reader.close_proc()

See https://github.com/Zulko/moviepy/issues/73and https://github.com/Zulko/moviepy/issues/164.

请参阅https://github.com/Zulko/moviepy/issues/73https://github.com/Zulko/moviepy/issues/164

回答by Si1veR

What worked for me, was closing Explorer, as gene tsai suggested. Worked instantly. I was using os.listdir(dirpath) when I got the error.

正如基因蔡所建议的那样,对我有用的是关闭资源管理器。立即工作。出现错误时,我正在使用 os.listdir(dirpath) 。

回答by DaWe

On windows 10, I did not able to call VideoFileClip() more than 5 times. clip.close()solved my issue, you need to close clips after you don't need:

在 Windows 10 上,我无法调用 VideoFileClip() 超过 5 次。clip.close()解决了我的问题,您需要在不需要后关闭剪辑:

clip = VideoFileClip("asd.mp4")
...
clip.close()