可以在遍历视频文件之前增加如下代码来统计文件大小并换算单位:

定义单位换算函数

def convert_size(size_bytes): """将文件大小从字节转换为更大的单位""" for unit in ['B', 'KB', 'MB', 'GB', 'TB']: if size_bytes < 1024.0: return f"{size_bytes:.2f} {unit}" size_bytes /= 1024.0

初始化文件大小变量

total_size = 0

遍历所有视频文件,获取视频信息并写入Excel文件

for row, video_file in enumerate(video_files, start=2): # 获取文件名和大小 file_name = os.path.basename(video_file) file_size = os.path.getsize(video_file)

# 统计文件大小
total_size += file_size

# 转换文件大小为更大的单位
file_size = convert_size(file_size)

# 使用mediainfo获取视频信息
result = subprocess.run(["mediainfo", "--Inform=Video;%Format%|%Width%x%Height%|%BitRate/String%|%FrameRate%|%Duration/String3%", video_file], stdout=subprocess.PIPE)
output = result.stdout.decode().strip()

# 解析输出结果
format, resolution, bitrate, framerate, duration = output.split("|")

# 写入Excel文件
ws.cell(row=row, column=1, value=file_name)
ws.cell(row=row, column=2, value=format)
ws.cell(row=row, column=3, value=resolution)
ws.cell(row=row, column=4, value=bitrate)
ws.cell(row=row, column=5, value=framerate)
ws.cell(row=row, column=6, value=duration)

# 写入文件大小
ws.cell(row=row, column=7, value=file_size)

统计文件夹大小并转换为更大的单位

total_size = convert_size(total_size)

写入文件夹大小

ws.cell(row=1, column=8, value="文件夹大小") ws.cell(row=2, column=8, value=total_size)

这样可以将所有视频文件的大小统计到Excel文件中,并转换为更大的单位。同时在表格最后一列也会写入文件大小。

标签: 科技


原文地址: https://gggwd.com/t/topic/94h 著作权归作者所有。请勿转载和采集!