import cv2
# 打开视频文件
video_path = 'input.mp4'
cap = cv2.VideoCapture(video_path)
# 获取视频的帧率、宽度和高度
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# 定义裁剪区域 (x, y, w, h)
x, y, w, h = 100, 100, 400, 300 # 你可以根据需要调整这些值
# 创建视频写入对象
output_path = 'output.mp4'
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 编码器
out = cv2.VideoWriter(output_path, fourcc, fps, (w, h))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# 裁剪帧
cropped_frame = frame[y:y+h, x:x+w]
# 写入裁剪后的帧到输出视频
out.write(cropped_frame)
# 显示裁剪后的帧(可选)
cv2.imshow('Cropped Frame', cropped_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放资源
cap.release()
out.release()
cv2.destroyAllWindows()
print(f"裁剪后的视频已保存到 {output_path}")
裁剪用opencv比较方便,因为opencv可视化 比较方便 ,