From 471fc13dc5ed8c6a42ed8aa074144e53749282ca Mon Sep 17 00:00:00 2001 From: WyattBlue Date: Thu, 9 May 2024 01:47:23 -0400 Subject: [PATCH] Subtitles: make text bytes, type hints --- av/packet.pyi | 3 +-- av/subtitles/codeccontext.pyx | 13 +++++++------ av/subtitles/subtitle.pxd | 5 ----- av/subtitles/subtitle.pyi | 3 ++- av/subtitles/subtitle.pyx | 4 +++- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/av/packet.pyi b/av/packet.pyi index 14b4b0bce..24cb9f5a0 100644 --- a/av/packet.pyi +++ b/av/packet.pyi @@ -1,6 +1,5 @@ from collections.abc import Buffer from fractions import Fraction -from typing import Iterator from av.subtitles.subtitle import SubtitleSet @@ -22,5 +21,5 @@ class Packet(Buffer): is_disposable: bool def __init__(self, input: int | bytes | None = None) -> None: ... - def decode(self) -> Iterator[SubtitleSet]: ... + def decode(self) -> list[SubtitleSet]: ... def __buffer__(self, arg1) -> memoryview: ... diff --git a/av/subtitles/codeccontext.pyx b/av/subtitles/codeccontext.pyx index 227add919..10e8a6009 100644 --- a/av/subtitles/codeccontext.pyx +++ b/av/subtitles/codeccontext.pyx @@ -8,13 +8,14 @@ from av.subtitles.subtitle cimport SubtitleProxy, SubtitleSet cdef class SubtitleCodecContext(CodecContext): cdef _send_packet_and_recv(self, Packet packet): cdef SubtitleProxy proxy = SubtitleProxy() - cdef int got_frame = 0 - err_check(lib.avcodec_decode_subtitle2( - self.ptr, - &proxy.struct, - &got_frame, - packet.ptr if packet else NULL)) + + err_check( + lib.avcodec_decode_subtitle2( + self.ptr, &proxy.struct, &got_frame, packet.ptr if packet else NULL + ) + ) + if got_frame: return [SubtitleSet(proxy)] else: diff --git a/av/subtitles/subtitle.pxd b/av/subtitles/subtitle.pxd index e9003ab9b..29ddc4220 100644 --- a/av/subtitles/subtitle.pxd +++ b/av/subtitles/subtitle.pxd @@ -4,19 +4,16 @@ from av.packet cimport Packet cdef class SubtitleProxy: - cdef lib.AVSubtitle struct cdef class SubtitleSet: - cdef readonly Packet packet cdef SubtitleProxy proxy cdef readonly tuple rects cdef class Subtitle: - cdef SubtitleProxy proxy cdef lib.AVSubtitleRect *ptr cdef readonly bytes type @@ -28,11 +25,9 @@ cdef class ASSSubtitle(Subtitle): pass cdef class BitmapSubtitle(Subtitle): - cdef readonly planes cdef class BitmapSubtitlePlane: - cdef readonly BitmapSubtitle subtitle cdef readonly int index cdef readonly long buffer_size diff --git a/av/subtitles/subtitle.pyi b/av/subtitles/subtitle.pyi index 6dcd16e8d..cdbe7fb31 100644 --- a/av/subtitles/subtitle.pyi +++ b/av/subtitles/subtitle.pyi @@ -5,6 +5,7 @@ class SubtitleSet: start_display_time: int end_display_time: int pts: int + rects: tuple[Subtitle] def __len__(self) -> int: ... def __iter__(self) -> Iterator[Subtitle]: ... @@ -29,7 +30,7 @@ class BitmapSubtitlePlane: class TextSubtitle(Subtitle): type: Literal[b"text"] - text: str + text: bytes class AssSubtitle(Subtitle): type: Literal[b"ass"] diff --git a/av/subtitles/subtitle.pyx b/av/subtitles/subtitle.pyx index d8d45e3fc..e1deca7b7 100644 --- a/av/subtitles/subtitle.pyx +++ b/av/subtitles/subtitle.pyx @@ -145,7 +145,9 @@ cdef class TextSubtitle(Subtitle): @property def text(self): - return self.ptr.text + if self.ptr.text is not NULL: + return PyBytes_FromString(self.ptr.text) + return b"" cdef class AssSubtitle(Subtitle):