Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

utube2stems

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.

Features

  • 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).

Prerequisites

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.
  • 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.txt

The first Demucs run can take a while because it may download the htdemucs_ft model weights.

How to Run

  1. Clone this repository or download the youtube_audio_processor.py script.

  2. Ensure all prerequisites (FFmpeg and Python libraries) are installed.

  3. Open your terminal or command prompt.

  4. Navigate to the directory where you saved the script.

  5. Run the script using Python:

    python youtube_audio_processor.py
  6. The script will prompt you to enter a YouTube URL. Paste the URL and press Enter.

  7. 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}.wav and {song_title}_instrumental_{bpm}bpm_{key}{mode}.wav files into the output/separated_tracks/ directory (relative to the script).

File Structure

  • 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 .wav files. 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.

Notes

  • 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.

About

Paste a YouTube link and get seperated vocal and instrumental files, labeled with BPM & key detection

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Used by

Contributors

Languages