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
5 changes: 5 additions & 0 deletions av/container/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ def add_stream_from_template(
if opaque is None:
opaque = template.type != "video"

if template.codec_context is None:
raise ValueError(
f"template stream of type {template.type} has no codec context"
)

codec_obj: Codec
if opaque: # Copy ctx from template.
codec_obj = template.codec_context.codec
Expand Down
21 changes: 21 additions & 0 deletions tests/test_streams.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from fractions import Fraction
from typing import Any, cast

import pytest

Expand Down Expand Up @@ -146,3 +147,23 @@ def test_data_stream(self) -> None:
assert repr.startswith("<av.DataStream #0") and repr.endswith(">")

container.close()

def test_data_stream_from_template(self) -> None:
"""Test that adding a data stream from a template raises ValueError."""

# Open an existing container with a data stream
input_container = av.open(fate_suite("mxf/track_01_v02.mxf"))
input_data_stream = input_container.streams.data[0]

# Create a new container and ensure using a data stream as a template raises ValueError
output_container = av.open("out.mkv", "w")
with pytest.raises(ValueError):
# input_data_stream is a DataStream at runtime; the test asserts that
# using it as a template raises ValueError. The static type stubs
# intentionally restrict which Stream subclasses are valid templates,
# so cast to Any here to keep the runtime check while satisfying
# the type checker.
output_container.add_stream_from_template(cast(Any, input_data_stream))

input_container.close()
output_container.close()
Loading