(input_video, output_video, config, mouse_icon_path, mouse_scale=1.0, mouse_rotation=0)
| 155 | |
| 156 | # 处理视频 |
| 157 | def process_video(input_video, output_video, config, mouse_icon_path, mouse_scale=1.0, mouse_rotation=0): |
| 158 | key_data, mouse_data = parse_config(config) |
| 159 | |
| 160 | cap = cv2.VideoCapture(input_video) |
| 161 | fps = int(cap.get(cv2.CAP_PROP_FPS)) |
| 162 | frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) |
| 163 | frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
| 164 | frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) |
| 165 | |
| 166 | mouse_icon = cv2.imread(mouse_icon_path, cv2.IMREAD_UNCHANGED) |
| 167 | |
| 168 | fourcc = cv2.VideoWriter_fourcc(*'mp4v') |
| 169 | # fourcc = cv2.VideoWriter_fourcc(*'H264') |
| 170 | out = cv2.VideoWriter(output_video, fourcc, fps, (frame_width, frame_height)) |
| 171 | |
| 172 | frame_idx = 0 |
| 173 | while cap.isOpened(): |
| 174 | ret, frame = cap.read() |
| 175 | if not ret: |
| 176 | break |
| 177 | |
| 178 | keys = key_data.get(frame_idx, {"W": False, "A": False, "S": False, "D": False, "Sp": False, "Sh": False, "Ct": False}) |
| 179 | mouse_position = mouse_data.get(frame_idx, (frame_width // 2, frame_height // 2)) |
| 180 | |
| 181 | draw_keys_on_frame(frame, keys, key_size=(50, 50), spacing=10, bottom_margin=20) |
| 182 | overlay_icon(frame, mouse_icon, mouse_position, scale=mouse_scale, rotation=mouse_rotation) |
| 183 | |
| 184 | out.write(frame) |
| 185 | frame_idx += 1 |
| 186 | print(f"Processing frame {frame_idx}/{frame_count}", end="\r") |
| 187 | |
| 188 | cap.release() |
| 189 | out.release() |
| 190 | print("\nProcessing complete!") |
| 191 | |
| 192 | # 使用示例 |
| 193 | mouse_icon_path = "./mouse.png" |
no test coverage detected