Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion av/container/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ def add_stream(self, codec_name, rate=None, options: dict | None = None, **kwarg

return py_stream

def add_stream_from_template(self, template: Stream, opaque=None, **kwargs):
def add_stream_from_template(
self, template: Stream, opaque: bool | None = None, **kwargs
):
"""
Creates a new stream from a template. Supports video, audio, and subtitle streams.

Expand Down Expand Up @@ -170,6 +172,11 @@ def add_stream_from_template(self, template: Stream, opaque=None, **kwargs):
if self.ptr.oformat.flags & lib.AVFMT_GLOBALHEADER:
ctx.flags |= lib.AV_CODEC_FLAG_GLOBAL_HEADER

# Copy flags If we're creating a new codec object. This fixes some muxing issues.
# Overwriting `ctx.flags |= lib.AV_CODEC_FLAG_GLOBAL_HEADER` is intentional.
if not opaque:
ctx.flags = template.codec_context.flags

# 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, ctx))
Expand Down
33 changes: 33 additions & 0 deletions tests/test_remux.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import av
import av.datasets


def test_video_remux() -> None:
input_path = av.datasets.curated("pexels/time-lapse-video-of-night-sky-857195.mp4")
input_ = av.open(input_path)
output = av.open("remuxed.mkv", "w")

in_stream = input_.streams.video[0]
out_stream = output.add_stream_from_template(in_stream)

for packet in input_.demux(in_stream):
if packet.dts is None:
continue

packet.stream = out_stream
output.mux(packet)

input_.close()
output.close()

with av.open("remuxed.mkv") as container:
# Assert output is a valid media file
assert len(container.streams.video) == 1
assert len(container.streams.audio) == 0
assert container.streams.video[0].codec.name == "h264"

packet_count = 0
for packet in container.demux(video=0):
packet_count += 1

assert packet_count > 50
Loading