opencv顺时针,逆时针旋转视频并保存视频

原视频

opencv顺时针,逆时针旋转视频并保存视频_第1张图片

代码

import cv2

# 打开视频文件
video = cv2.VideoCapture('inference/video/lianzhang.mp4')

# 获取原视频的宽度和高度
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))

# 创建视频编写器并设置输出视频参数
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
output = cv2.VideoWriter('inference/video/output.mp4', fourcc, 30.0, (height, width))

while video.isOpened():
    ret, frame = video.read()

    if not ret:
        break

    # 对每一帧图像进行逆时针旋转90度,正时针是cv2.ROTATE_90_CLOCKWISE
    rotated_frame = cv2.rotate(frame, cv2.ROTATE_90_COUNTERCLOCKWISE)

    # 写入旋转后的帧到输出视频文件
    output.write(rotated_frame)

    cv2.imshow('Rotated Video', rotated_frame)

    # 按下 'q' 键退出循环
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

# 释放资源
video.release()
output.release()
cv2.destroyAllWindows()

旋转后

opencv顺时针,逆时针旋转视频并保存视频_第2张图片

你可能感兴趣的:(opencv,音视频,计算机视觉)