diff --git a/av/codec/context.pxd b/av/codec/context.pxd index 3d87023d7..08d24761e 100644 --- a/av/codec/context.pxd +++ b/av/codec/context.pxd @@ -14,6 +14,10 @@ cdef class CodecContext: # Whether AVCodecContext.extradata should be de-allocated upon destruction. cdef bint extradata_set + # True when created via add_stream_from_template(); start_encoding() skips + # avcodec_open2() and lets encode()/decode() open the codec lazily if needed. + cdef readonly bint _template_initialized + # Used as a signal that this is within a stream, and also for us to access that # stream. This is set "manually" by the stream after constructing this object. cdef int stream_index diff --git a/av/container/output.py b/av/container/output.py index b36cf307f..b5bafc660 100644 --- a/av/container/output.py +++ b/av/container/output.py @@ -193,6 +193,7 @@ def add_stream_from_template( # Construct the user-land stream py_codec_context: CodecContext = wrap_codec_context(ctx, codec, None) + py_codec_context._template_initialized = True py_stream: Stream = wrap_stream(self, stream, py_codec_context) self.streams.add_stream(py_stream) @@ -368,12 +369,14 @@ def start_encoding(self): if not ctx.is_open: for k, v in self.options.items(): ctx.options.setdefault(k, v) - ctx.open() - # Track option consumption. - for k in self.options: - if k not in ctx.options: - used_options.add(k) + if not ctx._template_initialized: + ctx.open() + + # Track option consumption. + for k in self.options: + if k not in ctx.options: + used_options.add(k) stream._finalize_for_output()