Skip to content

Commit 6982d81

Browse files
HanzCEOjlaine
andcommitted
[streams] populate added stream with codec param
avformat_new_stream(AVFormatContext *s, const AVCodec *c) does not use its second parameter [1], so the codec type for the stream is not populated until avcodec_parameters_from_context() [2] is called. [1]: https://ffmpeg.org/doxygen/trunk/group__lavf__core.html#gadcb0fd3e507d9b58fe78f61f8ad39827 [2]: https://ffmpeg.org/doxygen/trunk/codec__par_8c_source.html#l00099 Co-authored-by: Jeremy Lainé <[email protected]>
1 parent 1ab3bf6 commit 6982d81

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

av/container/output.pyx

+6
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ cdef class OutputContainer(Container):
116116
if self.ptr.oformat.flags & lib.AVFMT_GLOBALHEADER:
117117
codec_context.flags |= lib.AV_CODEC_FLAG_GLOBAL_HEADER
118118

119+
# Initialise stream codec parameters to populate the codec type.
120+
#
121+
# Subsequent changes to the codec context will be applied just before
122+
# encoding starts in `start_encoding()`.
123+
err_check(lib.avcodec_parameters_from_context(stream.codecpar, codec_context))
124+
119125
# Construct the user-land stream
120126
cdef CodecContext py_codec_context = wrap_codec_context(codec_context, codec)
121127
cdef Stream py_stream = wrap_stream(self, stream, py_codec_context)

tests/test_encode.py

+7
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class TestBasicVideoEncoding(TestCase):
126126
def test_default_options(self):
127127
with av.open(self.sandboxed("output.mov"), "w") as output:
128128
stream = output.add_stream("mpeg4")
129+
self.assertIn(stream, output.streams.video)
129130
self.assertEqual(stream.average_rate, Fraction(24, 1))
130131
self.assertEqual(stream.time_base, None)
131132

@@ -152,6 +153,7 @@ def test_encoding_with_pts(self):
152153

153154
with av.open(path, "w") as output:
154155
stream = output.add_stream("libx264", 24)
156+
self.assertIn(stream, output.streams.video)
155157
stream.width = WIDTH
156158
stream.height = HEIGHT
157159
stream.pix_fmt = "yuv420p"
@@ -182,6 +184,7 @@ class TestBasicAudioEncoding(TestCase):
182184
def test_default_options(self):
183185
with av.open(self.sandboxed("output.mov"), "w") as output:
184186
stream = output.add_stream("mp2")
187+
self.assertIn(stream, output.streams.audio)
185188
self.assertEqual(stream.time_base, None)
186189

187190
# codec context properties
@@ -203,6 +206,7 @@ def test_transcode(self):
203206
sample_fmt = "s16"
204207

205208
stream = output.add_stream("mp2", sample_rate)
209+
self.assertIn(stream, output.streams.audio)
206210

207211
ctx = stream.codec_context
208212
ctx.time_base = sample_rate
@@ -241,11 +245,13 @@ class TestEncodeStreamSemantics(TestCase):
241245
def test_stream_index(self):
242246
with av.open(self.sandboxed("output.mov"), "w") as output:
243247
vstream = output.add_stream("mpeg4", 24)
248+
self.assertIn(vstream, output.streams.video)
244249
vstream.pix_fmt = "yuv420p"
245250
vstream.width = 320
246251
vstream.height = 240
247252

248253
astream = output.add_stream("mp2", 48000)
254+
self.assertIn(astream, output.streams.audio)
249255
astream.channels = 2
250256
astream.format = "s16"
251257

@@ -277,6 +283,7 @@ def test_stream_index(self):
277283
def test_set_id_and_time_base(self):
278284
with av.open(self.sandboxed("output.mov"), "w") as output:
279285
stream = output.add_stream("mp2")
286+
self.assertIn(stream, output.streams.audio)
280287

281288
# set id
282289
self.assertEqual(stream.id, 0)

0 commit comments

Comments
 (0)