|
2 | 2 |
|
3 | 3 | import numpy as np
|
4 | 4 | from pluggy import HookimplMarker
|
| 5 | +from typing import Tuple |
| 6 | + |
| 7 | +from numpy.typing import ArrayLike |
5 | 8 |
|
6 | 9 | hookimpl = HookimplMarker("ffmpegio")
|
7 | 10 |
|
|
16 | 19 |
|
17 | 20 |
|
18 | 21 | @hookimpl
|
19 |
| -def video_info(obj: object) -> tuple[tuple[int, int, int], str]: |
| 22 | +def video_info(obj: ArrayLike) -> Tuple[Tuple[int, int, int], str]: |
20 | 23 | """get video frame info
|
21 | 24 |
|
22 |
| - :param obj: object containing video frame data with arbitrary number of frames |
23 |
| - :type obj: object |
24 |
| - :return: shape (height,width,components) and data type in numpy dtype str expression |
25 |
| - :rtype: tuple[tuple[int, int, int], str] |
| 25 | + :param obj: video frame data with arbitrary number of frames |
| 26 | + :type obj: ArrayLike |
| 27 | + :return: shape (height,width,components) and data type str |
| 28 | + :rtype: Tuple[Tuple[int, int, int], str] |
26 | 29 | """
|
27 | 30 | return obj.shape[-3:], obj.dtype.str
|
28 | 31 |
|
29 | 32 |
|
30 | 33 | @hookimpl
|
31 |
| -def audio_info(obj: object) -> tuple[int, str]: |
| 34 | +def audio_info(obj: ArrayLike) -> Tuple[int, str]: |
32 | 35 | """get audio sample info
|
33 | 36 |
|
34 |
| - :param obj: object containing audio data (with interleaving channels) with arbitrary number of samples |
35 |
| - :type obj: object |
36 |
| - :return: number of channels and sample data type in numpy dtype str expression |
37 |
| - :rtype: tuple[tuple[int], str] |
| 37 | + :param obj: column-wise audio data with arbitrary number of samples |
| 38 | + :type obj: ArrayLike |
| 39 | + :return: number of channels and sample data type in data type str |
| 40 | + :rtype: Tuple[Tuple[int], str] |
38 | 41 | """
|
39 | 42 | return obj.shape[-1:], obj.dtype.str
|
40 | 43 |
|
41 | 44 |
|
42 | 45 | @hookimpl
|
43 |
| -def video_bytes(obj: object) -> memoryview: |
44 |
| - """return bytes-like object of packed video pixels, associated with `video_info()` |
| 46 | +def video_bytes(obj: ArrayLike) -> memoryview: |
| 47 | + """return bytes-like object of rawvideo NumPy array |
45 | 48 |
|
46 |
| - :param obj: object containing video frame data with arbitrary number of frames |
47 |
| - :type obj: object |
48 |
| - :return: packed bytes of video frames |
49 |
| - :rtype: bytes-like object |
| 49 | + :param obj: video frame data with arbitrary number of frames |
| 50 | + :type obj: ArrayLike |
| 51 | + :return: memoryview of video frames |
| 52 | + :rtype: memoryview |
50 | 53 | """
|
51 | 54 |
|
52 | 55 | return memoryview(obj)
|
53 | 56 |
|
54 | 57 |
|
55 | 58 | @hookimpl
|
56 |
| -def audio_bytes(obj: object) -> memoryview: |
57 |
| - """return bytes-like object of packed audio samples |
| 59 | +def audio_bytes(obj: ArrayLike) -> memoryview: |
| 60 | + """return bytes-like object of rawaudio NumPy array |
58 | 61 |
|
59 |
| - :param obj: object containing audio data (with interleaving channels) with arbitrary number of samples |
60 |
| - :type obj: object |
61 |
| - :return: packed bytes of audio samples |
62 |
| - :rtype: bytes-like object |
| 62 | + :param obj: column-wise audio data with arbitrary number of samples |
| 63 | + :type obj: ArrayLike |
| 64 | + :return: memoryview of audio samples |
| 65 | + :rtype: memoryview |
63 | 66 | """
|
64 | 67 |
|
65 | 68 | return memoryview(obj)
|
66 | 69 |
|
67 | 70 |
|
68 | 71 | @hookimpl
|
69 |
| -def bytes_to_video(b: bytes, dtype: str, shape: tuple[int, int, int]) -> object: |
70 |
| - """convert bytes to rawvideo object |
| 72 | +def bytes_to_video(b: bytes, dtype: str, shape: Tuple[int, int, int]) -> ArrayLike: |
| 73 | + """convert bytes to rawvideo NumPy array |
71 | 74 |
|
72 | 75 | :param b: byte data of arbitrary number of video frames
|
73 | 76 | :type b: bytes
|
74 |
| - :param dtype: data type numpy dtype string (e.g., '|u1', '<f4') |
| 77 | + :param dtype: data type string (e.g., '|u1', '<f4') |
75 | 78 | :type dtype: str
|
76 | 79 | :param size: frame dimension in pixels and number of color components (height, width, components)
|
77 |
| - :type size: tuple[int, int, int] |
78 |
| - :return: python object holding the rawvideo frames |
79 |
| - :rtype: object |
| 80 | + :type size: Tuple[int, int, int] |
| 81 | + :return: rawvideo frames |
| 82 | + :rtype: ArrayLike |
80 | 83 | """
|
81 | 84 |
|
82 | 85 | return np.frombuffer(b, dtype).reshape(-1, *shape)
|
83 | 86 |
|
84 | 87 |
|
85 | 88 | @hookimpl
|
86 |
| -def bytes_to_audio(b: bytes, dtype: str, shape: tuple[int]) -> object: |
87 |
| - """convert bytes to rawaudio object |
| 89 | +def bytes_to_audio(b: bytes, dtype: str, shape: Tuple[int]) -> ArrayLike: |
| 90 | + """convert bytes to rawaudio NumPy array |
88 | 91 |
|
89 | 92 | :param b: byte data of arbitrary number of video frames
|
90 | 93 | :type b: bytes
|
91 |
| - :param dtype: numpy dtype string of the bytes (e.g., '<s2', '<f4') |
| 94 | + :param dtype: data type string (e.g., '<s2', '<f4') |
92 | 95 | :type dtype: str
|
93 |
| - :param shape: number of interleaved audio channels (1-element tuple) |
94 |
| - :type shape: tuple[int] |
95 |
| - :return: python object to hold the raw audio samples |
96 |
| - :rtype: object |
| 96 | + :param shape: number of audio channels |
| 97 | + :type shape: Tuple[int] |
| 98 | + :return: raw audio samples |
| 99 | + :rtype: ArrayLike |
97 | 100 | """
|
98 | 101 |
|
99 | 102 | return np.frombuffer(b, dtype).reshape(-1, *shape)
|
0 commit comments