Skip to content

Commit

Permalink
Allow user to select audio codec when opening an audio device
Browse files Browse the repository at this point in the history
  • Loading branch information
z-khan authored Jan 29, 2025
1 parent cc06a6c commit dd2d71a
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
39 changes: 38 additions & 1 deletion av/container/core.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from enum import Flag
from enum import Flag, IntEnum
from fractions import Fraction
from pathlib import Path
from types import TracebackType
Expand Down Expand Up @@ -30,6 +30,43 @@ class Flags(Flag):
shortest: ClassVar[Flags]
auto_bsf: ClassVar[Flags]

class AudioCodec(IntEnum):
none: int
pcm_alaw: int
pcm_bluray: int
pcm_dvd: int
pcm_f16le: int
pcm_f24le: int
pcm_f32be: int
pcm_f32le: int
pcm_f64be: int
pcm_f64le: int
pcm_lxf: int
pcm_mulaw: int
pcm_s16be: int
pcm_s16be_planar: int
pcm_s16le: int
pcm_s16le_planar: int
pcm_s24be: int
pcm_s24daud: int
pcm_s24le: int
pcm_s24le_planar: int
pcm_s32be: int
pcm_s32le: int
pcm_s32le_planar: int
pcm_s64be: int
pcm_s64le: int
pcm_s8: int
pcm_s8_planar: int
pcm_u16be: int
pcm_u16le: int
pcm_u24be: int
pcm_u24le: int
pcm_u32be: int
pcm_u32le: int
pcm_u8: int
pcm_vidc: int

class Container:
writeable: bool
name: str
Expand Down
46 changes: 45 additions & 1 deletion av/container/core.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from libc.stdint cimport int64_t

import os
import time
from enum import Flag
from enum import Flag, IntEnum
from pathlib import Path

cimport libav as lib
Expand Down Expand Up @@ -141,6 +141,44 @@ class Flags(Flag):
shortest: "Stop muxing when the shortest stream stops." = lib.AVFMT_FLAG_SHORTEST
auto_bsf: "Add bitstream filters as requested by the muxer." = lib.AVFMT_FLAG_AUTO_BSF

class AudioCodec(IntEnum):
"""Enumeration for audio codec IDs."""
none = lib.AV_CODEC_ID_NONE # No codec.
pcm_alaw = lib.AV_CODEC_ID_PCM_ALAW # PCM A-law.
pcm_bluray = lib.AV_CODEC_ID_PCM_BLURAY # PCM Blu-ray.
pcm_dvd = lib.AV_CODEC_ID_PCM_DVD # PCM DVD.
pcm_f16le = lib.AV_CODEC_ID_PCM_F16LE # PCM F16 little-endian.
pcm_f24le = lib.AV_CODEC_ID_PCM_F24LE # PCM F24 little-endian.
pcm_f32be = lib.AV_CODEC_ID_PCM_F32BE # PCM F32 big-endian.
pcm_f32le = lib.AV_CODEC_ID_PCM_F32LE # PCM F32 little-endian.
pcm_f64be = lib.AV_CODEC_ID_PCM_F64BE # PCM F64 big-endian.
pcm_f64le = lib.AV_CODEC_ID_PCM_F64LE # PCM F64 little-endian.
pcm_lxf = lib.AV_CODEC_ID_PCM_LXF # PCM LXF.
pcm_mulaw = lib.AV_CODEC_ID_PCM_MULAW # PCM μ-law.
pcm_s16be = lib.AV_CODEC_ID_PCM_S16BE # PCM signed 16-bit big-endian.
pcm_s16be_planar = lib.AV_CODEC_ID_PCM_S16BE_PLANAR # PCM signed 16-bit big-endian planar.
pcm_s16le = lib.AV_CODEC_ID_PCM_S16LE # PCM signed 16-bit little-endian.
pcm_s16le_planar = lib.AV_CODEC_ID_PCM_S16LE_PLANAR # PCM signed 16-bit little-endian planar.
pcm_s24be = lib.AV_CODEC_ID_PCM_S24BE # PCM signed 24-bit big-endian.
pcm_s24daud = lib.AV_CODEC_ID_PCM_S24DAUD # PCM signed 24-bit D-Cinema audio.
pcm_s24le = lib.AV_CODEC_ID_PCM_S24LE # PCM signed 24-bit little-endian.
pcm_s24le_planar = lib.AV_CODEC_ID_PCM_S24LE_PLANAR # PCM signed 24-bit little-endian planar.
pcm_s32be = lib.AV_CODEC_ID_PCM_S32BE # PCM signed 32-bit big-endian.
pcm_s32le = lib.AV_CODEC_ID_PCM_S32LE # PCM signed 32-bit little-endian.
pcm_s32le_planar = lib.AV_CODEC_ID_PCM_S32LE_PLANAR # PCM signed 32-bit little-endian planar.
pcm_s64be = lib.AV_CODEC_ID_PCM_S64BE # PCM signed 64-bit big-endian.
pcm_s64le = lib.AV_CODEC_ID_PCM_S64LE # PCM signed 64-bit little-endian.
pcm_s8 = lib.AV_CODEC_ID_PCM_S8 # PCM signed 8-bit.
pcm_s8_planar = lib.AV_CODEC_ID_PCM_S8_PLANAR # PCM signed 8-bit planar.
pcm_u16be = lib.AV_CODEC_ID_PCM_U16BE # PCM unsigned 16-bit big-endian.
pcm_u16le = lib.AV_CODEC_ID_PCM_U16LE # PCM unsigned 16-bit little-endian.
pcm_u24be = lib.AV_CODEC_ID_PCM_U24BE # PCM unsigned 24-bit big-endian.
pcm_u24le = lib.AV_CODEC_ID_PCM_U24LE # PCM unsigned 24-bit little-endian.
pcm_u32be = lib.AV_CODEC_ID_PCM_U32BE # PCM unsigned 32-bit big-endian.
pcm_u32le = lib.AV_CODEC_ID_PCM_U32LE # PCM unsigned 32-bit little-endian.
pcm_u8 = lib.AV_CODEC_ID_PCM_U8 # PCM unsigned 8-bit.
pcm_vidc = lib.AV_CODEC_ID_PCM_VIDC # PCM VIDC.


