DEV Community

Cover image for Playing MP3 Files in Python with Pydub and Pyaudio
Mathew Chan
Mathew Chan

Posted on • Edited on

11 2

Playing MP3 Files in Python with Pydub and Pyaudio

Introduction

Pyaudio allows us to play and record sounds with Python. To play MP3, however, we first need to convert the MP3 file to WAV format with ffmpeg. To use ffmpeg in Python, we use an interface tool called Pydub, which directly calls our ffmpeg executable and integrates with Pyaudio.

Installation

pip install pyaudio
pip install pydub
Enter fullscreen mode Exit fullscreen mode

Playing sound

from pydub import AudioSegment

song = AudioSegment.from_mp3('test.mp3')
play(song)
Enter fullscreen mode Exit fullscreen mode

If ffmpeg isn't installed on the machine, AudioSegment will fail to locate the mp3 file. There are a few solutions:

  1. Install ffmpeg and add to environment path
  2. Copy ffmpeg to the same folder as the python file
  3. Static link ffmpeg to pydub

Let's talk about the third option.

Static linking ffmpeg

By static linking ffmpeg, our program would be portable: users would not need to manually install ffmpeg to their OS.

Download ffmpeg and copy it to your script directory.

import os
import platform
from pathlib import Path
from pydub import AudioSegment

AudioSegment.ffmpeg = path_to_ffmpeg()
platform_name = platform.system()
if platform_name == 'Windows':
    os.environ["PATH"] += os.pathsep + str(Path(path_to_ffmpeg()).parent)
else:
    os.environ["LD_LIBRARY_PATH"] += ":" + str(Path(path_to_ffmpeg()).parent)

def path_to_ffmpeg():
    SCRIPT_DIR = Path(__file__).parent 
    if platform_name == 'Windows':
        return str(Path(SCRIPT_DIR, "win", "ffmpeg", "ffmpeg.exe"))
    elif platform_name == 'Darwin':
        return str(Path(SCRIPT_DIR, "mac", "ffmpeg", "ffmpeg"))
    else:
        return str(Path(SCRIPT_DIR, "linux", "ffmpeg", "ffmpeg"))

song = AudioSegment.from_mp3('test')
play(song)
Enter fullscreen mode Exit fullscreen mode

The program should now be able to run and play the mp3 file. The path added to os environment is temporary and would be gone when the python program is completed.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (2)

Collapse
 
photostockedit1 profile image
Photostockeditor

Great, you can get free mp3 o wav from free-sound-effects.net/

Collapse
 
bitsnaps profile image
Ibrahim.H

Nice, just to mention the play() comes from pydub.playback.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay