This script downloads audio from a YouTube video, processes it to detect BPM and key, separates vocals from instrumentals, and saves the processed files.
- Downloads audio from a given YouTube URL.
- Converts audio to MP3 format.
- Detects Beats Per Minute (BPM) and the musical key of the audio.
- Separates vocals and instrumentals into two separate audio files.
- Outputs MP3 files for vocals and instrumentals, named with their BPM and key (e.g.,
vocal_120bpm_C.mp3).
Before running the script, ensure you have Python installed and the following dependencies:
- FFmpeg: Spleeter (used for vocal separation) requires 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.pydub: For audio manipulation (conversion to MP3).librosa: For audio analysis (BPM and key detection).spleeter: For vocal and instrumental separation.numpy: A dependency for librosa and other numerical operations.
You can install the Python libraries using pip:
pip install pytubefix pydub librosa spleeter numpy-
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 audio (temporary files are created in the script's root directory and cleaned up).
- Detect BPM and key.
- Separate vocals and instrumentals (Spleeter uses a temporary
spleeter_temp_processing/directory which is also cleaned up). - Rename and save the final
vocal_{bpm}bpm_{key}.mp3andinstrumental_{bpm}bpm_{key}.mp3files into theoutput/separated_tracks/directory (relative to the script).
youtube_audio_processor.py: The main Python script._temp_initial_download.*: A temporary file for the initial audio download from YouTube, created in the script's root directory. This file is automatically cleaned up after conversion to MP3.temp_audio.mp3: A temporary MP3 file created from the initial download, also in the script's root directory. This file is used for BPM/key detection and vocal separation, and is cleaned up afterwards.spleeter_temp_processing/: A temporary directory created during runtime by Spleeter to store its intermediate separated.wavand.mp3files. This directory and its contents are automatically cleaned up.output/separated_tracks/: The directory where the final processed audio files are saved.vocal_{bpm}bpm_{key}.mp3: The final vocals-only audio file.instrumental_{bpm}bpm_{key}.mp3: 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.
- 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.