# Unable to get vtkMP4Writer to work on Windows vtk 9.2

**URL:** https://discourse.vtk.org/t/unable-to-get-vtkmp4writer-to-work-on-windows-vtk-9-2/9282
**Category:** Support
**Tags:** python
**Created:** [September 5, 2022, 12:41pm UTC](https://discourse.vtk.org/t/unable-to-get-vtkmp4writer-to-work-on-windows-vtk-9-2/9282 "2022-09-05T12:41:52Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Flagada](https://discourse.vtk.org/user_avatar/discourse.vtk.org/flagada/32/273_2.png) [@Flagada](https://discourse.vtk.org/u/Flagada)
#### Post date: [September 5, 2022, 12:41pm UTC](https://discourse.vtk.org/t/unable-to-get-vtkmp4writer-to-work-on-windows-vtk-9-2/9282/1 "2022-09-05T12:41:52Z")

</div>

Hi all!

Following the build instructions ([https://gitlab.kitware.com/vtk/vtk/-/blob/master/Documentation/dev/build.md#build-settings](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Documentation/dev/build.md#build-settings)) I tried the “VTK\_USE\_VIDEO\_FOR\_WINDOWS\_CAPTURE" option in order to enable the use of vtkMP4Writer.  
Then I try to generate a video using the vtkMP4Writer exactly the same way I use a vtkAVIWriter but it doesn’t work. As an avi file is well generated with vtkAVIWriter the mp4 file generated by the vtkMP4Writer is desperately empty (size = 0) but no error is raised.  
Here is globally my code :  
Initialization :

```auto
MP4Writer = vtk.vtkMP4Writer()
MP4Writer.SetInputConnection(windowToImageFilter.GetOutputPort())
MP4Writer.SetFileName(MP4File)
MP4Writer.SetRate(20)
MP4Writer.Start()

```

Then for each frame :

```auto
windowToImageFilter.Modified()
MP4Writer.Write()

```

And at the end :  
`MP4Writer.End()`

My questions are : Do I miss something? Is it working for someone? Do I need ffmpeg or something else (If I understand well it uses a native windows encoder calles Sink Writer)?

Thanks in advance for your replies and your help ^^

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)
#### Post date: [September 6, 2022, 11:21pm UTC](https://discourse.vtk.org/t/unable-to-get-vtkmp4writer-to-work-on-windows-vtk-9-2/9282/2 "2022-09-06T23:21:49Z")

</div>

I wrote the `vtkMP4Writer`. It works okay, and the code you supplied looks correct. You should enable `VTK_USE_MICROSOFT_MEDIA_FOUNDATION` in the build, not `VTK_USE_VIDEO_FOR_WINDOWS_CAPTURE`. That’s the one you need to enable in the build on Windows to make this class work.

Make sure your image size in each dimension is a multiple of 2.

Does `TestMP4Writer` in your VTK build run and produce an MP4 that you can play?

---

<div class="post-metadata">

### Author: ![Flagada](https://discourse.vtk.org/user_avatar/discourse.vtk.org/flagada/32/273_2.png) [@Flagada](https://discourse.vtk.org/u/Flagada)
#### Post date: [September 7, 2022, 9:16am UTC](https://discourse.vtk.org/t/unable-to-get-vtkmp4writer-to-work-on-windows-vtk-9-2/9282/3 "2022-09-07T09:16:05Z")

</div>

Hi Cory and thank you for your reply  
My bad I well used the `VTK_USE_MICROSOFT_MEDIA_FOUNDATION` option for my tests.  
You are right the problem was that the image size was not a multiple of 2! Maybe this should be more explicit in the doc of this great function.  
Now I have to check the best way to crop my image before writting 🙂

---

<div class="post-metadata">

### Author: ![Flagada](https://discourse.vtk.org/user_avatar/discourse.vtk.org/flagada/32/273_2.png) [@Flagada](https://discourse.vtk.org/u/Flagada)
#### Post date: [September 13, 2022, 3:19pm UTC](https://discourse.vtk.org/t/unable-to-get-vtkmp4writer-to-work-on-windows-vtk-9-2/9282/4 "2022-09-13T15:19:57Z")

</div>

**Edit: finally this is my working code. I simply modify the vtkWindowToImageFilter Viewport size.**

If this can help somebody :

```auto
# Creating WindowsToImageFilter
windowToImageFilter= vtk.vtkWindowToImageFilter()
windowToImageFilter.SetInput(self.GetRenderWindow())
windowToImageFilter.ReadFrontBufferOff() # read from the back buffer
windowToImageFilter.Update()

# Modify ViewPort to ensure a multiple of 2
imSize = windowToImageFilter.GetOutput().GetDimensions()
windowToImageFilter.SetViewport(0, 0, (imSize[0]-imSize[0]%2)/imSize[0], (imSize[1]-imSize[1]%2)/imSize[1])
windowToImageFilter.Update()

# Creating Writer normally
MOVWriter = vtk.vtkMP4Writer()
MOVWriter.SetFileName(MovFile)
MOVWriter.SetRate(20)
MOVWriter.SetInputConnection(windowToImageFilter.GetOutputPort())
MOVWriter.Start()

# Call windowToImageFilter.Modified() before each MovWriter.Write()
# End with MovWriter.End()

```
