Skip to content

Commit

Permalink
Use f-strings in video and subtitle dir
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Feb 27, 2024
1 parent d432bb2 commit 72621f2
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 79 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
/ipch
/msvc-projects
/src
/docs/_ffmpeg

# Testing.
*.spyderproject
Expand Down
9 changes: 4 additions & 5 deletions av/data/stream.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ cimport libav as lib

cdef class DataStream(Stream):
def __repr__(self):
cls_name = self.__class__.__name__
_type = self.type or "<notype>"
name = self.name or "<nocodec>"

return f"<av.{cls_name} #{self.index} {_type}/{name} at 0x{id(self):x}>"
return (
f"<av.{self.__class__.__name__} #{self.index} {self.type or '<notype>'}/"
f"{self.name or '<nocodec>'} at 0x{id(self):x}>"
)

def encode(self, frame=None):
return []
Expand Down
1 change: 0 additions & 1 deletion av/subtitles/codeccontext.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ from av.subtitles.subtitle cimport SubtitleProxy, SubtitleSet


cdef class SubtitleCodecContext(CodecContext):

cdef _send_packet_and_recv(self, Packet packet):
cdef SubtitleProxy proxy = SubtitleProxy()

Expand Down
1 change: 0 additions & 1 deletion av/subtitles/stream.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

cdef class SubtitleStream(Stream):
pass
55 changes: 16 additions & 39 deletions av/subtitles/subtitle.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@ cdef class SubtitleProxy:


cdef class SubtitleSet:

def __cinit__(self, SubtitleProxy proxy):
self.proxy = proxy
cdef int i
self.rects = tuple(build_subtitle(self, i) for i in range(self.proxy.struct.num_rects))

def __repr__(self):
return "<%s.%s at 0x%x>" % (
self.__class__.__module__,
self.__class__.__name__,
id(self),
)
return f"<{self.__class__.__module__}.{self.__class__.__name__} at 0x{id(self):x}>"

@property
def format(self): return self.proxy.struct.format
Expand Down Expand Up @@ -64,7 +59,6 @@ cdef Subtitle build_subtitle(SubtitleSet subtitle, int index):


cdef class Subtitle:

def __cinit__(self, SubtitleSet subtitle, int index):
if index < 0 or <unsigned int>index >= subtitle.proxy.struct.num_rects:
raise ValueError("subtitle rect index out of range")
Expand All @@ -80,18 +74,13 @@ cdef class Subtitle:
elif self.ptr.type == lib.SUBTITLE_ASS:
self.type = b"ass"
else:
raise ValueError("unknown subtitle type %r" % self.ptr.type)
raise ValueError(f"unknown subtitle type {self.ptr.type!r}")

def __repr__(self):
return "<%s.%s at 0x%x>" % (
self.__class__.__module__,
self.__class__.__name__,
id(self),
)
return f"<{self.__class__.__module__}.{self.__class__.__name__} at 0x{id(self):x}>"


cdef class BitmapSubtitle(Subtitle):

def __cinit__(self, SubtitleSet subtitle, int index):
self.planes = tuple(
BitmapSubtitlePlane(self, i)
Expand All @@ -100,14 +89,9 @@ cdef class BitmapSubtitle(Subtitle):
)

