Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test subtitle muxing #1646

Merged
merged 2 commits into from
Nov 20, 2024
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
20 changes: 7 additions & 13 deletions av/container/output.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,15 @@ cdef class OutputContainer(Container):
:param template: Copy codec from another :class:`~av.stream.Stream` instance.
:param \\**kwargs: Set attributes for the stream.
:rtype: The new :class:`~av.stream.Stream`.

"""
cdef const lib.AVCodec *codec
cdef Codec codec_obj

if not template.codec_context:
raise ValueError("template has no codec context")

cdef Codec codec_obj = Codec(template.codec_context.codec.name, "w")
cdef const lib.AVCodec *codec = codec_obj.ptr
if template.type == "subtitle":
codec_obj = template.codec_context.codec
else:
codec_obj = Codec(template.codec_context.codec.name, "w")
codec = codec_obj.ptr

# Assert that this format supports the requested codec.
if not lib.avformat_query_codec(self.ptr.oformat, codec.id, lib.FF_COMPLIANCE_NORMAL):
Expand Down Expand Up @@ -182,13 +183,6 @@ cdef class OutputContainer(Container):
cdef Stream py_stream = wrap_stream(self, stream, py_codec_context)
self.streams.add_stream(py_stream)

if template.type == "video":
py_stream.time_base = kwargs.pop("time_base", 1 / template.average_rate)
elif template.type == "audio":
py_stream.time_base = kwargs.pop("time_base", 1 / template.rate)
else:
py_stream.time_base = kwargs.pop("time_base", Fraction(0, 1))

for k, v in kwargs.items():
setattr(py_stream, k, v)

Expand Down
23 changes: 23 additions & 0 deletions tests/test_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,29 @@ def test_transcode(self) -> None:
assert stream.sample_rate == sample_rate


class TestSubtitleEncoding:
def test_subtitle_muxing(self) -> None:
input_ = av.open(fate_suite("sub/MovText_capability_tester.mp4"))
in_stream = input_.streams.subtitles[0]

output_bytes = io.BytesIO()
output = av.open(output_bytes, "w", format="mp4")

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)

output.close()
output_bytes.seek(0)
assert output_bytes.getvalue().startswith(
b"\x00\x00\x00\x1cftypisom\x00\x00\x02\x00isomiso2mp41\x00\x00\x00\x08free"
)


class TestEncodeStreamSemantics(TestCase):
def test_stream_index(self) -> None:
with av.open(self.sandboxed("output.mov"), "w") as output:
Expand Down
7 changes: 0 additions & 7 deletions tests/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,3 @@ def test_data_stream(self) -> None:
assert repr.startswith("<av.DataStream #0") and repr.endswith(">")

container.close()

# def test_side_data(self) -> None:
# container = av.open(fate_suite("mov/displaymatrix.mov"))
# video = container.streams.video[0]

# assert video.nb_side_data == 1
# assert video.side_data["DISPLAYMATRIX"] == -90.0
Loading