Python Audio Separation Libraries allowing you to isolate individual tracks

CK CK 3788 
Created at
Updated at  
1,441 0 0

Python offers a variety of libraries for audio separation, allowing you to isolate individual instruments or vocals from a mixed audio track. Popular choices include Librosa, a powerful audio analysis and manipulation library with functions for source separation using techniques like Non-negative Matrix Factorization (NMF) and Deep Neural Networks (DNNs), and Spleeter, a deep learning-based audio source separation tool that can separate vocals, drums, bass, and other instruments with impressive accuracy. These libraries offer pre-trained models and flexible APIs, enabling you to easily incorporate audio separation into your Python projects for music production, analysis, and more. 
 

Here’s a list of high-quality audio separation libraries in Python, along with examples for each. These libraries are designed to handle audio with intricate vocal and instrumental components, delivering precise separation results.

 

1. Demucs

Demucs, developed by Facebook AI Research, is a state-of-the-art model for music source separation. It uses deep convolutional and recurrent layers, which significantly improve performance for complex mixtures. Demucs is especially good at separating vocals from intricate background music.

Installation

pip install demucs

Example

import subprocess
from moviepy.editor import VideoFileClip, AudioFileClip

def separate_with_demucs(input_audio_path):
    command = f'demucs --two-stems=vocals {input_audio_path}'
    subprocess.call(command, shell=True)

# Step 1: Extract audio from video
video = VideoFileClip("input_video.mp4")
audio_path = "input_audio.wav"
video.audio.write_audiofile(audio_path)

# Step 2: Run Demucs to separate vocals and accompaniment
separate_with_demucs(audio_path)

# Step 3: Load separated vocals and replace in video
vocals_path = 'separated/vocals.wav'  # Demucs output folder
vocals = AudioFileClip(vocals_path)
final_video = video.set_audio(vocals)
final_video.write_videofile("output_video.mp4", codec="libx264", audio_codec="aac")

 

2. X-UMX (Open-Unmix)

Open-Unmix, or X-UMX, is an open-source deep learning-based audio separation library for source separation. This library can separate vocals and accompaniment or even drums and bass if needed. It's easy to customize and works well on audio files with distinct frequency patterns for each source.

Installation

pip install openunmix

 

Example

from openunmix import predict
from moviepy.editor import VideoFileClip, AudioFileClip

# Step 1: Extract audio from video
video = VideoFileClip("input_video.mp4")
audio_path = "input_audio.wav"
video.audio.write_audiofile(audio_path)

# Step 2: Use Open-Unmix to separate vocals
output_path = 'output_vocals.wav'
predict.separate(audio_path, targets=['vocals'], output=output_path)

# Step 3: Replace audio in video with vocal-only track
vocals = AudioFileClip(output_path)
final_video = video.set_audio(vocals)
final_video.write_videofile("output_video.mp4", codec="libx264", audio_codec="aac")

 

3. Spleeter

Spleeter, developed by Deezer, is widely used for quick audio separation. While not as high quality as Demucs for complex tracks, Spleeter remains effective for general use and is known for its ease of installation and operation.

 

Installation

pip install spleeter

Example

from spleeter.separator import Separator
from moviepy.editor import VideoFileClip, AudioFileClip

# Step 1: Extract audio from video
video = VideoFileClip("input_video.mp4")
audio_path = "input_audio.wav"
video.audio.write_audiofile(audio_path)

# Step 2: Use Spleeter to separate vocals
separator = Separator('spleeter:2stems')  # separate into 'vocals' and 'accompaniment'
separator.separate_to_file(audio_path, 'output')

# Step 3: Load separated vocals and replace in video
vocals_path = 'output/input_audio/vocals.wav'  # Spleeter output folder
vocals = AudioFileClip(vocals_path)
final_video = video.set_audio(vocals)
final_video.write_videofile("output_video.mp4", codec="libx264", audio_codec="aac")

 

4. UVR (Ultimate Vocal Remover)

UVR (Ultimate Vocal Remover) is an advanced open-source tool that combines several separation models like Demucs and MDX-Net to provide multiple source separation options. It’s highly flexible, allowing users to experiment with different models and find the best one for their audio.

Installation

  1. Install UVR’s dependencies:

    pip install demucs
    
  2. Download UVR from its GitHub page for more advanced options.

Example (using Demucs through UVR)

Since UVR integrates other models like Demucs, this example shows how to call Demucs via UVR’s shell command:

import subprocess
from moviepy.editor import VideoFileClip, AudioFileClip

# Step 1: Run Demucs through UVR for separation
def separate_with_uvr(input_audio):
    command = f'demucs --two-stems=vocals {input_audio}'
    subprocess.call(command, shell=True)

# Step 2: Process video audio
video = VideoFileClip("input_video.mp4")
audio_path = "input_audio.wav"
video.audio.write_audiofile(audio_path)

# Step 3: Run separation
separate_with_uvr(audio_path)

# Step 4: Load separated vocals and replace in video
vocals_path = 'separated/vocals.wav'  # UVR/Demucs output folder
vocals = AudioFileClip(vocals_path)
final_video = video.set_audio(vocals)
final_video.write_videofile("output_video.mp4", codec="libx264", audio_codec="aac")

 

