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

Subtitles: make text bytes, type hints #1398

Merged
merged 1 commit into from
May 9, 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
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