Skip to content

Commit ee5c40e

Browse files
authored
Allow extradata to be set for encoders
1 parent 8809032 commit ee5c40e

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

av/codec/context.pyx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,6 @@ cdef class CodecContext:
189189

190190
@extradata.setter
191191
def extradata(self, data):
192-
if not self.is_decoder:
193-
raise ValueError("Can only set extradata for decoders.")
194-
195192
if data is None:
196193
lib.av_freep(&self.ptr.extradata)
197194
self.ptr.extradata_size = 0

av/container/output.pyx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,14 @@ cdef class OutputContainer(Container):
9393
# Now lets set some more sane video defaults
9494
elif codec.type == lib.AVMEDIA_TYPE_VIDEO:
9595
codec_context.pix_fmt = lib.AV_PIX_FMT_YUV420P
96-
codec_context.width = 640
97-
codec_context.height = 480
98-
codec_context.bit_rate = 1024000
99-
codec_context.bit_rate_tolerance = 128000
96+
codec_context.width = kwargs.pop("width", 640)
97+
codec_context.height = kwargs.pop("height", 480)
98+
codec_context.bit_rate = kwargs.pop("bit_rate", 1024000)
99+
codec_context.bit_rate_tolerance = kwargs.pop("bit_rate_tolerance", 128000)
100+
try:
101+
to_avrational(kwargs.pop("time_base"), &codec_context.time_base)
102+
except KeyError:
103+
pass
100104
to_avrational(rate or 24, &codec_context.framerate)
101105

102106
stream.avg_frame_rate = codec_context.framerate
@@ -105,9 +109,14 @@ cdef class OutputContainer(Container):
105109
# Some sane audio defaults
106110
elif codec.type == lib.AVMEDIA_TYPE_AUDIO:
107111
codec_context.sample_fmt = codec.sample_fmts[0]
108-
codec_context.bit_rate = 128000
109-
codec_context.bit_rate_tolerance = 32000
112+
codec_context.bit_rate = kwargs.pop("bit_rate", 128000)
113+
codec_context.bit_rate_tolerance = kwargs.pop("bit_rate_tolerance", 32000)
114+
try:
115+
to_avrational(kwargs.pop("time_base"), &codec_context.time_base)
116+
except KeyError:
117+
pass
110118
codec_context.sample_rate = rate or 48000
119+
stream.time_base = codec_context.time_base
111120
lib.av_channel_layout_default(&codec_context.ch_layout, 2)
112121

113122
# Some formats want stream headers to be separate

tests/test_codec_context.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ def test_encoder_extradata(self):
105105
self.assertEqual(ctx.extradata, None)
106106
self.assertEqual(ctx.extradata_size, 0)
107107

108-
with self.assertRaises(ValueError) as cm:
109-
ctx.extradata = b"123"
110-
self.assertEqual(str(cm.exception), "Can only set extradata for decoders.")
108+
ctx.extradata = b"123"
109+
self.assertEqual(ctx.extradata, b"123")
110+
self.assertEqual(ctx.extradata_size, 3)
111111

112112
def test_encoder_pix_fmt(self):
113113
ctx = av.codec.Codec("h264", "w").create()

0 commit comments

Comments
 (0)