Open In App

Create an Audio Editor in Python using PyDub

Last Updated : 02 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Audio editing is a crucial aspect of modern multimedia production, from music production to podcasting and video editing. Python, with its extensive libraries and tools, offers a versatile platform for audio editing tasks. Among these libraries, PyDub stands out as a powerful and user-friendly library for audio manipulation.

In this tutorial, we’ll explore the fundamentals of using PyDub for audio editing. We’ll cover a variety of operations, such as extracting audio from video files, dividing stereo into mono channels, converting formats, applying effects, trimming, concatenating, adjusting volume, and retrieving audio properties.

By the end of this tutorial, you’ll have a solid understanding of how to use PyDub to perform various audio editing tasks, which you can apply to your own projects, whether it’s creating music, editing podcasts, or enhancing video soundtracks.

Prerequisite (Required Module for Audio editing in Python)

1. PyDub library in Python

For Audio editing in Python, PyDub library can be used.

PyDub is a Python library that simplifies the process of working with audio files. It provides a simple and intuitive interface for various audio editing tasks. PyDub simplifies audio editing tasks in Python by providing a straightforward interface for common operations. It’s a powerful tool for working with audio files, whether you’re extracting audio from video files, converting formats, applying effects, or performing other tasks.

Installation of PyDub library:

To install PyDub, use pip:

pip install pydub

2. AudioSegment Class in PyDub library

AudioSegment is a class provided by the PyDub library that represents an audio segment. It is used to load audio files, manipulate audio data, and perform various audio editing operations. AudioSegment class in PyDub provides a convenient and easy-to-use interface for working with audio data in Python.

Basic Operations on PyDub library AudioSegment Class:

We will be using operations often in our Audio Editor:

  • Import the necessary modules:
    from pydub import AudioSegment
  • Load an audio file:
    audio = AudioSegment.from_file("input.wav", format="wav")
  • Export an audio file:
    audio.export("output.mp3", format="mp3")

Creating an Audio Editor in Python using PyDub library AudioSegment Class

Here’s a guide on how to perform common audio editing operations using PyDub:

1. Extracting Audio from Video Files:

  • Install the necessary libraries: pip install pydub moviepy
  • Import the necessary modules: from pydub import AudioSegment and from moviepy.editor import VideoFileClip
  • Load the video file and extract its audio: video = VideoFileClip("input.mp4") and audio = video.audio
  • Export the extracted audio: audio.write_audiofile("output.wav")

Python3




from pydub import AudioSegment
from moviepy.editor import VideoFileClip
 
# Load the video file and extract its audio
video = VideoFileClip("input.mp4")
audio = video.audio
 
# Export the extracted audio
audio.write_audiofile("output.wav")


test

2. Dividing Stereo into Mono Channels:

  • Import the necessary modules: from pydub import AudioSegment
  • Load the stereo audio file: audio = AudioSegment.from_file("input.wav", format="wav")
  • Split the stereo audio into two mono channels: left_channel, right_channel = audio.split_to_mono()

Python3




from pydub import AudioSegment
 
# Load the stereo audio file
audio = AudioSegment.from_file("input.wav", format="wav")
 
# Split the stereo audio into two mono channels
left_channel, right_channel = audio.split_to_mono()


3. Converting Formats:

  • Import the necessary modules: from pydub import AudioSegment
  • Load the audio file: audio = AudioSegment.from_file("input.wav", format="wav")
  • Export the audio to a different format: audio.export("output.mp3", format="mp3")

Python3




from pydub import AudioSegment
 
# Load the audio file
audio = AudioSegment.from_file("input.wav", format="wav")
 
# Export the audio to a different format
audio.export("output.mp3", format="mp3")


4. Applying Effects:

  • Import the necessary modules: from pydub import AudioSegment
  • Load the audio file: audio = AudioSegment.from_file("input.wav", format="wav")
  • Apply an effect to the audio: audio = audio.low_pass_filter(500)

Python3




from pydub import AudioSegment
 
# Load the audio file
audio = AudioSegment.from_file("input.wav", format="wav")
 
# Apply an effect to the audio
audio = audio.low_pass_filter(500)


5. Trimming:

  • Import the necessary modules: from pydub import AudioSegment
  • Load the audio file: audio = AudioSegment.from_file("input.wav", format="wav")
  • Trim the audio to a specific duration: trimmed_audio = audio[1000:5000]

Python3




from pydub import AudioSegment
 
# Load the audio file
audio = AudioSegment.from_file("input.wav", format="wav")
 
# Trim the audio to a specific duration
trimmed_audio = audio[1000:5000]


6. Concatenating:

  • Import the necessary modules: from pydub import AudioSegment
  • Load the audio files: audio1 = AudioSegment.from_file("input1.wav", format="wav") and audio2 = AudioSegment.from_file("input2.wav", format="wav")
  • Concatenate the audio files: concatenated_audio = audio1 + audio2

Python3




from pydub import AudioSegment
 
# Load the audio files
audio1 = AudioSegment.from_file("input1.wav", format="wav")
audio2 = AudioSegment.from_file("input2.wav", format="wav")
 