5. Lalal.ai API (Cloud-Based Service)

Lalal.ai is a high-quality, cloud-based audio separation tool. It’s highly effective at separating vocals and background music even in complex audio. However, it’s a paid service with a REST API for integration.

 

Example (using requests library)

First, sign up on Lalal.ai and get an API key.

import requests
import json
from moviepy.editor import VideoFileClip, AudioFileClip

# Step 1: Upload audio and request separation
def separate_with_lalalai(api_key, input_audio_path):
    url = "https://api.lalal.ai/upload"
    headers = {"Authorization": f"Bearer {api_key}"}
    files = {"file": open(input_audio_path, "rb")}
    response = requests.post(url, headers=headers, files=files)
    data = response.json()
    return data['vocals_url']

# Step 2: Process video
video = VideoFileClip("input_video.mp4")
audio_path = "input_audio.wav"
video.audio.write_audiofile(audio_path)

# Step 3: Separate audio using Lalal.ai API
api_key = "your_api_key_here"
vocal_url = separate_with_lalalai(api_key, audio_path)

# Step 4: Download separated vocals and replace audio in video
vocals = AudioFileClip(vocal_url)
final_video = video.set_audio(vocals)
final_video.write_videofile("output_video.mp4", codec="libx264", audio_codec="aac")

 

Summary

LibraryStrengthsInstallationNotes
DemucsExcellent for complex mixtures, high qualitypip install demucsSlow but high-quality
X-UMXEffective for specific componentspip install openunmixCustomizable but requires tuning
SpleeterQuick and general-purpose separationpip install spleeterFast but not as high-quality
UVRCombines models, highly flexibleUVR + demucs (for models)Can be complex to set up
Lalal.aiHigh-quality cloud service, low setupREST API (online service)Paid, cloud-based

These libraries provide high-quality audio separation solutions depending on your needs, hardware, and willingness to customize. Let me know if you’d like further help with any!

Tags Audio Analysis Audio Manipulation Audio Processing Audio Separation Libraries Deep Learning Audio Demucs Instrument Isolation Lalal.ai Music Production Music Source Separation Open-Unmix Python Audio Processing Python Audio Separation Spleeter UVR Vocal Isolation Facebook X
Comments 0
Similar posts
  1. Deleting a specific user voice from the original audio track with Python
    548
  2. Creating an MR from the original MP3 audio in Python - Deleting Voice Track from Audio File
    438
  3. Creating mp3 audio file based on the text input with Python
    465
  4. Implementing Text To Speech / Audio with Python3 and pyttsx3
    355
  5. Machine Learning Models for Ad Detection from a Video Stream
    188
  1. Uploading a file to a Google Drive Folder programmatically with Python
    3,307
  2. Garbage Collection when your system does not have enough memory in your Python App
    1,011
  3. Method to maximize full CPU performance with Python
    315
  4. How do I get the maximum available of thread in python?
    990
  5. Using Queue Algorithm with Redis - RPUSH and LPOP
    769
  6. Use cache concept with Redis : Let's do Redis programming with Python
    531
  7. Installing Python 3.8 in a venv environment on CentOS 7
    463
  8. CUDA versus OpenCL
    578
  9. Simple gRPC server based on Python
    394
  10. Python code to convert PDF file to Excel Spreadsheet
    699
  11. Python script to list files on your Google Drive and download one of them
    940
  12. Coinbase Python API - Basic Example
    461
  13. AWS Lambda Code Example based on Python
    333
  14. Python code using GPU on CentOS with AWS
    1,251
  15. Video Scence Detection with PySceneDetect
    2,320
  16. Python Version Control for Library Compatibility with pyenv - Downgrading/Upgrading Python Version
    908
  17. Deleting a specific user voice from the original audio track with Python
    548
  18. Creating an MR from the original MP3 audio in Python - Deleting Voice Track from Audio File
    438
  19. Creating mp3 audio file based on the text input with Python
    465
  20. Implementing Text To Speech / Audio with Python3 and pyttsx3
    355
  21. Building Initial Virtual Environment at Python - The first step you should do for Python Programming
    559
  22. Python Simplified Assignment Operators
    533
  23. Trackback module used for Python Threading debugging
    843
  24. Python Flask with simple HTML files to server web pages along with API
    927
  25. Python Flask web server tutorial from the scratch
    622
  26. Simple Web Server based in Python
    371
  27. Thread Implementation in Python
    509
  28. A shell script that activates a Python virtual environment and then runs a specific Python script
    742
  29. Learning about Python Multiprocessing Technique
    592
  30. Simple Example to get the total number of records in BigQuery with Python
    666
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    39
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    34
  3. How do I create animated background for Google Meet?
    20,611
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    244
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    156
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    69
  11. Frequency Cap Architecture Overview
    513
  12. Collecting Proof-of-Play from 100,000 Screens
    145
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    287
  14. What is RedisVL?
    108
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    230
  16. TPU & GPU Cluster Management Tools
    243
  17. The Concept of Scaffolding in AI Agents
    232
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    582
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    258
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    337
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    221
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    220
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    655
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    195
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    216
  30. Understanding Data Lineage
    259