我需要一个python3编写的统计视频信息并写入excel的脚本
以下是一个可能的脚本,它使用FFmpeg和openpyxl库来获取视频信息并将其写入Excel文件中。请注意,此脚本仅适用于Windows操作系统。
import os
import openpyxl
from openpyxl.utils import get_column_letter
from openpyxl.styles import Font
import subprocess
# 设置FFmpeg路径
FFMPEG_PATH = r"C:\ffmpeg\bin\ffmpeg.exe"
# 设置要处理的视频文件夹路径和Excel文件路径
VIDEO_FOLDER = r"C:\videos"
EXCEL_FILE = r"C:\video_info.xlsx"
# 定义要获取的视频信息
VIDEO_INFO = {
"Filename": "%filename%",
"Duration": "%duration%",
"Codec": "%codec_name%",
"Bitrate": "%bit_rate%",
"Resolution": "%width%x%height%",
"Frame Rate": "%avg_frame_rate%",
"Audio Codec": "%audio_codec_name%",
"Audio Bitrate": "%audio_bit_rate%",
"Audio Channels": "%audio_channels%"
}
# 获取视频信息的命令
FFMPEG_CMD = [FFMPEG_PATH, "-i", "", "-hide_banner"]
# 创建Excel文件并写入表头
wb = openpyxl.Workbook()
ws = wb.active
for col_num, header in enumerate(VIDEO_INFO.keys(), 1):
col_letter = get_column_letter(col_num)
cell = ws["{}1".format(col_letter)]
cell.value = header
cell.font = Font(bold=True)
# 遍历视频文件夹并处理每个视频文件
for root, dirs, files in os.walk(VIDEO_FOLDER):
for filename in files:
if filename.endswith(".mp4") or filename.endswith(".avi") or filename.endswith(".mov"):
# 构建获取视频信息的命令并执行
filepath = os.path.join(root, filename)
FFMPEG_CMD[2] = filepath
result = subprocess.run(FFMPEG_CMD, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
output = result.stderr
# 解析输出,获取视频信息
video_info = {}
for key, value in VIDEO_INFO.items():
start_index = output.find(value)
end_index = output.find("\n", start_index)
video_info[key] = output[start_index:end_index].replace(value, "").strip()
# 将视频信息写入Excel文件
row_num = ws.max_row + 1
for col_num, info in enumerate(video_info.values(), 1):
col_letter = get_column_letter(col_num)
cell = ws["{}{}".format(col_letter, row_num)]
cell.value = info
# 保存Excel文件
wb.save(EXCEL_FILE)
print("视频信息已成功写入Excel文件。")
使用此脚本时,请确保已安装FFmpeg和openpyxl库,并将FFmpeg路径和要处理的视频文件夹路径和Excel文件路径替换为您自己的路径。如果要获取其他视频信息,请在VIDEO_INFO字典中添加相应的键值对,并在FFMPEG_CMD命令中添加相应的参数。此外,请注意,此脚本仅支持处理MP4,AVI和MOV格式的视频文件。
原文地址: https://www.cveoy.top/t/topic/90t 著作权归作者所有。请勿转载和采集!