> For the complete documentation index, see [llms.txt](https://docs.clore.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clore.ai/guides/guides_v2-zh/yin-pin-yu-yu-yin/demucs-separation.md).

# Demucs 分离

使用 Demucs 将音乐分离为人声、鼓、贝斯等

使用 Demucs 将音乐分离为各个音轨（人声、鼓、贝斯、其他）。

{% hint style="success" %}
所有示例都可以在通过以下方式租用的 GPU 服务器上运行 [CLORE.AI 市场](https://clore.ai/marketplace).
{% endhint %}

## 在 CLORE.AI 上租用

1. 访问 [CLORE.AI 市场](https://clore.ai/marketplace)
2. 按 GPU 类型、VRAM 和价格筛选
3. 选择 **按需** （固定费率）或 **竞价** （出价）
4. 配置你的订单：
   * 选择 Docker 镜像
   * 设置端口（SSH 用 TCP，Web UI 用 HTTP）
   * 如有需要，添加环境变量
   * 输入启动命令
5. 选择支付方式： **CLORE**, **BTC**，或 **USDT/USDC**
6. 创建订单并等待部署

### 访问你的服务器

* 在以下位置查找连接信息 **我的订单**
* Web 界面：使用 HTTP 端口 URL
* SSH： `ssh -p <port> root@<proxy-address>`

## 什么是 Demucs？

Meta AI 的 Demucs 可以：

* 将人声与音乐分离
* 提取鼓、贝斯和其他乐器
* 处理任意音频格式
* 高质量音轨提取

## 快速部署

**Docker 镜像：**

```
pytorch/pytorch:2.11.0-cuda12.8-cudnn9-runtime
```

**端口：**

```
22/tcp
7860/http
```

**命令：**

```bash
pip install demucs gradio && \
python -c "
import gradio as gr
from demucs.pretrained import get_model
from demucs.apply import apply_model
import torch
import torchaudio
import tempfile
import os

model = get_model('htdemucs')
model.cuda()

def separate(audio_path, stem):
    wav, sr = torchaudio.load(audio_path)
    wav = wav.cuda()

    with torch.no_grad():
        sources = apply_model(model, wav.unsqueeze(0), split=True)[0]

    stems = {'鼓': 0, '贝斯': 1, '其他': 2, '人声': 3}
    output = sources[stems[stem]].cpu()

    with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
        torchaudio.save(f.name, output, sr)
        return f.name

demo = gr.Interface(
    fn=separate,
    inputs=[gr.Audio(type='filepath'), gr.Dropdown(['人声', '鼓', '贝斯', '其他'])],
    outputs=gr.Audio(),
    title='Demucs 音频分离器'
)
demo.launch(server_name='0.0.0.0', server_port=7860)
"
```

## 访问你的服务

部署后，找到你的 `http_pub` URL 在 **我的订单**:

1. 前往 **我的订单** 页面
2. 点击你的订单
3. 找到 `http_pub` URL（例如， `abc123.clorecloud.net`)

使用 `https://YOUR_HTTP_PUB_URL` 替代 `localhost` 在下面的示例中。

## 安装

```bash
pip install demucs

# 或
pip install -e git+https://github.com/facebookresearch/demucs#egg=demucs
```

## 命令行用法

### 基础分离

```bash

# 分离为 4 个音轨
demucs song.mp3

# 输出：separated/htdemucs/song/{drums,bass,other,vocals}.wav
```

### 选项

```bash
demucs \
    --two-stems vocals \     # 仅人声 + 伴奏
    -n htdemucs \            # 模型名称
    -d cuda \                # 使用 GPU
    -o ./output \            # 输出目录
    --mp3 \                  # 以 MP3 输出
    song.mp3
```

### 处理文件夹

```bash
demucs --two-stems vocals -d cuda ./songs/*.mp3
```

## Python API

### 基础分离

```python
from demucs.pretrained import get_model
from demucs.apply import apply_model
import torchaudio
import torch

# 加载模型
model = get_model('htdemucs')
model.cuda()
model.eval()

# 加载音频
wav, sr = torchaudio.load("song.mp3")
wav = wav.cuda()

# 分离
with torch.no_grad():
    sources = apply_model(model, wav.unsqueeze(0), split=True)[0]

# sources 形状：[4, channels, samples]

# 0：鼓，1：贝斯，2：其他，3：人声

# 保存音轨
stems = ['鼓', '贝斯', '其他', '人声']
for i, stem in enumerate(stems):
    torchaudio.save(f"{stem}.wav", sources[i].cpu(), sr)
```

### 仅获取人声

```python
def extract_vocals(audio_path):
    wav, sr = torchaudio.load(audio_path)
    wav = wav.cuda()

    with torch.no_grad():
        sources = apply_model(model, wav.unsqueeze(0), split=True)[0]

    vocals = sources[3].cpu()  # 索引 3 = 人声
    return vocals, sr

vocals, sr = extract_vocals("song.mp3")
torchaudio.save("vocals.wav", vocals, sr)
```

### 获取伴奏（无人声）

```python
def extract_instrumental(audio_path):
    wav, sr = torchaudio.load(audio_path)
    wav = wav.cuda()

    with torch.no_grad():
        sources = apply_model(model, wav.unsqueeze(0), split=True)[0]

    # 合并非人声音轨
    instrumental = sources[0] + sources[1] + sources[2]
    return instrumental.cpu(), sr

instrumental, sr = extract_instrumental("song.mp3")
torchaudio.save("instrumental.wav", instrumental, sr)
```

## 模型变体

| 模型           | 音轨 | 质量    | 速度 |
| ------------ | -- | ----- | -- |
| htdemucs     | 4  | 最佳    | 中等 |
| htdemucs\_ft | 4  | Best+ | 慢  |
| htdemucs\_6s | 6  | 很高    | 中等 |
| mdx\_extra   | 4  | 很高    | 快  |

### 6 音轨模型

```python
model = get_model('htdemucs_6s')

# 音轨：鼓、贝斯、其他、人声、吉他、钢琴
```

### 微调模型

```python
model = get_model('htdemucs_ft')

# 质量更高，但更慢
```

## 批量处理

```python
import os
from demucs.pretrained import get_model
from demucs.apply import apply_model
import torchaudio
import torch

model = get_model('htdemucs')
model.cuda()
model.eval()

input_dir = "./songs"
output_dir = "./separated"

for filename in os.listdir(input_dir):
    if filename.endswith(('.mp3', '.wav', '.flac')):
        input_path = os.path.join(input_dir, filename)
        song_output_dir = os.path.join(output_dir, filename.rsplit('.', 1)[0])
        os.makedirs(song_output_dir, exist_ok=True)

        print(f"正在处理：{filename}")

        wav, sr = torchaudio.load(input_path)
        wav = wav.cuda()

        with torch.no_grad():
            sources = apply_model(model, wav.unsqueeze(0), split=True)[0]

        stems = ['鼓', '贝斯', '其他', '人声']
        for i, stem in enumerate(stems):
            torchaudio.save(
                os.path.join(song_output_dir, f"{stem}.wav"),
                sources[i].cpu(),
                sr
            )

        print(f"已保存：{song_output_dir}")
```

## API 服务器

```python
from fastapi import FastAPI, UploadFile
from fastapi.responses import FileResponse
from demucs.pretrained import get_model
from demucs.apply import apply_model
import torchaudio
import torch
import tempfile
import os

app = FastAPI()

model = get_model('htdemucs')
model.cuda()
model.eval()

@app.post("/separate")
async def separate(file: UploadFile, stem: str = "vocals"):
    # 保存上传的文件
    with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
        content = await file.read()
        tmp.write(content)
        tmp_path = tmp.name

    # 加载并分离
    wav, sr = torchaudio.load(tmp_path)
    wav = wav.cuda()

    with torch.no_grad():
        sources = apply_model(model, wav.unsqueeze(0), split=True)[0]

    stems = {'鼓': 0, '贝斯': 1, '其他': 2, '人声': 3}
    output = sources[stems[stem]].cpu()

    # 保存输出
    with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as out:
        torchaudio.save(out.name, output, sr)
        return FileResponse(out.name, media_type="audio/wav")

@app.post("/instrumental")
async def get_instrumental(file: UploadFile):
    with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
        content = await file.read()
        tmp.write(content)
        tmp_path = tmp.name

    wav, sr = torchaudio.load(tmp_path)
    wav = wav.cuda()

    with torch.no_grad():
        sources = apply_model(model, wav.unsqueeze(0), split=True)[0]

    # 合并非人声音轨
    instrumental = sources[0] + sources[1] + sources[2]

    with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as out:
        torchaudio.save(out.name, instrumental.cpu(), sr)
        return FileResponse(out.name, media_type="audio/wav")

# 运行：uvicorn server:app --host 0.0.0.0 --port 8000
```

## 内存优化

### 针对长音频

```python
from demucs.apply import apply_model

# 对长音频使用分块处理
sources = apply_model(
    model,
    wav.unsqueeze(0),
    split=True,         # 分成块
    overlap=0.25,       # 块之间的重叠
    progress=True
)[0]
```

### 适用于有限 VRAM

```python

# 对某些操作使用 CPU
model.cpu()
wav = wav.cpu()

# 或使用片段处理
sources = apply_model(
    model,
    wav.unsqueeze(0),
    split=True,
    segment=10  # 10 秒片段
)[0]
```

## 应用场景

### 卡拉 OK 音轨

```python
def create_karaoke(song_path):
    wav, sr = torchaudio.load(song_path)
    wav = wav.cuda()

    with torch.no_grad():
        sources = apply_model(model, wav.unsqueeze(0), split=True)[0]

    # 除人声外的所有内容
    karaoke = sources[0] + sources[1] + sources[2]
    return karaoke.cpu(), sr
```

### 混音准备

```python
def extract_all_stems(song_path, output_dir):
    wav, sr = torchaudio.load(song_path)
    wav = wav.cuda()

    with torch.no_grad():
        sources = apply_model(model, wav.unsqueeze(0), split=True)[0]

    stems = ['鼓', '贝斯', '其他', '人声']
    paths = {}

    for i, stem in enumerate(stems):
        path = os.path.join(output_dir, f"{stem}.wav")
        torchaudio.save(path, sources[i].cpu(), sr)
        paths[stem] = path

    return paths
```

### 清唱提取

```python
def extract_acapella(song_path):
    wav, sr = torchaudio.load(song_path)
    wav = wav.cuda()

    with torch.no_grad():
        sources = apply_model(model, wav.unsqueeze(0), split=True)[0]

    vocals = sources[3]
    return vocals.cpu(), sr
```

## 质量提示

### 为获得最佳效果

* 使用无损输入（WAV、FLAC）
* 更高的采样率 = 更好的质量
* 使用 `htdemucs_ft` 适用于关键工作

### 后处理

```python
from pydub import AudioSegment
from pydub.effects import normalize, high_pass_filter

# 加载分离出的人声音轨
vocals = AudioSegment.from_wav("vocals.wav")

# 去除低频隆隆声
vocals = high_pass_filter(vocals, 80)

# 归一化
vocals = normalize(vocals)

vocals.export("vocals_clean.wav", format="wav")
```

## 性能

| 音频时长   | GPU      | 时间     |
| ------ | -------- | ------ |
| 3 分钟歌曲 | RTX 3090 | \~15s  |
| 3 分钟歌曲 | RTX 4090 | \~10 秒 |
| 3 分钟歌曲 | A100     | \~8s   |
| 1 小时专辑 | RTX 3090 | \~5 分钟 |

## 故障排查

### 内存不足

```bash

# 使用更小的片段
demucs --segment 10 song.mp3
```

### 分离效果不佳

* 使用 htdemucs\_ft 模型
* 检查输入质量
* 避免高压缩率的 MP3

### 伪影

* 增加重叠
* 使用更高质量的模型
* 检查输入中是否有削波

## 成本估算

CLORE.AI 市场常见费率（截至 2024 年）：

| GPU       | 小时费率    | 日费率     | 4 小时会话  |
| --------- | ------- | ------- | ------- |
| RTX 3060  | \~$0.03 | \~$0.70 | \~$0.12 |
| RTX 3090  | \~$0.06 | \~$1.50 | \~$0.25 |
| RTX 4090  | \~$0.10 | \~$2.30 | \~$0.40 |
| A100 40GB | \~$0.17 | \~$4.00 | \~$0.70 |
| A100 80GB | \~$0.25 | \~$6.00 | \~$1.00 |

*价格因提供商和需求而异。请查看* [*CLORE.AI 市场*](https://clore.ai/marketplace) *以获取当前费率。*

**节省费用：**

* 使用 **竞价** 可中断工作市场——约三分之一的服务器将现货价格定得低于按需价格（中位数约优惠 13%），其余则与按需价格持平
* 使用 **CLORE** 代币支付
* 比较不同提供商的价格

## 下一步

* [RVC 变声克隆](/guides/guides_v2-zh/yin-pin-yu-yu-yin/rvc-voice-clone.md) - 处理提取的人声
* [AudioCraft 音乐](/guides/guides_v2-zh/yin-pin-yu-yu-yin/audiocraft-music.md) - 生成新音乐
* [Whisper Transcription](/guides/guides_v2-zh/yin-pin-yu-yu-yin/whisper-transcription.md) - 转录人声


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/yin-pin-yu-yu-yin/demucs-separation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
