From b0bd1ba623c2e454b36c8412809577a0afc95587 Mon Sep 17 00:00:00 2001 From: laggykiller Date: Mon, 4 Mar 2024 07:00:42 +0800 Subject: [PATCH 1/4] Improve stub in av.audio --- av/audio/codeccontext.pyi | 7 +------ av/audio/format.pyi | 6 ++---- av/audio/frame.pyi | 17 ++++++++++++++--- av/audio/layout.pyi | 5 +++-- av/audio/plane.pyi | 4 +++- av/audio/resampler.pyi | 13 ++++++++----- av/audio/stream.pyi | 11 ++++++++++- 7 files changed, 41 insertions(+), 22 deletions(-) diff --git a/av/audio/codeccontext.pyi b/av/audio/codeccontext.pyi index 6dcf039fa..231f64680 100644 --- a/av/audio/codeccontext.pyi +++ b/av/audio/codeccontext.pyi @@ -1,11 +1,6 @@ -from typing import Literal - from av.codec.context import CodecContext -from av.frame import Frame -from av.packet import Packet from .format import AudioFormat -from .frame import AudioFrame from .layout import AudioLayout class AudioCodecContext(CodecContext): @@ -16,4 +11,4 @@ class AudioCodecContext(CodecContext): channel_layout: int layout: AudioLayout format: AudioFormat - type: Literal["audio"] + type = "audio" diff --git a/av/audio/format.pyi b/av/audio/format.pyi index 2a2519008..bb4a1e603 100644 --- a/av/audio/format.pyi +++ b/av/audio/format.pyi @@ -1,11 +1,9 @@ -from typing import Any - class AudioFormat: name: str bytes: int bits: int is_planar: bool is_packed: bool - planar: Any - packed: Any + planar: AudioFormat + packed: AudioFormat container_name: str diff --git a/av/audio/frame.pyi b/av/audio/frame.pyi index 7f78535bf..777c3dd70 100644 --- a/av/audio/frame.pyi +++ b/av/audio/frame.pyi @@ -1,8 +1,19 @@ +from typing import Any, Union + +import numpy as np + from av.frame import Frame from .plane import AudioPlane format_dtypes: dict[str, str] +_SupportedNDarray = Union[ + np.ndarray[Any, np.dtype[np.float64]], # f8 + np.ndarray[Any, np.dtype[np.float32]], # f4 + np.ndarray[Any, np.dtype[np.int32]], # i4 + np.ndarray[Any, np.dtype[np.int16]], # i2 + np.ndarray[Any, np.dtype[np.uint8]], # u1 +] class AudioFrame(Frame): planes: tuple[AudioPlane, ...] @@ -16,9 +27,9 @@ class AudioFrame(Frame): layout: str = "stereo", samples: int = 0, align: int = 1, - ): ... - def to_ndarray(self): ... + ) -> None: ... @staticmethod def from_ndarray( - array, format: str = "s16", layout: str = "stereo" + array: _SupportedNDarray, format: str = "s16", layout: str = "stereo" ) -> AudioFrame: ... + def to_ndarray(self) -> _SupportedNDarray: ... diff --git a/av/audio/layout.pyi b/av/audio/layout.pyi index 600ffeef5..338532e1a 100644 --- a/av/audio/layout.pyi +++ b/av/audio/layout.pyi @@ -1,10 +1,11 @@ channel_descriptions: dict[str, str] class AudioLayout: + name: str layout: int nb_channels: int - channels: tuple[AudioChannels] + channels: tuple[AudioChannel, ...] -class AudioChannels: +class AudioChannel: name: str description: str diff --git a/av/audio/plane.pyi b/av/audio/plane.pyi index c9a190bf1..64524dcdb 100644 --- a/av/audio/plane.pyi +++ b/av/audio/plane.pyi @@ -1,2 +1,4 @@ -class AudioPlane: +from av.plane import Plane + +class AudioPlane(Plane): buffer_size: int diff --git a/av/audio/resampler.pyi b/av/audio/resampler.pyi index 7c2033e83..df2c12f98 100644 --- a/av/audio/resampler.pyi +++ b/av/audio/resampler.pyi @@ -1,17 +1,20 @@ +from av.filter.graph import Graph + from .format import AudioFormat +from .layout import AudioLayout from .frame import AudioFrame class AudioResampler: rate: int frame_size: int format: AudioFormat - graph: None + graph: Graph | None def __init__( self, - format=None, - layout=None, + format: AudioFormat | None = None, + layout: AudioLayout | None = None, rate: int | None = None, frame_size: int | None = None, - ): ... - def resample(self, frame: AudioFrame | None) -> list: ... + ) -> None: ... + def resample(self, frame: AudioFrame | None) -> list[AudioFrame]: ... diff --git a/av/audio/stream.pyi b/av/audio/stream.pyi index 59c259613..d079550e8 100644 --- a/av/audio/stream.pyi +++ b/av/audio/stream.pyi @@ -1,3 +1,12 @@ +from av.packet import Packet from av.stream import Stream -class AudioStream(Stream): ... +from .format import AudioFormat +from .frame import AudioFrame + +class AudioStream(Stream): + format: AudioFormat + type = "audio" + + def encode(self, frame: AudioFrame | None = None) -> list[Packet]: ... + def decode(self, packet: Packet | None = None) -> list[AudioFrame]: ... From 947c478401695e886866a97a266ee1380759dfe6 Mon Sep 17 00:00:00 2001 From: laggykiller Date: Mon, 4 Mar 2024 10:26:28 +0800 Subject: [PATCH 2/4] Fix literal typing --- av/audio/codeccontext.pyi | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/av/audio/codeccontext.pyi b/av/audio/codeccontext.pyi index 231f64680..ed478a906 100644 --- a/av/audio/codeccontext.pyi +++ b/av/audio/codeccontext.pyi @@ -1,3 +1,5 @@ +from typing import Literal + from av.codec.context import CodecContext from .format import AudioFormat @@ -11,4 +13,4 @@ class AudioCodecContext(CodecContext): channel_layout: int layout: AudioLayout format: AudioFormat - type = "audio" + type: Literal["audio"] From 1dd5cdc766760da51e9a0e058be89c87334983a2 Mon Sep 17 00:00:00 2001 From: laggykiller Date: Mon, 4 Mar 2024 11:22:59 +0800 Subject: [PATCH 3/4] Fix formatting --- av/audio/resampler.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/av/audio/resampler.pyi b/av/audio/resampler.pyi index df2c12f98..32861258e 100644 --- a/av/audio/resampler.pyi +++ b/av/audio/resampler.pyi @@ -1,8 +1,8 @@ from av.filter.graph import Graph from .format import AudioFormat -from .layout import AudioLayout from .frame import AudioFrame +from .layout import AudioLayout class AudioResampler: rate: int From 4e485b3c77ff28615ee6da6522203ffe3d99f23d Mon Sep 17 00:00:00 2001 From: laggykiller Date: Tue, 5 Mar 2024 11:46:51 +0800 Subject: [PATCH 4/4] Fix literal typing --- av/audio/stream.pyi | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/av/audio/stream.pyi b/av/audio/stream.pyi index d079550e8..c85f15787 100644 --- a/av/audio/stream.pyi +++ b/av/audio/stream.pyi @@ -1,3 +1,5 @@ +from typing import Literal + from av.packet import Packet from av.stream import Stream @@ -6,7 +8,7 @@ from .frame import AudioFrame class AudioStream(Stream): format: AudioFormat - type = "audio" + type: Literal["audio"] def encode(self, frame: AudioFrame | None = None) -> list[Packet]: ... def decode(self, packet: Packet | None = None) -> list[AudioFrame]: ...