Skip to content

Commit

Permalink
Subtitles: make text bytes, type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed May 9, 2024
1 parent 7ee9aa0 commit 471fc13
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 15 deletions.
3 changes: 1 addition & 2 deletions av/packet.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from collections.abc import Buffer
from fractions import Fraction
from typing import Iterator

from av.subtitles.subtitle import SubtitleSet

Expand All @@ -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: ...
13 changes: 7 additions & 6 deletions av/subtitles/codeccontext.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 0 additions & 5 deletions av/subtitles/subtitle.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion av/subtitles/subtitle.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]: ...
Expand All @@ -29,7 +30,7 @@ class BitmapSubtitlePlane:

class TextSubtitle(Subtitle):
type: Literal[b"text"]
text: str
text: bytes

class AssSubtitle(Subtitle):
type: Literal[b"ass"]
Expand Down
4 changes: 3 additions & 1 deletion av/subtitles/subtitle.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 471fc13

Please sign in to comment.