Can’t play a sound extracted from a video using pyav with pygame-ce

  Kiến thức lập trình

Hi i’m trying to make a videoplayer in pygame using the pyav module.

For the audio part, I can’t find any demo code.

Pygame need a buffer or an array for the Sound class, but I don’t really know how to obtain a compatible data from pyav AudioFrame.

import av
import numpy as np
import pygame

def main(video):
    container =


    for frame in container.decode(audio=0):
        data = frame.to_ndarray(type=np.int16).astype(np.int16)  # .tobytes()
        sound = pygame.mixer.Sound(array=data)


if __name__ == "__main__":
    import sys
    if len(sys.argv) != 2:
        print(f"usage: {sys.argv[0]} [code]")

Running this code with a video, it raise an error :
Traceback (most recent call last):
  File "", line 26, in <module>
  File "", line 14, in main
    sound = pygame.mixer.Sound(array=data)
ValueError: Array depth must match number of mixer channels

The mixer is initialized with default values and the video have a stereo sound.
I can play the video with mpv player (based on ffmpeg).

Thanks to give me some hints to progress.

New contributor

M Mentat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.