| import os | |
| import torch | |
| import demucs.separate | |
| import sys | |
| from pathlib import Path | |
| def separate_audio_gpu(input_file, output_dir): | |
| """ | |
| Separates audio into 4 stems using Demucs v4 (HTDemucs) on GPU. | |
| """ | |
| print(f"\n[STAGE 2] Starting Stem Separation (GPU)...") | |
| print(f"Target File: {input_file}") | |
| # Ensure output directory exists | |
| os.makedirs(output_dir, exist_ok=True) | |
| # Device check | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| print(f"Using Device: {device}") | |
| # Demucs arguments | |
| # htdemucs is the latest v4 model | |
| args = [ | |
| "-d", device, | |
| "--out", output_dir, | |
| "-n", "htdemucs", | |
| str(input_file) | |
| ] | |
| try: | |
| demucs.separate.main(args) | |
| print(f"[SUCCESS] Stems saved to: {output_dir}") | |
| return True | |
| except Exception as e: | |
| print(f"[ERROR] Separation failed: {e}") | |
| return False | |
| if __name__ == "__main__": | |
| if len(sys.argv) < 2: | |
| print("Usage: python separate_gpu.py <input_path>") | |
| else: | |
| input_path = sys.argv[1] | |
| out_path = os.path.join(os.getcwd(), "Outputs", "Stems") | |
| separate_audio_gpu(input_path, out_path) | |