diff --git a/vr180_matting/vr180_processor.py b/vr180_matting/vr180_processor.py index cd6d2b9..8051996 100644 --- a/vr180_matting/vr180_processor.py +++ b/vr180_matting/vr180_processor.py @@ -158,12 +158,48 @@ class VR180Processor(VideoProcessor): try: # Write frames to temporary video height, width = eye_frames[0].shape[:2] - fourcc = cv2.VideoWriter_fourcc(*'mp4v') - writer = cv2.VideoWriter(str(temp_video_path), fourcc, 30.0, (width, height)) + + # Try different codecs if mp4v fails + codecs_to_try = [ + ('mp4v', '.mp4'), + ('XVID', '.avi'), + ('MJPG', '.avi') + ] + + writer = None + for fourcc_str, ext in codecs_to_try: + fourcc = cv2.VideoWriter_fourcc(*fourcc_str) + temp_video_path_with_ext = temp_video_path.with_suffix(ext) + writer = cv2.VideoWriter(str(temp_video_path_with_ext), fourcc, 30.0, (width, height)) + + if writer.isOpened(): + temp_video_path = temp_video_path_with_ext + print(f"Using codec {fourcc_str} for temp video") + break + else: + writer.release() + writer = None + + if writer is None: + raise RuntimeError("Failed to open video writer with any codec") for frame in eye_frames: - writer.write(frame) + success = writer.write(frame) + if not success: + raise RuntimeError(f"Failed to write frame to {temp_video_path}") + writer.release() + del writer # Ensure it's fully released + + # Verify the file was created and has content + if not temp_video_path.exists(): + raise RuntimeError(f"Temporary video file was not created: {temp_video_path}") + + file_size = temp_video_path.stat().st_size + if file_size == 0: + raise RuntimeError(f"Temporary video file is empty: {temp_video_path}") + + print(f"Created temp video {temp_video_path} ({file_size / 1024 / 1024:.1f} MB)") # Initialize SAM2 with video path self.sam2_model.init_video_state(video_path=str(temp_video_path))