修改headers

headers = ["文件名", "文件路径", "格式", "大小", "时长", "分辨率", "码率", "帧率", "音频码率", "音频采样率", "音频语言"]

修改遍历视频文件的代码

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

# 获取文件大小并进行单位换算
size = os.path.getsize(video_file)
if size < 1024:
    size_str = f"{size} B"
elif size < 1024*1024:
    size_str = f"{size/1024:.2f} KiB"
elif size < 1024*1024*1024:
    size_str = f"{size/1024/1024:.2f} MiB"
else:
    size_str = f"{size/1024/1024/1024:.2f} GiB"

# 使用mediainfo获取视频信息和音频信息
video_result = subprocess.run(["mediainfo", "--Inform=Video;%Format%|%Width%x%Height%|%BitRate/String%|%FrameRate%|%Duration/String3%", video_file], stdout=subprocess.PIPE)
video_output = video_result.stdout.decode().strip()
audio_result = subprocess.run(["mediainfo", "--Inform=Audio;%BitRate/String%|%SamplingRate/String%|%Language/String%", video_file], stdout=subprocess.PIPE)
audio_output = audio_result.stdout.decode().strip()

# 解析输出结果
format, resolution, bitrate, framerate, duration = video_output.split("|")
audiobitrate, audiosamplingrate, audiolanguage = audio_output.split("|")

# 将时长取整
duration = duration.split('.')[0]
h, m, s = duration.split(':')
duration = f"{h}:{m}:{math.ceil(float(s))}"

# 写入Excel文件
ws.cell(row=row, column=1, value=file_name)
ws.cell(row=row, column=2, value=video_folder) # 写入文件路径
ws.cell(row=row, column=3, value=format)
ws.cell(row=row, column=4, value=size_str) # 写入文件大小
ws.cell(row=row, column=5, value=duration)
ws.cell(row=row, column=6, value=resolution)
ws.cell(row=row, column=7, value=bitrate)
ws.cell(row=row, column=8, value=framerate)
ws.cell(row=row, column=9, value=audiobitrate) # 写入音频码率
ws.cell(row=row, column=10, value=audiosamplingrate) # 写入音频采样率
ws.cell(row=row, column=11, value=audiolanguage) # 写入音频语言

自动调整列宽

for col in ws.columns: max_length = 0 column = col[0].column_letter for cell in col: try: if len(str(cell.value)) > max_length: max_length = len(str(cell.value)) except: pass adjusted_width = (max_length + 2) ws.column_dimensions[column].width = adjusted_width

保存Excel文件

wb.save(excel_file) print(f"视频信息已保存到 {excel_file}")

请为下面这段python代码增加统计音频语言。并保证所有代码能正常运行代码如下:import osimport subprocessimport openpyxlimport mathfrom openpyxlstyles import Font PatternFill Alignment# 安装openpyxl模块# pip install openpyxl# 安装mediainfo命令行工具#

原文地址: https://www.cveoy.top/t/topic/96q 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录