File size: 1,193 Bytes
95a9a89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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)