File size: 2,944 Bytes
f8ba0eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import cv2
import os

def rotate_video(input_path, output_path, rotation_angle=90):
    """
    旋转视频文件
    :param input_path: 输入视频路径
    :param output_path: 输出视频路径
    :param rotation_angle: 旋转角度 (90, 180, 270)
    :return: 是否成功
    """
    try:
        # 打开视频文件
        cap = cv2.VideoCapture(input_path)

        if not cap.isOpened():
            print(f"无法打开视频文件: {input_path}")
            return False

        # 获取视频属性
        width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = cap.get(cv2.CAP_PROP_FPS)
        total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

        print(f"视频信息: {width}x{height}, FPS: {fps}, 总帧数: {total_frames}")

        # 根据旋转角度调整输出视频的宽高
        if rotation_angle == 90 or rotation_angle == 270:
            output_width = height
            output_height = width
        else:
            output_width = width
            output_height = height

        # 创建视频写入器
        fourcc = cv2.VideoWriter_fourcc(*'mp4v')
        out = cv2.VideoWriter(output_path, fourcc, fps, (output_width, output_height))

        frame_count = 0
        while True:
            ret, frame = cap.read()
            if not ret:
                break

            # 旋转帧
            if rotation_angle == 90:
                rotated = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
            elif rotation_angle == 180:
                rotated = cv2.rotate(frame, cv2.ROTATE_180)
            elif rotation_angle == 270:
                rotated = cv2.rotate(frame, cv2.ROTATE_90_COUNTERCLOCKWISE)
            else:
                print("不支持的旋转角度,请使用90, 180, 或270")
                return False

            # 写入旋转后的帧
            out.write(rotated)
            frame_count += 1

            if frame_count % 100 == 0:
                print(".1f")

        # 释放资源
        cap.release()
        out.release()

        print(f"视频旋转完成! 已处理 {frame_count} 帧")
        print(f"输出文件: {output_path}")
        return True

    except Exception as e:
        print(f"处理视频时出错: {str(e)}")
        return False

if __name__ == "__main__":
    # 设置输入输出路径
    input_video = "videos/sample1_raw.mp4"
    output_video = "videos/sample1_rotated_270.mp4"

    # 检查输入文件是否存在
    if not os.path.exists(input_video):
        print(f"输入视频文件不存在: {input_video}")
        print("请确保视频文件在videos目录下")
    else:
        # 旋转视频 (默认90度顺时针)
        print("开始旋转视频...")
        success = rotate_video(input_video, output_video, 270)

        if success:
            print("视频旋转成功!")
        else:
            print("视频旋转失败!")