| 1 |
from enum import IntEnum, IntFlag |
| 2 |
|
| 3 |
import cython |
| 4 |
from cython.cimports import libav as lib |
| 5 |
from cython.cimports.av.error import err_check |
| 6 |
from cython.cimports.av.index import wrap_index_entries |
| 7 |
from cython.cimports.av.utils import ( |
| 8 |
avdict_to_dict, |
| 9 |
avrational_to_fraction, |
| 10 |
dict_to_avdict, |
| 11 |
to_avrational, |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
class Disposition(IntFlag): |
| 16 |
default = 1 << 0 |
| 17 |
dub = 1 << 1 |
| 18 |
original = 1 << 2 |
| 19 |
comment = 1 << 3 |
| 20 |
lyrics = 1 << 4 |
| 21 |
karaoke = 1 << 5 |
| 22 |
forced = 1 << 6 |
| 23 |
hearing_impaired = 1 << 7 |
| 24 |
visual_impaired = 1 << 8 |
| 25 |
clean_effects = 1 << 9 |
| 26 |
attached_pic = 1 << 10 |
| 27 |
timed_thumbnails = 1 << 11 |
| 28 |
non_diegetic = 1 << 12 |
| 29 |
captions = 1 << 16 |
| 30 |
descriptions = 1 << 17 |
| 31 |
metadata = 1 << 18 |
| 32 |
dependent = 1 << 19 |
| 33 |
still_image = 1 << 20 |
| 34 |
multilayer = 1 << 21 |
| 35 |
|
| 36 |
|
| 37 |
class Discard(IntEnum): |
| 38 |
none = lib.AVDISCARD_NONE |
| 39 |
default = lib.AVDISCARD_DEFAULT |
| 40 |
nonref = lib.AVDISCARD_NONREF |
| 41 |
bidir = lib.AVDISCARD_BIDIR |
| 42 |
nonintra = lib.AVDISCARD_NONINTRA |
| 43 |
nonkey = lib.AVDISCARD_NONKEY |
| 44 |
all = lib.AVDISCARD_ALL |
| 45 |
|
| 46 |
|
| 47 |
_cinit_bypass_sentinel = cython.declare(object, object()) |
| 48 |
|
| 49 |
|
| 50 |
@cython.cfunc |
| 51 |
def wrap_stream( |
| 52 |
container: Container, |
| 53 |
c_stream: cython.pointer[lib.AVStream], |
| 54 |
codec_context: CodecContext, |
| 55 |
) -> Stream: |
| 56 |
"""Build an av.Stream for an existing AVStream. |
| 57 |
|
| 58 |
The AVStream MUST be fully constructed and ready for use before this is called. |
| 59 |
""" |
| 60 |
|
| 61 |
# This better be the right one... |
| 62 |
assert container.ptr.streams[c_stream.index] == c_stream |
| 63 |
|
| 64 |
py_stream: Stream |
| 65 |
|
| 66 |
from av.audio.stream import AudioStream |
| 67 |
from av.subtitles.stream import SubtitleStream |
| 68 |
from av.video.stream import VideoStream |
| 69 |
|
| 70 |
if c_stream.codecpar.codec_type == lib.AVMEDIA_TYPE_VIDEO: |
| 71 |
py_stream = VideoStream.__new__(VideoStream, _cinit_bypass_sentinel) |
| 72 |
elif c_stream.codecpar.codec_type == lib.AVMEDIA_TYPE_AUDIO: |
| 73 |
py_stream = AudioStream.__new__(AudioStream, _cinit_bypass_sentinel) |
| 74 |
elif c_stream.codecpar.codec_type == lib.AVMEDIA_TYPE_SUBTITLE: |
| 75 |
py_stream = SubtitleStream.__new__(SubtitleStream, _cinit_bypass_sentinel) |
| 76 |
elif c_stream.codecpar.codec_type == lib.AVMEDIA_TYPE_ATTACHMENT: |
| 77 |
py_stream = AttachmentStream.__new__(AttachmentStream, _cinit_bypass_sentinel) |
| 78 |
elif c_stream.codecpar.codec_type == lib.AVMEDIA_TYPE_DATA: |
| 79 |
py_stream = DataStream.__new__(DataStream, _cinit_bypass_sentinel) |
| 80 |
else: |
| 81 |
py_stream = Stream.__new__(Stream, _cinit_bypass_sentinel) |
| 82 |
|
| 83 |
py_stream._init(container, c_stream, codec_context) |
| 84 |
return py_stream |
| 85 |
|
| 86 |
|
| 87 |
@cython.cclass |
| 88 |
class Stream: |
| 89 |
""" |
| 90 |
A single stream of audio, video or subtitles within a :class:`.Container`. |
| 91 |
|
| 92 |
:: |
| 93 |
|
| 94 |
>>> fh = av.open(video_path) |
| 95 |
>>> stream = fh.streams.video[0] |
| 96 |
>>> stream |
| 97 |
<av.VideoStream #0 h264, yuv420p 1280x720 at 0x...> |
| 98 |
|
| 99 |
This encapsulates a :class:`.CodecContext`, located at :attr:`Stream.codec_context`. |
| 100 |
Attribute access is passed through to that context when attributes are missing |
| 101 |
on the stream itself. E.g. ``stream.options`` will be the options on the |
| 102 |
context. |
| 103 |
""" |
| 104 |
|
| 105 |
def __cinit__(self, name): |
| 106 |
if name is _cinit_bypass_sentinel: |
| 107 |
return |
| 108 |
raise RuntimeError("cannot manually instantiate Stream") |
| 109 |
|
| 110 |
@cython.cfunc |
| 111 |
def _init( |
| 112 |
self, |
| 113 |
container: Container, |
| 114 |
stream: cython.pointer[lib.AVStream], |
| 115 |
codec_context: CodecContext, |
| 116 |
): |
| 117 |
self.container = container |
| 118 |
self.ptr = stream |
| 119 |
self.index_entries = wrap_index_entries(self.ptr) |
| 120 |
|
| 121 |
self.codec_context = codec_context |
| 122 |
if self.codec_context: |
| 123 |
self.codec_context.stream_index = stream.index |
| 124 |
|
| 125 |
self.metadata = avdict_to_dict( |
| 126 |
stream.metadata, |
| 127 |
encoding=self.container.metadata_encoding, |
| 128 |
errors=self.container.metadata_errors, |
| 129 |
) |
| 130 |
|
| 131 |
def __repr__(self): |
| 132 |
name = getattr(self, "name", None) |
| 133 |
return ( |
| 134 |
f"<av.{self.__class__.__name__} #{self.index} {self.type or '<notype>'}/" |
| 135 |
f"{name or '<nocodec>'} at 0x{id(self):x}>" |
| 136 |
) |
| 137 |
|
| 138 |
def __setattr__(self, name, value): |
| 139 |
if name == "id": |
| 140 |
self._set_id(value) |
| 141 |
return |
| 142 |
if name == "disposition": |
| 143 |
self.ptr.disposition = value |
| 144 |
return |
| 145 |
if name == "discard": |
| 146 |
self.ptr.discard = Discard(value).value |
| 147 |
return |
| 148 |
if name == "time_base": |
| 149 |
to_avrational(value, cython.address(self.ptr.time_base)) |
| 150 |
return |
| 151 |
|
| 152 |
# Convenience setter for codec context properties. |
| 153 |
if self.codec_context is not None: |
| 154 |
setattr(self.codec_context, name, value) |
| 155 |
|
| 156 |
@cython.cfunc |
| 157 |
def _finalize_for_output(self): |
| 158 |
dict_to_avdict( |
| 159 |
cython.address(self.ptr.metadata), |
| 160 |
self.metadata, |
| 161 |
encoding=self.container.metadata_encoding, |
| 162 |
errors=self.container.metadata_errors, |
| 163 |
) |
| 164 |
|
| 165 |
if self.codec_context is None: |
| 166 |
return |
| 167 |
|
| 168 |
if not self.ptr.time_base.num: |
| 169 |
self.ptr.time_base = self.codec_context.ptr.time_base |
| 170 |
|
| 171 |
# It prefers if we pass it parameters via this other object. Let's just copy what we want. |
| 172 |
err_check( |
| 173 |
lib.avcodec_parameters_from_context( |
| 174 |
self.ptr.codecpar, self.codec_context.ptr |
| 175 |
) |
| 176 |
) |
| 177 |
|
| 178 |
@property |
| 179 |
def id(self): |
| 180 |
""" |
| 181 |
The format-specific ID of this stream. |
| 182 |
|
| 183 |
:type: int |
| 184 |
|
| 185 |
""" |
| 186 |
return self.ptr.id |
| 187 |
|
| 188 |
@cython.cfunc |
| 189 |
def _set_id(self, value): |
| 190 |
if value is None: |
| 191 |
self.ptr.id = 0 |
| 192 |
else: |
| 193 |
self.ptr.id = value |
| 194 |
|
| 195 |
@property |
| 196 |
def profiles(self): |
| 197 |
""" |
| 198 |
List the available profiles for this stream. |
| 199 |
|
| 200 |
:type: list[str] |
| 201 |
""" |
| 202 |
if self.codec_context: |
| 203 |
return self.codec_context.profiles |
| 204 |
else: |
| 205 |
return [] |
| 206 |
|
| 207 |
@property |
| 208 |
def profile(self): |
| 209 |
""" |
| 210 |
The profile of this stream. |
| 211 |
|
| 212 |
:type: str |
| 213 |
""" |
| 214 |
if self.codec_context: |
| 215 |
return self.codec_context.profile |
| 216 |
else: |
| 217 |
return None |
| 218 |
|
| 219 |
@property |
| 220 |
def index(self): |
| 221 |
""" |
| 222 |
The index of this stream in its :class:`.Container`. |
| 223 |
|
| 224 |
:type: int |
| 225 |
""" |
| 226 |
return self.ptr.index |
| 227 |
|
| 228 |
@property |
| 229 |
def time_base(self): |
| 230 |
""" |
| 231 |
The unit of time (in fractional seconds) in which timestamps are expressed. |
| 232 |
|
| 233 |
:type: fractions.Fraction | None |
| 234 |
|
| 235 |
""" |
| 236 |
return avrational_to_fraction(cython.address(self.ptr.time_base)) |
| 237 |
|
| 238 |
@property |
| 239 |
def start_time(self): |
| 240 |
""" |
| 241 |
The presentation timestamp in :attr:`time_base` units of the first |
| 242 |
frame in this stream. |
| 243 |
|
| 244 |
:type: int | None |
| 245 |
""" |
| 246 |
if self.ptr.start_time != lib.AV_NOPTS_VALUE: |
| 247 |
return self.ptr.start_time |
| 248 |
|
| 249 |
@property |
| 250 |
def duration(self): |
| 251 |
""" |
| 252 |
The duration of this stream in :attr:`time_base` units. |
| 253 |
|
| 254 |
:type: int | None |
| 255 |
|
| 256 |
""" |
| 257 |
if self.ptr.duration != lib.AV_NOPTS_VALUE: |
| 258 |
return self.ptr.duration |
| 259 |
|
| 260 |
@property |
| 261 |
def frames(self): |
| 262 |
""" |
| 263 |
The number of frames this stream contains. |
| 264 |
|
| 265 |
Returns ``0`` if it is not known. |
| 266 |
|
| 267 |
:type: int |
| 268 |
""" |
| 269 |
return self.ptr.nb_frames |
| 270 |
|
| 271 |
@property |
| 272 |
def language(self): |
| 273 |
""" |
| 274 |
The language of the stream. |
| 275 |
|
| 276 |
:type: str | None |
| 277 |
""" |
| 278 |
return self.metadata.get("language") |
| 279 |
|
| 280 |
@property |
| 281 |
def disposition(self): |
| 282 |
return Disposition(self.ptr.disposition) |
| 283 |
|
| 284 |
@property |
| 285 |
def discard(self): |
| 286 |
""" |
| 287 |
Controls which packets of this stream are discarded by the demuxer. |
| 288 |
|
| 289 |
Set this to e.g. :attr:`Discard.all` on streams you don't need so that |
| 290 |
:meth:`.Container.demux` and :meth:`.Container.seek` skip them, avoiding |
| 291 |
the cost of synchronizing streams you never read. |
| 292 |
|
| 293 |
:type: Discard |
| 294 |
""" |
| 295 |
return Discard(self.ptr.discard) |
| 296 |
|
| 297 |
@property |
| 298 |
def type(self): |
| 299 |
""" |
| 300 |
The type of the stream. |
| 301 |
|
| 302 |
:type: Literal["audio", "video", "subtitle", "data", "attachment"] |
| 303 |
""" |
| 304 |
media_type = lib.av_get_media_type_string(self.ptr.codecpar.codec_type) |
| 305 |
return "unknown" if media_type == cython.NULL else media_type |
| 306 |
|
| 307 |
|
| 308 |
@cython.final |
| 309 |
@cython.cclass |
| 310 |
class DataStream(Stream): |
| 311 |
def __repr__(self): |
| 312 |
return ( |
| 313 |
f"<av.{self.__class__.__name__} #{self.index} data/" |
| 314 |
f"{self.name or '<nocodec>'} at 0x{id(self):x}>" |
| 315 |
) |
| 316 |
|
| 317 |
@property |
| 318 |
def name(self): |
| 319 |
desc: cython.pointer[cython.const[lib.AVCodecDescriptor]] = ( |
| 320 |
lib.avcodec_descriptor_get(self.ptr.codecpar.codec_id) |
| 321 |
) |
| 322 |
if desc == cython.NULL: |
| 323 |
return None |
| 324 |
return desc.name |
| 325 |
|
| 326 |
|
| 327 |
@cython.final |
| 328 |
@cython.cclass |
| 329 |
class AttachmentStream(Stream): |
| 330 |
""" |
| 331 |
An :class:`AttachmentStream` represents a stream of attachment data within a media container. |
| 332 |
Typically used to attach font files that are referenced in ASS/SSA Subtitle Streams. |
| 333 |
""" |
| 334 |
|
| 335 |
@property |
| 336 |
def name(self): |
| 337 |
""" |
| 338 |
Returns the file name of the attachment. |
| 339 |
|
| 340 |
:rtype: str | None |
| 341 |
""" |
| 342 |
return self.metadata.get("filename") |
| 343 |
|
| 344 |
@property |
| 345 |
def mimetype(self): |
| 346 |
""" |
| 347 |
Returns the MIME type of the attachment. |
| 348 |
|
| 349 |
:rtype: str | None |
| 350 |
""" |
| 351 |
return self.metadata.get("mimetype") |
| 352 |
|
| 353 |
@property |
| 354 |
def data(self): |
| 355 |
"""Return the raw attachment payload as bytes.""" |
| 356 |
extradata: cython.p_uchar = self.ptr.codecpar.extradata |
| 357 |
size: cython.Py_ssize_t = self.ptr.codecpar.extradata_size |
| 358 |
if extradata == cython.NULL or size <= 0: |
| 359 |
return b"" |
| 360 |
|
| 361 |
payload = bytearray(size) |
| 362 |
for i in range(size): |
| 363 |
payload[i] = extradata[i] |
| 364 |
|
| 365 |
return bytes(payload) |