统计视频时长VideoFlipClip--在VideoFileClip函數中獲取“OSError:[WinError 6]句柄無效”

在代码中添加下面2句即可
clip.reader.close()
clip.audio.reader.close_proc()

import os
import datetime
import sys
import argparse
from moviepy.editor import VideoFileClip

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description='Compute Total Time of a Series of Videos')
    parser.add_argument("--path", metavar="PATH", default=".",
                        help="the root path of the videos(default: .)")
    parser.add_argument("--type", metavar="TYPE", default=".mp4",
                        help="the type of the videos(default: .mkv)")
    args = parser.parse_args()
    filelist = []
    
    for a, b, c in os.walk(args.path):
        for name in c:
            fname = os.path.join(a, name)
            if fname.endswith(args.type):
                filelist.append(fname)
    
    ftime = 0.0
    for item in filelist:
        clip = VideoFileClip(item)
        clip.reader.close()  #
        clip.audio.reader.close_proc()  #
        #上面2句主要是解决在VideoFileClip函數中獲取“OSError:[WinError 6]句柄無效” 问题的
        ftime += clip.duration
    print("%d seconds: " % ftime,str(datetime.timedelta(seconds=ftime)))

你可能感兴趣的:(视频相关)