Cannot modify frame rate for a video using vtkFFMPEGWriter class

(Arvin) #1

I’m trying to save an animation using vtkFFMPEGWriter class and it outputs MJPG videos with a fixed frame rate of 600 frames/sec. I tried explicitly setting the frame rate by calling SetRate() method but it seems to ignore it.

Here’s the code I’m testing with

    #include <vtkActor.h>
    #include <vtkNamedColors.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkProperty.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>
    #include <vtkSmartPointer.h>
    #include <vtkSphereSource.h>
    #include <vtkWindowToImageFilter.h>
    #include <vtkRenderLargeImage.h>
    #include <vtkInteractorStyleTrackballCamera.h>
    #include <vtkCallbackCommand.h>
    #include <vtkFFMPEGWriter.h>

    static int frameCount = 0;
    vtkSmartPointer<vtkFFMPEGWriter> aviWriter;
    vtkSmartPointer<vtkRenderLargeImage> screenshotFilter;
    vtkSmartPointer<vtkActor> actor;
    static void ExecuteTimerCallback(vtkObject* caller, unsigned long eventId, void* clientData, void* callData)
    {
       vtkRenderWindowInteractor* iren = static_cast<vtkRenderWindowInteractor*>(caller);
       // Only write 200 frames
       if (frameCount < 600)
       {
          actor->RotateZ(5);
          iren->Render();
          screenshotFilter->Modified();
          screenshotFilter->Update();
          aviWriter->Write();
          frameCount++;
       }
    }
    int main()
    {
       // Animation 
       vtkSmartPointer<vtkNamedColors> colors =
          vtkSmartPointer<vtkNamedColors>::New();

       // Create a sphere
       vtkSmartPointer<vtkSphereSource> sphereSource =
          vtkSmartPointer<vtkSphereSource>::New();
       sphereSource->SetCenter(0.0, 0.0, 0.0);
       sphereSource->SetRadius(1.0);
       sphereSource->SetThetaResolution(15);
       sphereSource->SetPhiResolution(15);
       sphereSource->Update();
     
  // Create a mapper and actor
       vtkSmartPointer<vtkPolyDataMapper> mapper =
          vtkSmartPointer<vtkPolyDataMapper>::New();

       mapper->SetInputConnection(sphereSource->GetOutputPort());
       actor = vtkSmartPointer<vtkActor>::New();
       actor->SetMapper(mapper);
       actor->RotateX(90);
       actor->GetProperty()->SetRepresentationToWireframe();

       // Create a renderer, render window, and interactor
       vtkNew<vtkRenderer> ren;
       ren->AddActor(actor);
       vtkNew<vtkRenderWindow> renWin;
       renWin->SetSize(640, 480);
       renWin->AddRenderer(ren);
       vtkNew<vtkRenderWindowInteractor> iren;
       iren->SetInteractorStyle(vtkInteractorStyleTrackballCamera::New());
       iren->SetRenderWindow(renWin);
       vtkNew<vtkCallbackCommand> callback;
       callback->SetCallback(ExecuteTimerCallback);
       iren->AddObserver(vtkCommand::TimerEvent, callback);
       screenshotFilter = vtkSmartPointer<vtkRenderLargeImage>::New();
       screenshotFilter->SetInput(ren);
       screenshotFilter->SetMagnification(1.0);
       screenshotFilter->Update();
       
       // Create Writer
       ffWriter = vtkSmartPointer<vtkFFMPEGWriter>::New();
       ffWriter->SetFileName("C:\\Users\\test\\Desktop\\test-mjpg.avi");
       ffWriter->SetInputData(screenshotFilter->GetOutput());
       ffWriter->SetQuality(2);
       ffWriter->SetRate(24);
       ffWriter->Start();

       ren->ResetCamera();
       ren->SetBackground(0.6, 0.6, 0.3);
       renWin->Render();
       iren->CreateRepeatingTimer(10);
       iren->Start();
       ffWriter->End();

       return 1;
    }

Warning messages in console:


Sample Output