@@ -110,8 +110,9 @@ def export_video(
110
110
cmap = "gray" ,
111
111
vmin : float = None ,
112
112
vmax : float = None ,
113
- ffmpeg_encoder : str = None ,
114
113
progress_bar : bool = True ,
114
+ ffmpeg_encoder : str = None ,
115
+ pad_mode : str = "edge" ,
115
116
):
116
117
"""Export a video numpy array to a video file (e.g. ``.mp4``) using `ffmpeg <https://www.ffmpeg.org>`_.
117
118
@@ -145,11 +146,14 @@ def export_video(
145
146
The minimum value for the colormap, by default None
146
147
vmax : float, optional
147
148
The maximum value for the colormap, by default None
149
+ progress_bar : bool, optional
150
+ Whether to show a progress bar, by default True
148
151
ffmpeg_encoder : str, optional
149
152
The ffmpeg encoder to use, by default ``'libx264'``. See :func:`set_default_ffmpeg_encoder` and
150
153
:func:`set_ffmpeg_defaults` for more information.
151
- progress_bar : bool, optional
152
- Whether to show a progress bar, by default True
154
+ pad_mode : str, optional
155
+ Odd-sized videos need to be padded to even dimensions, otherwise ffmpeg will error. The padding mode to use,
156
+ by default "edge". See :func:`numpy.pad` for more information.
153
157
"""
154
158
if isinstance (video , (str , os .PathLike )):
155
159
filename , video = video , filename
@@ -178,6 +182,13 @@ def export_video(
178
182
179
183
if frame .dtype != np .uint8 :
180
184
frame = (frame * 255 ).astype (np .uint8 )
185
+
186
+ # pad odd-sized frames to even dimension, otherwise ffmpeg will error
187
+ if frame .shape [0 ] % 2 == 1 :
188
+ frame = np .pad (frame , ((0 , 1 ), (0 , 0 ), (0 , 0 )), mode = pad_mode )
189
+ if frame .shape [1 ] % 2 == 1 :
190
+ frame = np .pad (frame , ((0 , 0 ), (0 , 1 ), (0 , 0 )), mode = pad_mode )
191
+
181
192
writer .writeFrame (frame )
182
193
writer .close ()
183
194
print (f"video exported to { filename } " )
0 commit comments