| 1 |
from typing import Iterator |
| 2 |
|
| 3 |
import cython |
| 4 |
import cython.cimports.libav as lib |
| 5 |
from cython.cimports.av.stream import Stream |
| 6 |
|
| 7 |
|
| 8 |
@cython.cfunc |
| 9 |
def _get_media_type_enum(type: str) -> lib.AVMediaType: |
| 10 |
if type == "video": |
| 11 |
return lib.AVMEDIA_TYPE_VIDEO |
| 12 |
elif type == "audio": |
| 13 |
return lib.AVMEDIA_TYPE_AUDIO |
| 14 |
elif type == "subtitle": |
| 15 |
return lib.AVMEDIA_TYPE_SUBTITLE |
| 16 |
elif type == "attachment": |
| 17 |
return lib.AVMEDIA_TYPE_ATTACHMENT |
| 18 |
elif type == "data": |
| 19 |
return lib.AVMEDIA_TYPE_DATA |
| 20 |
else: |
| 21 |
raise ValueError(f"Invalid stream type: {type}") |
| 22 |
|
| 23 |
|
| 24 |
@cython.cfunc |
| 25 |
@cython.exceptval(check=False) |
| 26 |
def _get_best_stream_index( |
| 27 |
fmtctx: cython.pointer[lib.AVFormatContext], |
| 28 |
enumtype: lib.AVMediaType, |
| 29 |
related: Stream | None, |
| 30 |
) -> cython.int: |
| 31 |
stream_index: cython.int |
| 32 |
|
| 33 |
if related is None: |
| 34 |
stream_index = lib.av_find_best_stream(fmtctx, enumtype, -1, -1, cython.NULL, 0) |
| 35 |
else: |
| 36 |
stream_index = lib.av_find_best_stream( |
| 37 |
fmtctx, enumtype, -1, related.ptr.index, cython.NULL, 0 |
| 38 |
) |
| 39 |
|
| 40 |
return stream_index |
| 41 |
|
| 42 |
|
| 43 |
@cython.final |
| 44 |
@cython.cclass |
| 45 |
class StreamContainer: |
| 46 |
""" |
| 47 |
|
| 48 |
A tuple-like container of :class:`Stream`. |
| 49 |
|
| 50 |
:: |
| 51 |
|
| 52 |
# There are a few ways to pulling out streams. |
| 53 |
first = container.streams[0] |
| 54 |
video = container.streams.video[0] |
| 55 |
audio = container.streams.get(audio=(0, 1)) |
| 56 |
|
| 57 |
|
| 58 |
""" |
| 59 |
|
| 60 |
def __cinit__(self): |
| 61 |
self._streams = [] |
| 62 |
|
| 63 |
@cython.cfunc |
| 64 |
def add_stream(self, stream: Stream) -> cython.void: |
| 65 |
assert stream.ptr.index == len(self._streams) |
| 66 |
self._streams.append(stream) |
| 67 |
|
| 68 |
# Basic tuple interface. |
| 69 |
def __len__(self): |
| 70 |
return len(self._streams) |
| 71 |
|
| 72 |
def __iter__(self): |
| 73 |
return iter(self._streams) |
| 74 |
|
| 75 |
def __getitem__(self, index): |
| 76 |
if isinstance(index, int): |
| 77 |
return self.get(index)[0] |
| 78 |
else: |
| 79 |
return self.get(index) |
| 80 |
|
| 81 |
@property |
| 82 |
def video(self): |
| 83 |
return tuple(s for s in self._streams if s.type == "video") |
| 84 |
|
| 85 |
@property |
| 86 |
def audio(self): |
| 87 |
return tuple(s for s in self._streams if s.type == "audio") |
| 88 |
|
| 89 |
@property |
| 90 |
def subtitles(self): |
| 91 |
return tuple(s for s in self._streams if s.type == "subtitle") |
| 92 |
|
| 93 |
@property |
| 94 |
def data(self): |
| 95 |
return tuple(s for s in self._streams if s.type == "data") |
| 96 |
|
| 97 |
@property |
| 98 |
def attachments(self): |
| 99 |
return tuple(s for s in self._streams if s.type == "attachment") |
| 100 |
|
| 101 |
def _get_streams(self, x) -> Iterator[Stream]: |
| 102 |
if x is None: |
| 103 |
pass |
| 104 |
elif isinstance(x, Stream): |
| 105 |
yield x |
| 106 |
elif isinstance(x, int): |
| 107 |
yield self._streams[x] |
| 108 |
elif isinstance(x, (tuple, list)): |
| 109 |
for item in x: |
| 110 |
yield from self._get_streams(item) |
| 111 |
elif isinstance(x, dict): |
| 112 |
for type_, indices in x.items(): |
| 113 |
# For compatibility with the pseudo signature |
| 114 |
streams = self._streams if type_ == "streams" else getattr(self, type_) |
| 115 |
if not isinstance(indices, (tuple, list)): |
| 116 |
indices = [indices] |
| 117 |
for i in indices: |
| 118 |
yield streams[i] |
| 119 |
else: |
| 120 |
raise TypeError("Argument must be Stream or int.", type(x)) |
| 121 |
|
| 122 |
def get(self, *args, **kwargs): |
| 123 |
"""get(streams=None, video=None, audio=None, subtitles=None, data=None) |
| 124 |
|
| 125 |
Get a selection of :class:`.Stream` as a ``list``. |
| 126 |
|
| 127 |
Positional arguments may be ``int`` (which is an index into the streams), |
| 128 |
or ``list`` or ``tuple`` of those:: |
| 129 |
|
| 130 |
# Get the first channel. |
| 131 |
streams.get(0) |
| 132 |
|
| 133 |
# Get the first two audio channels. |
| 134 |
streams.get(audio=(0, 1)) |
| 135 |
|
| 136 |
Keyword arguments (or dicts as positional arguments) as interpreted |
| 137 |
as ``(stream_type, index_value_or_set)`` pairs:: |
| 138 |
|
| 139 |
# Get the first video channel. |
| 140 |
streams.get(video=0) |
| 141 |
# or |
| 142 |
streams.get({'video': 0}) |
| 143 |
|
| 144 |
:class:`.Stream` objects are passed through untouched. |
| 145 |
|
| 146 |
If nothing is selected, then all streams are returned. |
| 147 |
|
| 148 |
""" |
| 149 |
selection = list(self._get_streams(args)) |
| 150 |
if kwargs: |
| 151 |
selection.extend(self._get_streams(kwargs)) |
| 152 |
|
| 153 |
return selection or self._streams[:] |
| 154 |
|
| 155 |
def best(self, type: str, /, related: Stream | None = None): |
| 156 |
"""best(type: Literal["video", "audio", "subtitle", "attachment", "data"], /, related: Stream | None) |
| 157 |
Finds the "best" stream in the file. Wraps :ffmpeg:`av_find_best_stream`. Example:: |
| 158 |
|
| 159 |
stream = container.streams.best("video") |
| 160 |
|
| 161 |
:param type: The type of stream to find |
| 162 |
:param related: A related stream to use as a reference (optional) |
| 163 |
:return: The best stream of the specified type |
| 164 |
:rtype: Stream | None |
| 165 |
""" |
| 166 |
if len(self._streams) == 0: |
| 167 |
return None |
| 168 |
|
| 169 |
first_stream: Stream = cython.cast(Stream, self._streams[0]) |
| 170 |
container: cython.pointer[lib.AVFormatContext] = first_stream.container.ptr |
| 171 |
stream_index: cython.int = _get_best_stream_index( |
| 172 |
container, _get_media_type_enum(type), related |
| 173 |
) |
| 174 |
|
| 175 |
if stream_index < 0: |
| 176 |
return None |
| 177 |
|
| 178 |
return self._streams[stream_index] |