ConstStar
发布于 2023-01-29 / 100 阅读 / 0 评论 / 0 点赞

小工具:Python统计文件夹内全部视频总时长

效果

03.组成原理
    ├── 01.第一章-计算机系统概述 2:1:21
    │    └── 00.课件
    ├── 02.第二章-数据的表示和运算 6:57:15
    │    └── 00.课件
    ├── 03.第三章-存储系统 4:57:0
    │    └── 00.课件
    ├── 04.第四章-指令系统 3:45:54
    │    └── 00.课件
    ├── 05.第五章-中央处理器 6:27:46
    │    └── 00.课件
    ├── 06.第六章-总线 2:31:29
    │    └── 00.课件
    └── 07.第七章-输入输出系统 3:33:42
         └── 00.课件


共计算79个视频文件
总时长为30:14:31
忽略了后缀名:{'.jpg', '.pdf'}

依赖

pip install moviepy

代码

import os
from moviepy.editor import VideoFileClip


class VideoTime:
    path = ''
    files = {}
    file_count = 0
    file_time_count = 0
    ignoran_list = set()

    def __init__(self, path):
        self.path = path

    def dirAll(self, pathname):
        name = os.path.basename(pathname)
        files = {'name': name, 'dir': [], 'file': []}
        if os.path.exists(pathname):
            filelist = os.listdir(pathname)
            for f in filelist:
                f = os.path.join(pathname, f)
                if os.path.isdir(f):
                    files['dir'].append(self.dirAll(f))
                else:
                    dirname = os.path.dirname(f)
                    baseName = os.path.basename(f)
                    # files['file'].append(baseName)

                    if dirname.endswith(os.sep):
                        files['file'].append(dirname+baseName)
                    else:
                        files['file'].append(dirname+os.sep+baseName)

        return files

    def get_time_count(self, files):
        tiem_count = 0
        for f in files['file']:
	# 过滤文件格式,需要其他格式可以再这里添加 
            if f.endswith('.mp4') or f.endswith('.flv'):
                clip = VideoFileClip(f)
                file_time = clip.duration / 60
                tiem_count += file_time
                clip.close()
                self.file_count += 1
            else:
                self.ignoran_list.add(os.path.splitext(f)[-1])

        return tiem_count

    def time_work(self, files, layer=0, prefix=""):

        tiem_count = self.get_time_count(files)
        self.file_time_count += tiem_count

        if tiem_count == 0:
            print(files['name'])
        else:
            print(files['name']+R" " +
                  str(int(tiem_count/60))+":" + str(int(tiem_count) % 60)+":"+str(int(tiem_count*60) % 60))

        l = len(files['dir'])
        i = 0
        while i < l:
            item = files['dir'][i]

            next_prefix = ""
            prefix_item = ""
            if i+1 < l:
                next_prefix = prefix+"    │"
                prefix_item = "    ├── "
            else:
                next_prefix = prefix+"     "
                prefix_item = "    └── "

            print(prefix+prefix_item, end='')
            self.time_work(item, layer+1, next_prefix)

            i += 1

    def work(self):
        self.files = self.dirAll(self.path)
        self.time_work(self.files)
        print("\n")
        print(f"共计算{self.file_count}个视频文件")
        print("总时长为" +
              str(int(self.file_time_count/60))+":" + str(int(self.file_time_count) % 60)+":"+str(int(self.file_time_count*60) % 60))
        if len(self.ignoran_list) != 0:
            print("忽略了后缀名:"+str(self.ignoran_list))


obj = VideoTime(R"D:\用户文档\桌面\03.组成原理")
obj.work()

评论