Skip to content

Fix save_video not creating video files - fixes issue 470#481

Merged
giswqs merged 2 commits intomainfrom
fix-issue-470-save-video
Jan 25, 2026
Merged

Fix save_video not creating video files - fixes issue 470#481
giswqs merged 2 commits intomainfrom
fix-issue-470-save-video

Conversation

@giswqs
Copy link
Member

@giswqs giswqs commented Jan 25, 2026

Summary

Fixes the issue where save_video displays success messages but doesn't actually create video files on disk.

Problem

The user reported that save_video outputs success messages like Video saved as out.mp4 and Saved video to out.mp4, but no files are created. This is a silent failure issue where cv2.VideoWriter.release does not always properly flush to disk, especially on Windows.

Solution

  • Try multiple codecs: Attempts avc1, mp4v, and XVID for cross-platform compatibility
  • Verify writer initialization: Checks that VideoWriter successfully opened before writing
  • File existence validation: Verifies the video file exists after release
  • File size validation: Ensures the file has content and isn't empty/corrupt
  • Clear error messages: Provides specific feedback when video creation fails

Changes Made

Modified images_to_video in samgeo/common.py to:

  1. Try multiple codec options instead of just avc1
  2. Verify the VideoWriter is properly initialized
  3. Add post-save validation to catch silent failures
  4. Raise clear RuntimeError with helpful messages when saving fails

Testing

Closes

Fixes #470

Fixes #470

The issue was that cv2.VideoWriter.release() doesn't always properly
flush to disk, especially on Windows, and there was no verification that
the file was created successfully.

Changes:
- Try multiple codecs (avc1, mp4v, XVID) for better cross-platform compatibility
- Verify VideoWriter is successfully opened before writing frames
- Add file existence check after release to ensure video was saved
- Add file size check to catch empty/corrupt videos
- Provide clear error messages when video creation fails

This ensures users get immediate feedback if video saving fails rather than
silent failure with misleading success messages.
Copilot AI review requested due to automatic review settings January 25, 2026 15:55
@giswqs giswqs merged commit d1f9fb9 into main Jan 25, 2026
9 checks passed
@giswqs giswqs deleted the fix-issue-470-save-video branch January 25, 2026 15:59
@github-actions
Copy link

github-actions bot commented Jan 25, 2026

@github-actions github-actions bot temporarily deployed to pull request January 25, 2026 15:59 Inactive
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes the silent failure where save_video() reports success but no video file is written to disk by hardening images_to_video().

Changes:

  • Add multi-codec fallback (avc1, mp4v, XVID) when initializing cv2.VideoWriter.
  • Validate VideoWriter.isOpened() before writing frames and raise a clear error when initialization fails.
  • Add post-save validation (file existence + non-zero size) to detect “successful” writes that produced no output.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +3548 to +3552
if not video_writer or not video_writer.isOpened():
raise RuntimeError(
f"Failed to initialize video writer for {output_video}. "
"Please ensure you have the necessary codecs installed."
)
Copy link

Copilot AI Jan 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RuntimeError on VideoWriter initialization failure currently attributes the problem to missing codecs, but VideoWriter.isOpened() can also fail when the output directory doesn’t exist or the path is not writable. Consider checking/creating the parent directory (or explicitly erroring on it) and including the list of attempted codecs + path in the error message so users can diagnose the real cause.

Copilot uses AI. Check for mistakes.
Comment on lines +3567 to +3579
# Verify the video file was created
if not os.path.exists(output_video):
raise RuntimeError(
f"Failed to create video file {output_video}. "
"The video writer completed but the file was not saved to disk."
)

# Verify the file has content
if os.path.getsize(output_video) == 0:
raise RuntimeError(
f"Video file {output_video} was created but is empty. "
"This may indicate a codec problem."
)
Copy link

Copilot AI Jan 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new codec-fallback and post-save validations are a regression-prone bugfix but there’s no unit test coverage for images_to_video() in tests/test_common.py. Adding a small test that writes a few synthetic frames to a temp dir and asserts the output file exists and is non-empty (and ideally can be opened via cv2.VideoCapture) would help prevent reintroducing the silent failure from #470.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

samgeo.SamGeo3Video().save_video() doesn't save the video to disk

2 participants