How to Save Lossless Quality Videos Using VTK? [bgr24 rawvideo cannot be written to avi, output file will be unreadable]

I’ve been working on a project using VTK, where I need to capture animations from the render window and save them as high-quality, lossless video files. However, I’ve encountered several issues along the way and despite trying different approaches, the results have been less than satisfactory. I’m hoping to get some advice from the community. Below are the specific problems I’ve encountered:

Problem Description:

  1. Pixelation in the Video:
  • When using **vtkFFMPEGWriter** to save the video, I noticed visible pixelation in the output, especially when using higher resolutions (such as 4K). The video quality significantly degrades and appears blocky.
  1. Encoding Issues:
  • I attempted to save the video in AVI format but encountered the following error: **bgr24 rawvideo cannot be written to avi, output file will be unreadable**.
  • When I switched to MP4, I got the same error. Could this be related to incorrect encoding settings?
  1. Uncompressed Video Issues:
  • When I set **vtkFFMPEGWriter->SetCompression(false)** to save the video without compression, I received the error **bgr24 rawvideo cannot be written to avi, and the output file was unreadable**.
  • I understand that uncompressed videos result in large file sizes, but I’m aiming for clear, lossless output.

My Attempts:

  1. Compression Settings:
  • I tried setting vtkFFMPEGWriter->SetCompression(true) to enable compression, which avoided the errors, but the video still showed significant pixelation and lacked clarity.
  1. Alternative Solutions:
  • I also experimented with saving each frame as a lossless PNG image and then manually combining them into a video using FFMPEG, but this requires post-processing, which is not ideal for my use case.

What I Need:

I would like to achieve the following, strictly through code-based automation:

  • Directly capture each frame from the VTK render window and save it as a lossless video file (such as MP4 or AVI).
  • Eliminate the pixelation issue and ensure the highest possible video quality.

Environment Details:

  • VTK Version: 9.3.1
  • Operating System: Windows 10
  • Programming Language: C++
vtkSmartPointer<vtkFFMPEGWriter> writer = vtkSmartPointer<vtkFFMPEGWriter>::New();
writer->SetInputConnection(windowToImageFilter->GetOutputPort());
writer->SetFileName("output_video.avi");
writer->SetRate(30);   // Set frame rate to 30fps
writer->SetQuality(2); // Set highest quality
writer->Start();

for (int i = 0; i < 100; ++i) {
    renderWindow->Render();
    windowToImageFilter->Modified();
    windowToImageFilter->Update();
    writer->Write();
}
writer->End();

I would greatly appreciate any assistance or suggestions on how to save the video without sacrificing quality and how to resolve the encoding issues. Thank you in advance for your help!