import cv2
# 指定视频文件路径
video_path = r'D:\desk\test_django\test\Videos\RGB\Videos\yuan2jing_1.mp4'
# 指定输出视频文件路径
output_path = r'D:\desk\test_django\test\Videos\RGB\Videos\yuan2jing_1_1.mp4'
# 指定新的分辨率
new_width = 1280 # 新宽度
new_height = 1024# 新高度
# 使用cv2.VideoCapture()打开视频文件
cap = cv2.VideoCapture(video_path)
# 获取视频的帧率
fps = cap.get(cv2.CAP_PROP_FPS)
# 获取视频的总帧数
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# 计算新的帧率
new_fps = round(fps * (new_width / new_height))
# 创建一个用于保存调整分辨率后视频的VideoWriter对象
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 视频编码器
out = cv2.VideoWriter(output_path, fourcc, new_fps, (new_width, new_height))
# 读取视频的每一帧并调整分辨率
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# 调整帧的分辨率
resized_frame = cv2.resize(frame, (new_width, new_height))
# 写入调整后的帧到输出视频
out.write(resized_frame)
# 释放VideoCapture和VideoWriter对象
cap.release()
out.release()
print("视频分辨率调整完成,已保存到:", output_path)
标签:视频,修改,分辨率,cap,cv2,OpenCV,path,new
From: https://blog.csdn.net/weixin_49824703/article/details/140124783