def __repr__(self):
return "<%s.%s %dx%d at %d,%d; at 0x%x>" % (
self.__class__.__module__,
self.__class__.__name__,
self.width,
self.height,
self.x,
self.y,
id(self),
return (
f"<{self.__class__.__module__}.{self.__class__.__name__} "
f"{self.width}x{self.height} at {self.x},{self.y}; at 0x{id(self):x}>"
)

@property
Expand All @@ -132,9 +116,7 @@ cdef class BitmapSubtitle(Subtitle):


cdef class BitmapSubtitlePlane:

def __cinit__(self, BitmapSubtitle subtitle, int index):

if index >= 4:
raise ValueError("BitmapSubtitles have only 4 planes")
if not subtitle.ptr.linesize[index]:
Expand All @@ -146,34 +128,29 @@ cdef class BitmapSubtitlePlane:
self._buffer = <void*>subtitle.ptr.data[index]

# New-style buffer support.

def __getbuffer__(self, Py_buffer *view, int flags):
PyBuffer_FillInfo(view, self, self._buffer, self.buffer_size, 0, flags)


cdef class TextSubtitle(Subtitle):

def __repr__(self):
return "<%s.%s %r at 0x%x>" % (
self.__class__.__module__,
self.__class__.__name__,
self.text,
id(self),
return (
f"<{self.__class__.__module__}.{self.__class__.__name__} "
f"{self.text!r} at 0x{id(self):x}>"
)

@property
def text(self): return self.ptr.text
def text(self):
return self.ptr.text


cdef class AssSubtitle(Subtitle):

def __repr__(self):
return "<%s.%s %r at 0x%x>" % (
self.__class__.__module__,
self.__class__.__name__,
self.ass,
id(self),
return (
f"<{self.__class__.__module__}.{self.__class__.__name__} "
f"{self.ass!r} at 0x{id(self):x}>"
)

@property
def ass(self): return self.ptr.ass
def ass(self):
return self.ptr.ass
2 changes: 0 additions & 2 deletions av/video/codeccontext.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ from av.deprecation import AVDeprecationWarning


cdef class VideoCodecContext(CodecContext):

def __cinit__(self, *args, **kwargs):
self.last_w = 0
self.last_h = 0
Expand All @@ -30,7 +29,6 @@ cdef class VideoCodecContext(CodecContext):
self.ptr.time_base.den = self.ptr.framerate.num or lib.AV_TIME_BASE

cdef _prepare_frames_for_encode(self, Frame input):

if not input:
return [None]

Expand Down
6 changes: 3 additions & 3 deletions av/video/format.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ cdef object _cinit_bypass_sentinel = object()
cdef VideoFormat get_video_format(lib.AVPixelFormat c_format, unsigned int width, unsigned int height):
if c_format == lib.AV_PIX_FMT_NONE:
return None

cdef VideoFormat format = VideoFormat.__new__(VideoFormat, _cinit_bypass_sentinel)
format._init(c_format, width, height)
return format
Expand Down Expand Up @@ -53,9 +54,9 @@ cdef class VideoFormat:

def __repr__(self):
if self.width or self.height:
return "<av.%s %s, %dx%d>" % (self.__class__.__name__, self.name, self.width, self.height)
return f"<av.{self.__class__.__name__} {self.name}, {self.width}x{self.height}>"
else:
return "<av.%s %s>" % (self.__class__.__name__, self.name)
return f"<av.{self.__class__.__name__} {self.name}>"

def __int__(self):
return int(self.pix_fmt)
Expand Down Expand Up @@ -129,7 +130,6 @@ cdef class VideoFormat:


cdef class VideoFormatComponent:

def __cinit__(self, VideoFormat format, size_t index):
self.format = format
self.index = index
Expand Down
18 changes: 6 additions & 12 deletions av/video/frame.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ cdef useful_array(VideoPlane plane, unsigned int bytes_per_pixel=1, str dtype="u


cdef class VideoFrame(Frame):

def __cinit__(self, width=0, height=0, format="yuv420p"):

if width is _cinit_bypass_sentinel:
return

Expand All @@ -88,7 +86,6 @@ cdef class VideoFrame(Frame):
self._init(c_format, width, height)

cdef _init(self, lib.AVPixelFormat format, unsigned int width, unsigned int height):

cdef int res = 0

with nogil:
Expand Down Expand Up @@ -126,14 +123,9 @@ cdef class VideoFrame(Frame):
self._np_buffer = None

def __repr__(self):
return "<av.%s #%d, pts=%s %s %dx%d at 0x%x>" % (
self.__class__.__name__,
self.index,
self.pts,
self.format.name,
self.width,
self.height,
id(self),
return (
f"<av.{self.__class__.__name__} #{self.index}, pts={self.pts} "
f"{self.format.name} {self.width}x{self.height} at 0x{id(self):x}>"
)

@property
Expand Down Expand Up @@ -367,7 +359,9 @@ cdef class VideoFrame(Frame):
useful_array(frame.planes[1], 2)
)).reshape(-1, frame.width)
else:
raise ValueError("Conversion to numpy array with format `%s` is not yet supported" % frame.format.name)
raise ValueError(
f"Conversion to numpy array with format `{frame.format.name}` is not yet supported"
)

@staticmethod
def from_image(img):
Expand Down
2 changes: 1 addition & 1 deletion av/video/plane.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ cdef class VideoPlane(Plane):
self.height = component.height
break
else:
raise RuntimeError("could not find plane %d of %r" % (index, frame.format))
raise RuntimeError(f"could not find plane {index} of {frame.format!r}")

# Sometimes, linesize is negative (and that is meaningful). We are only
# insisting that the buffer size be based on the extent of linesize, and
Expand Down
7 changes: 1 addition & 6 deletions av/video/reformatter.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,8 @@ cdef class VideoReformatter:
cdef int brightness, contrast, saturation
cdef int ret

if (
src_colorspace != dst_colorspace or
src_color_range != dst_color_range
):
if src_colorspace != dst_colorspace or src_color_range != dst_color_range:
with nogil:

# Casts for const-ness, because Cython isn't expressive enough.
ret = lib.sws_getColorspaceDetails(
self.ptr,
Expand All @@ -171,7 +167,6 @@ cdef class VideoReformatter:
err_check(ret)

with nogil:

# Grab the coefficients for the requested transforms.
# The inv_table brings us to linear, and `tbl` to the new space.
if src_colorspace != lib.SWS_CS_DEFAULT:
Expand Down
13 changes: 4 additions & 9 deletions av/video/stream.pyx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
cdef class VideoStream(Stream):

def __repr__(self):
return "<av.%s #%d %s, %s %dx%d at 0x%x>" % (
self.__class__.__name__,
self.index,
self.name,
self.format.name if self.format else None,
self.codec_context.width,
self.codec_context.height,
id(self),
return (
f"<av.{self.__class__.__name__} #{self.index} {self.name}, "
f"{self.format.name if self.format else None} {self.codec_context.width}x"
f"{self.codec_context.height} at 0x{id(self):x}>"
)

0 comments on commit 72621f2

Please sign in to comment.