Skip to content

Commit 4e01ca7

Browse files
jlaineWyattBlue
authored andcommitted
Add typing overloads for CodecContext.create
The return type of `CodecContext.create` can be narrowed based on the name of the requested codec, allowing us to know whether we are dealing with an `AudioCodecContext` or a `VideoCodecContext`. As the same logic is used by `OutputContainer.add_stream`, use a shared list of audio / video codec names. We add the following codec names: - audio : libopus, pcm_alaw, pcm_mulaw - video : libvpx, libx264
1 parent dc0e6d0 commit 4e01ca7

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

av/audio/__init__.pyi

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1+
from typing import Literal
2+
13
from .frame import AudioFrame
24
from .stream import AudioStream
35

6+
_AudioCodecName = Literal[
7+
"aac",
8+
"libopus",
9+
"mp2",
10+
"mp3",
11+
"pcm_alaw",
12+
"pcm_mulaw",
13+
"pcm_s16le",
14+
]
15+
416
__all__ = ("AudioFrame", "AudioStream")

av/codec/context.pyi

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
from enum import Flag, IntEnum
22
from fractions import Fraction
3-
from typing import ClassVar, Literal, cast
3+
from typing import ClassVar, Literal, cast, overload
44

5+
from av.audio import _AudioCodecName
6+
from av.audio.codeccontext import AudioCodecContext
57
from av.packet import Packet
8+
from av.video import _VideoCodecName
9+
from av.video.codeccontext import VideoCodecContext
610

711
from .codec import Codec
812
from .hwaccel import HWAccel
@@ -87,6 +91,21 @@ class CodecContext:
8791
@property
8892
def is_hwaccel(self) -> bool: ...
8993
def open(self, strict: bool = True) -> None: ...
94+
@overload
95+
@staticmethod
96+
def create(
97+
codec: _AudioCodecName,
98+
mode: Literal["r", "w"] | None = None,
99+
hwaccel: HWAccel | None = None,
100+
) -> AudioCodecContext: ...
101+
@overload
102+
@staticmethod
103+
def create(
104+
codec: _VideoCodecName,
105+
mode: Literal["r", "w"] | None = None,
106+
hwaccel: HWAccel | None = None,
107+
) -> VideoCodecContext: ...
108+
@overload
90109
@staticmethod
91110
def create(
92111
codec: str | Codec,

av/container/output.pyi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from fractions import Fraction
2-
from typing import Literal, Sequence, TypeVar, Union, overload
2+
from typing import Sequence, TypeVar, Union, overload
33

4+
from av.audio import _AudioCodecName
45
from av.audio.stream import AudioStream
56
from av.data.stream import DataStream
67
from av.packet import Packet
7-
from av.stream import Stream
88
from av.subtitles.stream import SubtitleStream
9+
from av.video import _VideoCodecName
910
from av.video.stream import VideoStream
1011

1112
from .core import Container
@@ -17,15 +18,15 @@ class OutputContainer(Container):
1718
@overload
1819
def add_stream(
1920
self,
20-
codec_name: Literal["pcm_s16le", "aac", "mp3", "mp2"],
21+
codec_name: _AudioCodecName,
2122
rate: int | None = None,
2223
options: dict[str, str] | None = None,
2324
**kwargs,
2425
) -> AudioStream: ...
2526
@overload
2627
def add_stream(
2728
self,
28-
codec_name: Literal["h264", "hevc", "mpeg4", "png", "gif", "qtrle"],
29+
codec_name: _VideoCodecName,
2930
rate: Fraction | int | None = None,
3031
options: dict[str, str] | None = None,
3132
**kwargs,

av/video/__init__.pyi

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1+
from typing import Literal
2+
13
from .frame import VideoFrame
24
from .stream import VideoStream
35

6+
_VideoCodecName = Literal[
7+
"gif",
8+
"h264",
9+
"hevc",
10+
"libvpx",
11+
"libx264",
12+
"mpeg4",
13+
"png",
14+
"qtrle",
15+
]
16+
417
__all__ = ("VideoFrame", "VideoStream")

0 commit comments

Comments
 (0)