Pular para o conteúdo

Como Converter Arquivos MP3 para WAV no Python

[

Converting Audio to the Right Format

Introduction

Audio files often require preprocessing before performing any operations on them. In this tutorial, we will guide you through the process of converting audio files from one format to another using Python. Specifically, we will focus on converting MP3 files to WAV format.

Prerequisites

Before we begin, make sure you have the following installed:

  • Python - version 3 or above
  • PyDub library - for manipulating audio files
  • ffmpeg - for working with non-WAV audio files

Steps:

  1. Import the necessary libraries:
from pydub import AudioSegment
  1. Create a helper function convert_to_wav:
def convert_to_wav(filename):
# Load the audio file using PyDub's from_file() method
audio = AudioSegment.from_file(filename)
# Set the export format to "wav"
output_format = "wav"
# Export the audio to the desired format
audio.export(f"{filename}.wav", format=output_format)
print("Audio conversion complete!")
  1. Call the convert_to_wav function with the file path of the MP3 file you want to convert:
convert_to_wav("call_1.mp3")
  1. Run the script and check the directory for the converted file, call_1.mp3.wav.

Explanation

  1. We import the necessary library, AudioSegment, from PyDub to manipulate the audio files.

  2. We define the convert_to_wav function which takes a filename parameter. Inside the function, we:

    • Load the audio file using AudioSegment.from_file().
    • Set the export format to wav using the output_format variable.
    • Export the audio file using audio.export() and save it as filename.wav.
  3. We call the convert_to_wav function with the file path of the MP3 file we want to convert.

  4. The function converts the MP3 file to WAV format and saves it in the same location with the extension .wav.

By following these steps, you can easily convert your MP3 audio files to WAV format using Python and the PyDub library.