跳至主要內容
统计一个文件夹中所有音频的时间(python)
import os

try:
    from pydub.utils import mediainfo
except:
    os.system("pip3 install pydub")
    from pydub.utils import mediainfo


def count_time(file_path):
    song = mediainfo(file_path)
    return song['duration']


# 递归统计文件夹下所有文件数量
def count_file_time(path):
    global all_time
    file_list = os.listdir(path)
    for file in file_list:
        file_path = os.path.join(path, file)
        if os.path.isdir(file_path):
            count_file_time(file_path)
        else:
            if file_path.split(".")[-1] in file_type:
                print(file_path, count_time(file_path))
                all_time += float(count_time(file_path))


# 放文件路径
path = r""
file_type = ["wav", "mp3"]
all_time = 0
count_file_time(path)
print("共:", all_time, "秒")


爱喝水的木子...小于 1 分钟pythonpythonpydub音频文件