跳至主要內容
使用cv统计视频时长
import cv2,os
# 统计视频
def count_video_duration(file_path):
    cap = cv2.VideoCapture(file_path)
    if cap.isOpened():  # 当成功打开视频时cap.isOpened()返回True,否则返回False
        # get方法参数按顺序对应下表(从0开始编号)
        rate = cap.get(5)  # 帧速率
        FrameNumber = cap.get(7)  # 视频文件的帧数
        duration = FrameNumber / rate
        return int(duration)
    else:
        return 0

# 递归获取视频路径进行统计时长
def count_video_duration_recursion(file_path):
    global duration_video
    if os.path.isdir(file_path):
        for file in os.listdir(file_path):
            count_video_duration_recursion(os.path.join(file_path, file))
    else:
        if file_path.endswith('.mp4') or file_path.endswith('.avi')or file_path.endswith('.3gp'):
            duration = count_video_duration(file_path)
            duration_video+=duration
            print(file_path, duration)
duration_video = 0
path = "路径"
count_video_duration_recursion(path)
print(duration_video)


爱喝水的木子...小于 1 分钟pythonpythoncv文件视频
筛选文件大小是0Kb的
import os
import shutil
from multiprocessing.pool import ThreadPool
import json

# 移动文件夹
def move_data(src, dst):
    shutil.move(src, dst)


def get_size(file):
    # 获取文件大小:KB
    size = os.path.getsize(file)
    return size / 1024


# 读取文件
def read_dir_data(in_file, out_file):
    th = ThreadPool(8)
    for i in os.listdir(in_file):
        t = os.path.join(in_file, i)

        # 判断json内容是否为空
        with open(t,"r",encoding="utf-8")as f:
            data=json.loads(f.read())
        if len(data["shapes"])==0:
            dst = os.path.join(out_file, i)
            print("move:{}>>>{}".format(t, dst))
            th.apply_async(move_data, args=(t, dst))

    th.close()
    th.join()
if __name__ == '__main__':
    # 筛选文件夹
    in_file=r"C:\Users\DM\Desktop\新建文件夹 (2)\1"
    # 筛选结果文件夹
    out_file=r"C:\Users\DM\Desktop\新建文件夹 (2)\2"
    read_dir_data(in_file,out_file)


爱喝水的木子...小于 1 分钟pythonpython筛选文件
统计一个文件夹中所有音频的时间(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音频文件
针对一个文件夹里边有成千上万的文件进行分类
import os, shutil

path = r"文件夹路径"  # 文件夹路径
data = os.listdir(path)
count = 1
p2 = os.path.join(path,"_{}".format(count))
temp = 0
for i in data:
	p1 = os.path.join(path,i)
    temp += 1
    if not os.path.exists(p2):
        os.mkdir(p2)
        shutil.move(p1, p2)
    else:
        shutil.move(p1, p2)
    print("move",p1,">>>",p2)
    if temp % 5 == 0:  # 将5改成需要多少个做成一个文件夹
        count += 1
        p2 = os.path.join(path,"_{}".format(count))

爱喝水的木子...小于 1 分钟pythonpython文件分类