【问题标题】:Getting "OSError: [WinError 6] The handle is invalid" in VideoFileClip function在 VideoFileClip 函数中获取“OSError:[WinError 6] 句柄无效”
【发布时间】:2017-05-14 16:51:38
【问题描述】:

我正在使用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

请告诉我一个解决方法。

【问题讨论】:

  • 没有额外的上下文很难说。当Popen 被调用时,它会通过_cleanup 函数进行一些内部清理,该函数检查是否有任何已删除的Popen 实例仍在运行并尝试等待它们并获取退出状态。这实际上只在 Unix 系统上需要以避免僵尸。在 Windows 上,让Popen 实例被收集就足够了,然后它又为调用CloseHandle 的进程句柄收集Handle 实例,仅此而已。如果句柄无效,这很奇怪,不应该发生,但我们可以在 Windows 上忽略它。
  • 作为一种解决方法,您可以通过猴子补丁 subprocess._cleanup() 忽略该错误。或者您可以深入调查以找出关闭已删除Popen 实例的_handle 的原因。这将需要使用本机调试器。
  • @eryksun:感谢您的建议。我是新手python程序员,一定会尝试你的提议。既然使用这个功能是必须的,能不能帮我成功执行一下?
  • 我不知道这是否有帮助,但我收到此错误是因为我打开了一个 Windows 资源管理器窗口,并且可能系统正在生成缩略图或其他东西。一旦我关闭窗口一切正常

标签: python jupyter-notebook moviepy


【解决方案1】:

我在看完视频后运行以下命令解决了这个问题。

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

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

【讨论】:

  • 我只需要使用 video_clip.close() 就可以了。谢谢!
  • 代码 video_clip.reader.close() 对我有用。 video_clip.audio.reader.close_proc() 抛出错误。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    不知何故,python.exe 失败了,因为像VideoFileClip 这样的内部函数在jupyter-notebook 中给出了错误。

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

    【讨论】:

    • 我假设您会尝试重新启动,并且每次都会可靠地导致错误的进程句柄,因为_cleanup 会反复失败,所以永远不会被删除。
    • 您可能确实在软件堆栈中的某处存在错误(不一定在您自己的代码中),导致某些例程关闭进程句柄。它不需要有任何事情要做子进程。例如,某些代码可能有一个文件句柄,该句柄从它下面被关闭了。然后句柄号被重新用作进程句柄,而原来的文件句柄所有者又在不知不觉中关闭了,以为它正在关闭文件。如果没有调试器,可能很难诊断出这样的问题。
    • @eryksun:实际上出现了一个对话框并要求进行调试,并且确实失败了很多次。我今天多次看到相同的行为,每次我不得不重新启动 jupyter-notebook。那么您认为我应该将此标记为答案,还是您想提供一个有见地的答案?
    • 我无法给出更深入的答案。这种问题与 Stack Overflow 不匹配。我唯一的通用答案是monkeypatch subprocess._cleanup,我不是特别喜欢这个选项。
    • @DeepakTekchandani 感谢您发布答案。我浪费了很多时间。重启!
    【解决方案4】:

    对我有用的是关闭 Explorer,正如gene tsai 所建议的那样。立即工作。当我收到错误时,我正在使用 os.listdir(dirpath)。

    【讨论】:

      猜你喜欢
      • 2022-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 2017-02-27
      • 1970-01-01
      • 2018-05-12
      相关资源
      最近更新 更多