# Concatenate the audio files
concatenated_audio = audio1 + audio2


7. Adjusting Volume:

  • Import the necessary modules: from pydub import AudioSegment
  • Load the audio file: audio = AudioSegment.from_file("input.wav", format="wav")
  • Increase or decrease the volume of the audio: louder_audio = audio + 10

Python3




from pydub import AudioSegment
 
# Load the audio file
audio = AudioSegment.from_file("input.wav", format="wav")
 
# Increase or decrease the volume of the audio
louder_audio = audio + 10


8. Retrieving Audio Properties:

  • Import the necessary modules: from pydub import AudioSegment
  • Load the audio file: audio = AudioSegment.from_file("input.wav", format="wav")
  • Get the duration, sample rate, and channels of the audio: duration = len(audio), sample_rate = audio.frame_rate, and channels = audio.channels

Python3




from pydub import AudioSegment
 
# Load the audio file
audio = AudioSegment.from_file("input.wav", format="wav")
 
# Get the duration, sample rate, and channels of the audio
duration = len(audio)  # in milliseconds
sample_rate = audio.frame_rate  # in Hz
channels = audio.channels  # 1 for mono, 2 for stereo


Some additional operations:

9. Splitting Stereo to Mono:

Python




from pydub import AudioSegment
 
# Load stereo audio file
stereo_audio = AudioSegment.from_file("stereo_audio.mp3", format="mp3")
 
# Split into mono channels
left_channel = stereo_audio.split_to_mono()[0]
right_channel = stereo_audio.split_to_mono()[1]
 
# Export mono channels
left_channel.export("left_channel.mp3", format="mp3")
right_channel.export("right_channel.mp3", format="mp3")


10. Getting Audio Properties:

Python




from pydub.utils import mediainfo
 
# Get audio properties
audio_info = mediainfo("audio_file.mp3")
 
# Print audio properties
print("Duration:", audio_info["duration"], "seconds")
print("Sample Rate:", audio_info["sample_rate"], "Hz")
print("Channels:", audio_info["channels"])


Create an Audio Editor in Python Using Flask Framework by API:

Installation

pip install flask pydub

Then, create a file called app.py and add the following code:

Python




from flask import Flask, request, send_from_directory, jsonify
from werkzeug.utils import secure_filename
import os
from pydub import AudioSegment
 
app = Flask(__name__)
 
UPLOAD_FOLDER = 'uploads'
OUTPUT_FOLDER = 'outputs'
ALLOWED_EXTENSIONS = {'mp3', 'wav'}
 
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['OUTPUT_FOLDER'] = OUTPUT_FOLDER
 
def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
 
def process_audio(input_path, output_path):
    # Example: Just convert to WAV format
    sound = AudioSegment.from_file(input_path)
    sound.export(output_path, format="wav")
 
@app.route('/upload', methods=['POST'])
def upload_file():
    if 'file' not in request.files:
        return jsonify({'error': 'No file part'})
    file = request.files['file']
    if file.filename == '':
        return jsonify({'error': 'No selected file'})
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        input_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        output_path = os.path.join(app.config['OUTPUT_FOLDER'], filename)
        file.save(input_path)
        process_audio(input_path, output_path)
        return jsonify({'message': 'File uploaded and processed successfully', 'filename': filename})
    return jsonify({'error': 'Invalid file format'})
 
@app.route('/download/<filename>', methods=['GET'])
def download_file(filename):
    return send_from_directory(app.config['OUTPUT_FOLDER'], filename)
 
if __name__ == '__main__':
    app.run(debug=True)


With this setup, clients can use HTTP requests to interact with the API. They can upload audio files using a POST request to /upload endpoint and download processed files using a GET request to /download/<filename> endpoint.

For example, a client can use requests library in Python to interact with the API:

Python




import requests
 
file_path = '/path/to/your/audio/file.mp3'
 
files = {'file': open(file_path, 'rb')}
response = requests.post(upload_url, files=files)
 
if response.status_code == 200:
    print('File uploaded and processed successfully')
    processed_filename = response.json()['filename']
    download_url = f'http://localhost:5000/download/{processed_filename}'
    # Use download_url to download the processed file
else:
    print('Error:', response.json()['error'])


Run the Flask application:

python app.py

Now, you can send a POST request to the /cut endpoint to cut an audio file. Here’s an example using curl:

curl -X POST -F "file=@input.wav" -F "start=10" -F "end=20" http://localhost:5000/cut -o output.wav

This example makes a POST request to the /cut endpoint with an audio file, start second, and finish second. The server executes the request, cuts the audio, stores it temporarily, and returns it as a response. The clipped audio is stored as output.wav.

Conclusion:

PyDub is an essential tool for Python audio editing, providing an easy way to accomplish a lot of different tasks. Developers can create engaging audio content for a variety of applications and streamline their audio processing workflows by mastering the fundamentals of audio editing and utilizing PyDub’s capabilities. PyDub enables users to easily and effectively accomplish their audio editing objectives, whether they involve trimming, concatenating, adjusting volume, applying effects, converting formats, extracting audio from video files, splitting stereo into mono channels, or retrieving audio properties.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads