Skip to content

Commit 72621f2

Browse files
committed
Use f-strings in video and subtitle dir
1 parent d432bb2 commit 72621f2

11 files changed

+36
-79
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
/ipch
3131
/msvc-projects
3232
/src
33+
/docs/_ffmpeg
3334

3435
# Testing.
3536
*.spyderproject

av/data/stream.pyx

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ cimport libav as lib
33

44
cdef class DataStream(Stream):
55
def __repr__(self):
6-
cls_name = self.__class__.__name__
7-
_type = self.type or "<notype>"
8-
name = self.name or "<nocodec>"
9-
10-
return f"<av.{cls_name} #{self.index} {_type}/{name} at 0x{id(self):x}>"
6+
return (
7+
f"<av.{self.__class__.__name__} #{self.index} {self.type or '<notype>'}/"
8+
f"{self.name or '<nocodec>'} at 0x{id(self):x}>"
9+
)
1110

1211
def encode(self, frame=None):
1312
return []

av/subtitles/codeccontext.pyx

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ from av.subtitles.subtitle cimport SubtitleProxy, SubtitleSet
66

77

88
cdef class SubtitleCodecContext(CodecContext):
9-
109
cdef _send_packet_and_recv(self, Packet packet):
1110
cdef SubtitleProxy proxy = SubtitleProxy()
1211

av/subtitles/stream.pyx

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
21
cdef class SubtitleStream(Stream):
32
pass

av/subtitles/subtitle.pyx

+16-39
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,13 @@ cdef class SubtitleProxy:
77

88

99
cdef class SubtitleSet:
10-
1110
def __cinit__(self, SubtitleProxy proxy):
1211
self.proxy = proxy
1312
cdef int i
1413
self.rects = tuple(build_subtitle(self, i) for i in range(self.proxy.struct.num_rects))
1514

1615
def __repr__(self):
17-
return "<%s.%s at 0x%x>" % (
18-
self.__class__.__module__,
19-
self.__class__.__name__,
20-
id(self),
21-
)
16+
return f"<{self.__class__.__module__}.{self.__class__.__name__} at 0x{id(self):x}>"
2217

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

6560

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

8579
def __repr__(self):
86-
return "<%s.%s at 0x%x>" % (
87-
self.__class__.__module__,
88-
self.__class__.__name__,
89-
id(self),
90-
)
80+
return f"<{self.__class__.__module__}.{self.__class__.__name__} at 0x{id(self):x}>"
9181

9282

9383
cdef class BitmapSubtitle(Subtitle):
94-
9584
def __cinit__(self, SubtitleSet subtitle, int index):
9685
self.planes = tuple(
9786
BitmapSubtitlePlane(self, i)
@@ -100,14 +89,9 @@ cdef class BitmapSubtitle(Subtitle):
10089
)
10190

