python使用moviepy包下的VideoFileClip时报错:OSError: [WinError 6] 句柄无效解决方法

在使用moviepy包下的VideoFileClip获取视频时长,在连续获取了若干个视频时长之后,突然报错:

Traceback (most recent call last):
  File "e:\Videos\get_video_duration.py", line 67, in 
    run()
  File "e:\Videos\get_video_duration.py", line 62, in run
    gen_video_duration()
  File "e:\Videos\get_video_duration.py", line 49, in get_video_duration
    duration = time_convert(get_video_times(os.path.join(root,i)))
  File "e:\Videos\gen_video_duration.py", line 26, in get_video_times
  File "D:\ProgramData\Anaconda3_x64\lib\site-packages\moviepy\video\io\VideoFileClip.py", line 91, in __init__
    fps_source=fps_source)
  File "D:\ProgramData\Anaconda3_x64\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 69, in __init__
    self.initialize()
  File "D:\ProgramData\Anaconda3_x64\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 104, in initialize
    self.proc = sp.Popen(cmd, **popen_params)
  File "D:\ProgramData\Anaconda3_x64\lib\subprocess.py", line 664, in __init__
    _cleanup()
  File "D:\ProgramData\Anaconda3_x64\lib\subprocess.py", line 228, in _cleanup
    res = inst._internal_poll(_deadstate=sys.maxsize)
  File "D:\ProgramData\Anaconda3_x64\lib\subprocess.py", line 1216, in _internal_poll
    if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
OSError: [WinError 6] 句柄无效。

 

左侧是错误写法,右侧加了两行问题解决了:

python使用moviepy包下的VideoFileClip时报错:OSError: [WinError 6] 句柄无效解决方法_第1张图片

 

正确获取视频时长的代码:

def get_video_time(video_path):
    """
    pip install moviepy
    获取视频时长(s:秒),如果需要换算成时分秒,则调用time_convert()即可
    """
    from moviepy.editor import VideoFileClip
    video_clip = VideoFileClip(filename)
    durantion = video_clip.duration
    video_clip.reader.close()
    video_clip.audio.reader.close_proc()
    return durantion

 

解决方案来源:http://www.voidcn.com/article/p-ynpmrlvx-bwa.html

因为感觉他写得太复杂,不太好看懂,自己简单的几行代码比较容易理解,就写了这么一篇水文

你可能感兴趣的:(Python)