Quick answer: Convert your music to OGG Vorbis. SDL2_mixer’s OGG support is guaranteed across platforms; MP3 support depends on the SDL2_mixer build flags.
A Pygame game loads music.mp3 via pygame.mixer.music.load. On the dev machine it works; on a teammate’s Linux laptop, pygame.error: Unrecognized music file (format). The file is valid; Pygame can’t decode it.
SDL2_mixer Codec Support
Pygame 2 uses SDL2_mixer. Codec support depends on which decoders SDL2_mixer was compiled with:
- OGG Vorbis: built-in everywhere. Always works.
- WAV: built-in everywhere.
- MP3: requires libmpg123 (or libsmpeg) at SDL2_mixer build time. May be missing on some Linux distros’ packaged builds.
- FLAC: requires libflac.
- MOD/XM: requires libxmp.
Fix 1: Convert to OGG Vorbis
# ffmpeg conversion
ffmpeg -i music.mp3 -c:a libvorbis -qscale:a 6 music.ogg
qscale 6 produces high quality at ~192 kbps. Adjust for your size/quality balance. Use the resulting .ogg file in Pygame:
pygame.mixer.music.load("music.ogg")
pygame.mixer.music.play(loops=-1)
OGG works everywhere Pygame works. Done.
Fix 2: Detect Codec Support at Startup
import pygame, os
def supported_music_formats():
return {
"ogg": True,
"wav": True,
"mp3": test_load("test.mp3"),
}
def test_load(path):
try:
pygame.mixer.music.load(path)
return True
except pygame.error:
return False
Probe at startup with a small test file. Adapt asset paths to the available format.
Fix 3: Path Encoding
If the path contains non-ASCII characters, encoding issues can produce the same error on Windows with non-UTF-8 locale:
path = os.path.normpath(os.path.abspath("music/épée.ogg"))
pygame.mixer.music.load(path)
Absolute paths with normalized separators reduce platform encoding mismatch. Or strip non-ASCII from filenames as a project convention.
Verifying
Cross-platform test: load on Windows, macOS, Linux. OGG should succeed on all three. If you specifically need MP3, ship both versions and select at runtime via the codec probe.
“OGG works. MP3 sometimes works. For cross-platform reliability, just use OGG.”
Add OGG conversion as a build step — designers can author in any format; the build produces guaranteed-loadable assets.