10291
def __repr__(self):
103-
return "<%s.%s %dx%d at %d,%d; at 0x%x>" % (
104-
self.__class__.__module__,
105-
self.__class__.__name__,
106-
self.width,
107-
self.height,
108-
self.x,
109-
self.y,
110-
id(self),
92+
return (
93+
f"<{self.__class__.__module__}.{self.__class__.__name__} "
94+
f"{self.width}x{self.height} at {self.x},{self.y}; at 0x{id(self):x}>"
11195
)
11296

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

133117

134118
cdef class BitmapSubtitlePlane:
135-
136119
def __cinit__(self, BitmapSubtitle subtitle, int index):
137-
138120
if index >= 4:
139121
raise ValueError("BitmapSubtitles have only 4 planes")
140122
if not subtitle.ptr.linesize[index]:
@@ -146,34 +128,29 @@ cdef class BitmapSubtitlePlane:
146128
self._buffer = <void*>subtitle.ptr.data[index]
147129

148130
# New-style buffer support.
149-
150131
def __getbuffer__(self, Py_buffer *view, int flags):
151132
PyBuffer_FillInfo(view, self, self._buffer, self.buffer_size, 0, flags)
152133

153134

154135
cdef class TextSubtitle(Subtitle):
155-
156136
def __repr__(self):
157-
return "<%s.%s %r at 0x%x>" % (
158-
self.__class__.__module__,
159-
self.__class__.__name__,
160-
self.text,
161-
id(self),
137+
return (
138+
f"<{self.__class__.__module__}.{self.__class__.__name__} "
139+
f"{self.text!r} at 0x{id(self):x}>"
162140
)
163141

164142
@property
165-
def text(self): return self.ptr.text
143+
def text(self):
144+
return self.ptr.text
166145

167146

168147
cdef class AssSubtitle(Subtitle):
169-
170148
def __repr__(self):
171-
return "<%s.%s %r at 0x%x>" % (
172-
self.__class__.__module__,
173-
self.__class__.__name__,
174-
self.ass,
175-
id(self),
149+
return (
150+
f"<{self.__class__.__module__}.{self.__class__.__name__} "
151+
f"{self.ass!r} at 0x{id(self):x}>"
176152
)
177153

178154
@property
179-
def ass(self): return self.ptr.ass
155+
def ass(self):
156+
return self.ptr.ass

av/video/codeccontext.pyx

-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ from av.deprecation import AVDeprecationWarning
1515

1616

1717
cdef class VideoCodecContext(CodecContext):
18-
1918
def __cinit__(self, *args, **kwargs):
2019
self.last_w = 0
2120
self.last_h = 0
@@ -30,7 +29,6 @@ cdef class VideoCodecContext(CodecContext):
3029
self.ptr.time_base.den = self.ptr.framerate.num or lib.AV_TIME_BASE
3130

3231
cdef _prepare_frames_for_encode(self, Frame input):
33-
3432
if not input:
3533
return [None]
3634

av/video/format.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ cdef object _cinit_bypass_sentinel = object()
44
cdef VideoFormat get_video_format(lib.AVPixelFormat c_format, unsigned int width, unsigned int height):
55
if c_format == lib.AV_PIX_FMT_NONE:
66
return None
7+
78
cdef VideoFormat format = VideoFormat.__new__(VideoFormat, _cinit_bypass_sentinel)
89
format._init(c_format, width, height)
910
return format
@@ -53,9 +54,9 @@ cdef class VideoFormat:
5354

5455
def __repr__(self):
5556
if self.width or self.height:
56-
return "<av.%s %s, %dx%d>" % (self.__class__.__name__, self.name, self.width, self.height)
57+
return f"<av.{self.__class__.__name__} {self.name}, {self.width}x{self.height}>"
5758
else:
58-
return "<av.%s %s>" % (self.__class__.__name__, self.name)
59+
return f"<av.{self.__class__.__name__} {self.name}>"
5960

6061
def __int__(self):
6162
return int(self.pix_fmt)
@@ -129,7 +130,6 @@ cdef class VideoFormat:
129130

130131

131132
cdef class VideoFormatComponent:
132-
133133
def __cinit__(self, VideoFormat format, size_t index):
134134
self.format = format
135135
self.index = index

av/video/frame.pyx

+6-12
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ cdef useful_array(VideoPlane plane, unsigned int bytes_per_pixel=1, str dtype="u
7777

7878

7979
cdef class VideoFrame(Frame):
80-
8180
def __cinit__(self, width=0, height=0, format="yuv420p"):
82-
8381
if width is _cinit_bypass_sentinel:
8482
return
8583

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

9088
cdef _init(self, lib.AVPixelFormat format, unsigned int width, unsigned int height):
91-
9289
cdef int res = 0
9390

9491
with nogil:
@@ -126,14 +123,9 @@ cdef class VideoFrame(Frame):
126123
self._np_buffer = None
127124

128125
def __repr__(self):
129-
return "<av.%s #%d, pts=%s %s %dx%d at 0x%x>" % (
130-
self.__class__.__name__,
131-
self.index,
132-
self.pts,
133-
self.format.name,
134-
self.width,
135-
self.height,
136-
id(self),
126+
return (
127+
f"<av.{self.__class__.__name__} #{self.index}, pts={self.pts} "
128+
f"{self.format.name} {self.width}x{self.height} at 0x{id(self):x}>"
137129
)
138130

139131
@property
@@ -367,7 +359,9 @@ cdef class VideoFrame(Frame):
367359
useful_array(frame.planes[1], 2)
368360
)).reshape(-1, frame.width)
369361
else:
370-
raise ValueError("Conversion to numpy array with format `%s` is not yet supported" % frame.format.name)
362+
raise ValueError(
363+
f"Conversion to numpy array with format `{frame.format.name}` is not yet supported"
364+
)
371365

372366
@staticmethod
373367
def from_image(img):

av/video/plane.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ cdef class VideoPlane(Plane):
1717
self.height = component.height
1818
break
1919
else:
20-
raise RuntimeError("could not find plane %d of %r" % (index, frame.format))
20+
raise RuntimeError(f"could not find plane {index} of {frame.format!r}")
2121

2222
# Sometimes, linesize is negative (and that is meaningful). We are only
2323
# insisting that the buffer size be based on the extent of linesize, and

av/video/reformatter.pyx

+1-6
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,8 @@ cdef class VideoReformatter:
150150
cdef int brightness, contrast, saturation
151151
cdef int ret
152152

153-
if (
154-
src_colorspace != dst_colorspace or
155-
src_color_range != dst_color_range
156-
):
153+
if src_colorspace != dst_colorspace or src_color_range != dst_color_range:
157154
with nogil:
158-
159155
# Casts for const-ness, because Cython isn't expressive enough.
160156
ret = lib.sws_getColorspaceDetails(
161157
self.ptr,
@@ -171,7 +167,6 @@ cdef class VideoReformatter:
171167
err_check(ret)
172168

173169
with nogil:
174-
175170
# Grab the coefficients for the requested transforms.
176171
# The inv_table brings us to linear, and `tbl` to the new space.
177172
if src_colorspace != lib.SWS_CS_DEFAULT:

av/video/stream.pyx

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
cdef class VideoStream(Stream):
2-
32
def __repr__(self):
4-
return "<av.%s #%d %s, %s %dx%d at 0x%x>" % (
5-
self.__class__.__name__,
6-
self.index,
7-
self.name,
8-
self.format.name if self.format else None,
9-
self.codec_context.width,
10-
self.codec_context.height,
11-
id(self),
3+
return (
4+
f"<av.{self.__class__.__name__} #{self.index} {self.name}, "
5+
f"{self.format.name if self.format else None} {self.codec_context.width}x"
6+
f"{self.codec_context.height} at 0x{id(self):x}>"
127
)

0 commit comments

Comments
 (0)