Skip to content

Commit

Permalink
Add opaque param to add_stream_from_template
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Feb 18, 2025
1 parent 4f61921 commit 4a7287a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 3 additions & 1 deletion av/container/output.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down
16 changes: 9 additions & 7 deletions av/container/output.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 4a7287a

Please # to comment.