cdef class Container:
def __cinit__(self, sentinel, file_, format_name, options,
Expand Down Expand Up @@ -176,7 +214,10 @@ cdef class Container:
self.buffer_size = buffer_size
self.io_open = io_open

acodec = None # no audio codec specified
if format_name is not None:
if ":" in format_name:
format_name, acodec = format_name.split(":")
self.format = ContainerFormat(format_name)

self.input_was_opened = False
Expand Down Expand Up @@ -211,6 +252,9 @@ cdef class Container:
self.ptr.interrupt_callback.callback = interrupt_cb
self.ptr.interrupt_callback.opaque = &self.interrupt_callback_info

if acodec is not None:
self.ptr.audio_codec_id = getattr(AudioCodec, acodec)

self.ptr.flags |= lib.AVFMT_FLAG_GENPTS
self.ptr.opaque = <void*>self

Expand Down
34 changes: 34 additions & 0 deletions include/libavcodec/avcodec.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,40 @@ cdef extern from "libavcodec/avcodec.h" nogil:
AV_CODEC_ID_NONE
AV_CODEC_ID_MPEG2VIDEO
AV_CODEC_ID_MPEG1VIDEO
AV_CODEC_ID_PCM_ALAW
AV_CODEC_ID_PCM_BLURAY
AV_CODEC_ID_PCM_DVD
AV_CODEC_ID_PCM_F16LE
AV_CODEC_ID_PCM_F24LE
AV_CODEC_ID_PCM_F32BE
AV_CODEC_ID_PCM_F32LE
AV_CODEC_ID_PCM_F64BE
AV_CODEC_ID_PCM_F64LE
AV_CODEC_ID_PCM_LXF
AV_CODEC_ID_PCM_MULAW
AV_CODEC_ID_PCM_S16BE
AV_CODEC_ID_PCM_S16BE_PLANAR
AV_CODEC_ID_PCM_S16LE
AV_CODEC_ID_PCM_S16LE_PLANAR
AV_CODEC_ID_PCM_S24BE
AV_CODEC_ID_PCM_S24DAUD
AV_CODEC_ID_PCM_S24LE
AV_CODEC_ID_PCM_S24LE_PLANAR
AV_CODEC_ID_PCM_S32BE
AV_CODEC_ID_PCM_S32LE
AV_CODEC_ID_PCM_S32LE_PLANAR
AV_CODEC_ID_PCM_S64BE
AV_CODEC_ID_PCM_S64LE
AV_CODEC_ID_PCM_S8
AV_CODEC_ID_PCM_S8_PLANAR
AV_CODEC_ID_PCM_U16BE
AV_CODEC_ID_PCM_U16LE
AV_CODEC_ID_PCM_U24BE
AV_CODEC_ID_PCM_U24LE
AV_CODEC_ID_PCM_U32BE
AV_CODEC_ID_PCM_U32LE
AV_CODEC_ID_PCM_U8
AV_CODEC_ID_PCM_VIDC

cdef enum AVDiscard:
AVDISCARD_NONE
Expand Down
1 change: 1 addition & 0 deletions include/libavformat/avformat.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ cdef extern from "libavformat/avformat.h" nogil:
int flags
int64_t max_analyze_duration

AVCodecID audio_codec_id
void *opaque

int (*io_open)(
Expand Down

0 comments on commit dd2d71a

Please sign in to comment.