This script downloads audio from a YouTube video, processes it to detect BPM and key, separates vocals from instrumentals with Demucs, and saves the processed files.
- Downloads audio from a given YouTube URL.
- Keeps the downloaded YouTube audio as the source and decodes it to temporary WAV/PCM for processing.
- Detects Beats Per Minute (BPM) and the musical key of the audio.
- Separates vocals and instrumentals into two separate audio files.
- Outputs WAV files for vocals and instrumentals, named with the YouTube title, stem type, BPM, and key/mode (e.g.,
Fetched_Song_Name_vocals_152bpm_Eminor.wav).
Before running the script, ensure you have Python installed and the following dependencies:
- FFmpeg: Demucs and the temporary WAV conversion require FFmpeg. Ensure it is installed and accessible in your system's PATH.
- You can download it from ffmpeg.org.
- Python Libraries:
pytubefix: For downloading YouTube videos.librosa: For audio analysis (BPM and key detection).demucs: For higher-quality vocal and instrumental separation.soundfile: For writing WAV output without relying on torchaudio's TorchCodec writer.numpy: A dependency for librosa and other numerical operations.
You can install the Python libraries using pip:
pip install -r requirements.txtThe first Demucs run can take a while because it may download the htdemucs_ft model weights.
-
Clone this repository or download the
youtube_audio_processor.pyscript. -
Ensure all prerequisites (FFmpeg and Python libraries) are installed.
-
Open your terminal or command prompt.
-
Navigate to the directory where you saved the script.
-
Run the script using Python:
python youtube_audio_processor.py
-
The script will prompt you to enter a YouTube URL. Paste the URL and press Enter.
-
The script will then perform the following steps:
- Download the best available YouTube audio stream.
- Decode that source to temporary WAV/PCM without adding another lossy MP3 encode.
- Detect BPM and key.
- Separate vocals and instrumentals with Demucs using a temporary
demucs_temp_processing/directory. - Rename and save the final
{song_title}_vocals_{bpm}bpm_{key}{mode}.wavand{song_title}_instrumental_{bpm}bpm_{key}{mode}.wavfiles into theoutput/separated_tracks/directory (relative to the script).
youtube_audio_processor.py: The main Python script.temp_source_audio/: Temporary folder for the initial YouTube audio download. This folder is automatically cleaned up.temp_audio.wav: Temporary WAV/PCM file used for BPM/key detection and vocal separation. This file is automatically cleaned up.demucs_temp_processing/: Temporary directory created during runtime by Demucs to store its intermediate separated.wavfiles. This directory and its contents are automatically cleaned up.output/separated_tracks/: The directory where the final processed audio files are saved.{song_title}_vocals_{bpm}bpm_{key}{mode}.wav: The final vocals-only audio file.{song_title}_instrumental_{bpm}bpm_{key}{mode}.wav: The final instrumental-only audio file.
- The quality of BPM and key detection, as well as vocal separation, can vary depending on the source audio. Higher-quality source files will produce better stems than YouTube audio.
- Key and mode detection is an estimate based on the source audio's chroma profile, so it can be wrong on ambiguous songs, key changes, or noisy mixes.
- The script uses specific itags (140, 251) for audio stream preference from YouTube for better quality, with fallbacks if these are not available.
- Ensure you have a stable internet connection for downloading the YouTube audio.