From 09f32c59661ef4211b95e9d79fbe04fa1b0f480d Mon Sep 17 00:00:00 2001 From: WyattBlue Date: Tue, 18 Feb 2025 13:57:50 -0500 Subject: [PATCH] Add opaque param to add_stream_from_template --- av/container/output.pyi | 4 +++- av/container/output.pyx | 16 +++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/av/container/output.pyi b/av/container/output.pyi index a7c89452f..3169f486b 100644 --- a/av/container/output.pyi +++ b/av/container/output.pyi @@ -38,7 +38,9 @@ class OutputContainer(Container): options: dict[str, str] | None = None, **kwargs, ) -> VideoStream | AudioStream | SubtitleStream: ... - def add_stream_from_template(self, template: _StreamT, **kwargs) -> _StreamT: ... + def add_stream_from_template( + self, template: _StreamT, opaque: bool | None = None, **kwargs + ) -> _StreamT: ... def add_data_stream( self, codec_name: str | None = None, options: dict[str, str] | None = None ) -> DataStream: ... diff --git a/av/container/output.pyx b/av/container/output.pyx index c2055486e..7140c64b5 100644 --- a/av/container/output.pyx +++ b/av/container/output.pyx @@ -129,20 +129,24 @@ cdef class OutputContainer(Container): return py_stream - def add_stream_from_template(self, Stream template not None, **kwargs): + def add_stream_from_template(self, Stream template not None, opaque=None, **kwargs): """ Creates a new stream from a template. Supports video, audio, and subtitle streams. :param template: Copy codec from another :class:`~av.stream.Stream` instance. + :param opaque: If True, copy opaque data from the template's codec context. :param \\**kwargs: Set attributes for the stream. :rtype: The new :class:`~av.stream.Stream`. """ cdef const lib.AVCodec *codec cdef Codec codec_obj - if template.type != "video": + if opaque is None: + opaque = template.type != "video" + + if opaque: # Copy ctx from template. codec_obj = template.codec_context.codec - else: + else: # Construct new codec object. codec_obj = Codec(template.codec_context.codec.name, "w") codec = codec_obj.ptr @@ -164,10 +168,8 @@ cdef class OutputContainer(Container): if self.ptr.oformat.flags & lib.AVFMT_GLOBALHEADER: codec_context.flags |= lib.AV_CODEC_FLAG_GLOBAL_HEADER - # Initialise stream codec parameters to populate the codec type. - # - # Subsequent changes to the codec context will be applied just before - # encoding starts in `start_encoding()`. + # Initialize stream codec parameters to populate the codec type. Subsequent changes to + # the codec context will be applied just before encoding starts in `start_encoding()`. err_check(lib.avcodec_parameters_from_context(stream.codecpar, codec_context)) # Construct the user-land stream