Python 统计视频信息并写入 Excel 表格
以下是一个示例 Python 脚本,可以统计视频文件的信息并将其写入 Excel 表格中。需要安装 openpyxl 模块来写入 Excel。
import os
import subprocess
from openpyxl import Workbook
# 文件路径
video_dir = 'path/to/video/dir'
# 创建 Excel 工作簿
wb = Workbook()
ws = wb.active
# 添加表头
ws.append(['文件名', '时长', '分辨率', '码率'])
# 遍历视频文件
for file in os.listdir(video_dir):
if file.endswith('.mp4') or file.endswith('.avi'):
file_path = os.path.join(video_dir, file)
# 使用 ffprobe 获取视频信息
cmd = ['ffprobe', '-v', 'error', '-show_entries', 'format=duration:stream=width,height,bit_rate', '-of', 'default=noprint_wrappers=1', file_path]
output = subprocess.check_output(cmd).decode('utf-8')
info = output.strip().split('
')
duration = float(info[0].split('=')[1])
resolution = info[1].split('=')[1]
bitrate = int(info[2].split('=')[1])
# 将信息添加到 Excel 表格中
ws.append([file, duration, resolution, bitrate])
# 保存 Excel 表格
wb.save('video_info.xlsx')
使用时需要将 video_dir 变量替换为要统计视频信息的目录路径。运行脚本后,会在当前目录下生成一个名为 video_info.xlsx 的 Excel 表格,其中包含了所有视频文件的信息。
原文地址: https://www.cveoy.top/t/topic/mz0B 著作权归作者所有。请勿转载和采集!