HOWTO · Python

How to Convert MP3 to WAV in Python

Convert an MP3 file to WAV with pydub or ffmpeg from Python, then verify the output.

On this page

To convert an MP3 file to WAV in Python, use pydub for a compact library interface or call ffmpeg when you want direct control over command-line options. Both approaches require the ffmpeg executable to be available. WAV is usually uncompressed and can be much larger than MP3; converting a lossy MP3 does not restore audio detail discarded during compression.

Convert MP3 to WAV with pydub

Install pydub in the environment that runs your script with python -m pip install pydub. You also need the ffmpeg executable installed and available on your PATH; installing pydub alone does not provide it. See the ffmpeg guide for background. Installation steps vary by operating system, so use the package manager or installation method supported by your system.

Use input.mp3 as the source and test.wav as the destination in this example. Replace those paths with your own files. The code checks for the input before importing pydub, so it can be run as a template; when the input exists, it loads the MP3 and exports a WAV file.

from pathlib import Path

source_path = Path("input.mp3")
destination_path = Path("test.wav")

if not source_path.is_file():
    print(f"Place an MP3 file at {source_path} and run again.")
else:
    from pydub import AudioSegment

    audio = AudioSegment.from_mp3(str(source_path))
    audio.export(str(destination_path), format="wav")
    print(f"Created {destination_path}")

Run ffmpeg directly to convert MP3 to WAV

You can run ffmpeg in a terminal without a Python audio library. Make sure ffmpeg is installed and callable by name, or use its full executable path. The -i argument names the input and the final argument is the output path: ffmpeg -i input.mp3 test.wav.

This command assumes input.mp3 is in the current directory and writes test.wav, matching the Python examples below. If the input is elsewhere, use its correct path. ffmpeg may ask before overwriting an existing destination; choose a new name or use its overwrite option only when replacing that file is intended.

Use subprocess to convert MP3 to WAV in Python

In a Python script, subprocess.run() can pass an argument list to the external ffmpeg program. This avoids assembling a shell command string and lets Python detect a nonzero process status. The example uses the same input.mp3 source and test.wav destination as the other methods; replace both paths for your files. It checks that the source exists before calling ffmpeg.

from pathlib import Path
import subprocess

source_path = Path("input.mp3")
destination_path = Path("test.wav")

if not source_path.is_file():
    print(f"Place an MP3 file at {source_path} and run again.")
else:
    subprocess.run(
        ["ffmpeg", "-i", str(source_path), str(destination_path)],
        check=True,
    )
    print(f"Created {destination_path}")

With check=True, Python reports a failed conversion instead of silently continuing. ffmpeg writes the destination audio file; subprocess does not redirect its standard output into that WAV file. For batch jobs, catch subprocess.CalledProcessError and record the source path and error details so a failed file can be diagnosed.

Verify the WAV output and troubleshoot common problems

All three conversion examples use input.mp3 as the source and test.wav as the destination. If you changed the destination in a conversion example, change destination_path below to that exact path. The check first confirms the file exists, then opens it with Python’s wave module to confirm it has a readable WAV container. This check is separate from the synthetic WAV fixture that follows; that fixture tests WAV inspection and does not perform an MP3 conversion.

from pathlib import Path
import wave

# Match this path to the destination used by the conversion example.
destination_path = Path("test.wav")

if not destination_path.is_file():
    print(f"WAV file not found: {destination_path}")
else:
    try:
        with wave.open(str(destination_path), "rb") as wav_file:
            print(
                f"WAV: {wav_file.getnchannels()} channel(s), "
                f"{wav_file.getframerate()} Hz"
            )
    except (OSError, wave.Error) as error:
        print(f"Could not read WAV file: {error}")

The following self-contained example creates a small WAV fixture using Python’s standard library and checks its basic metadata. It demonstrates WAV inspection only; it does not decode an MP3.

from pathlib import Path
import tempfile
import wave

with tempfile.TemporaryDirectory() as directory:
    output_path = Path(directory) / "test.wav"
    with wave.open(str(output_path), "wb") as wav_file:
        wav_file.setnchannels(1)
        wav_file.setsampwidth(2)
        wav_file.setframerate(8000)
        wav_file.writeframes(b"\x00\x00" * 800)

    with wave.open(str(output_path), "rb") as wav_file:
        print(f"WAV: {wav_file.getnchannels()} channel(s), {wav_file.getframerate()} Hz")

Output:

WAV: 1 channel(s), 8000 Hz

The printed values describe the WAV stream, not whether its sound quality is better than the MP3 source. If Python cannot find ffmpeg, install the executable or add its directory to PATH, then restart the terminal or process that runs Python. If ffmpeg cannot open the input, confirm the filename and source path relative to the script’s working directory. If conversion fails, inspect ffmpeg’s diagnostic text and check that the input is readable and the destination directory is writable. Choose pydub when its simpler API suits the script; use ffmpeg through subprocess when direct access to its options and process status is useful.