From 74f6aec8e9d639785976c7e6108349780673d3d1 Mon Sep 17 00:00:00 2001 From: WyattBlue Date: Tue, 19 Nov 2024 20:37:10 -0500 Subject: [PATCH] Test subtitle muxing --- av/container/output.pyx | 20 +++++++------------- tests/test_encode.py | 23 +++++++++++++++++++++++ tests/test_streams.py | 7 ------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/av/container/output.pyx b/av/container/output.pyx index 1227c4340..8fad652d4 100644 --- a/av/container/output.pyx +++ b/av/container/output.pyx @@ -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): @@ -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) diff --git a/tests/test_encode.py b/tests/test_encode.py index 0848e9b1a..c107aa044 100644 --- a/tests/test_encode.py +++ b/tests/test_encode.py @@ -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: diff --git a/tests/test_streams.py b/tests/test_streams.py index ad8be2a62..b7699e622 100644 --- a/tests/test_streams.py +++ b/tests/test_streams.py @@ -135,10 +135,3 @@ def test_data_stream(self) -> None: assert repr.startswith("") 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