一、依赖 FFmpeg
import cv2def get_video_size(video_path):cap = cv2.VideoCapture(video_path)if not cap.isOpened():print("无法打开视频")return Nonewidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # 获取视频宽度height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # 获取视频高度cap.release()return width, heightvideo_path = "example.mp4"
print(get_video_size(video_path)) # 输出 (宽度, 高度)
二、不需要 FFmpeg
pip install imageio
import imageiodef get_video_size(video_path):reader = imageio.get_reader(video_path)meta_data = reader.get_meta_data()return meta_data['size'] # (宽度, 高度)video_path = "example.mp4"
print(get_video_size(video_path))