Skip to content

Commit c566965

Browse files
committed
fix VideoWriter kwargs
1 parent e9eefae commit c566965

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/daio/video.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,14 @@ class VideoWriter:
174174

175175
def __init__(self, filename, codec='libx264', fps=25, frame_shape=None, pix_fmt='yuv420p', **kwargs):
176176
self.container = av.open(filename, mode='w')
177+
self.audio_stream = None
177178
#define codec defaults:
178179
if codec == 'h264':
179-
kwargs = dict(bit_rate=1000000, pix_fmt='yuv420p').update(kwargs)
180+
#kwargs = dict(bit_rate=1000000, pix_fmt='yuv420p').update(kwargs)
181+
kwargs = {**dict(bit_rate=1000000), **kwargs}
180182
elif codec == 'libx264':
181-
kwargs = dict(crf=23, preset='superfast', pix_fmt='yuv420p').update(kwargs)
183+
#kwargs = dict(crf=23, preset='superfast', pix_fmt='yuv420p').update(kwargs)
184+
kwargs = {**dict(crf=23, preset='superfast'), **kwargs}
182185
#add video stream, if frame_shape is provided (otherwise create when the first frame is written)
183186
if frame_shape is not None:
184187
self.stream = self.container.add_stream(codec, rate=fps, height=frame_shape[0], width=frame_shape[1], pix_fmt=pix_fmt, **kwargs)
@@ -189,11 +192,12 @@ def __init__(self, filename, codec='libx264', fps=25, frame_shape=None, pix_fmt=
189192
self.pix_fmt = pix_fmt
190193
self.kwargs = kwargs if kwargs is not None else {}
191194

192-
def write(self, im):
195+
def write(self, im, pts=None):
193196
""" Write a frame to the video file
194197
195198
Args:
196199
im (np.ndarray): frame to write
200+
pts (int): presentation timestamp of the frame. Default is None (don't use unles you know what you are doing)
197201
"""
198202
if im.ndim == 2:
199203
format = 'gray'
@@ -204,6 +208,8 @@ def write(self, im):
204208
if self.stream is None:
205209
self.stream = self.container.add_stream(self.codec, rate=self.fps, height=im.shape[0], width=im.shape[1], pix_fmt=self.pix_fmt, **self.kwargs)
206210
out_frame = av.VideoFrame.from_ndarray(im, format=format)
211+
if pts is not None:
212+
out_frame.pts = pts
207213
for packet in self.stream.encode(out_frame):
208214
self.container.mux(packet)
209215

0 commit comments

Comments
 (0)