Skip to content

Commit

Permalink
🔨 add download_audio_backend.py
Browse files Browse the repository at this point in the history
- 增加音频后端下载脚本

相关 #68
  • Loading branch information
zhzLuke96 committed Jun 24, 2024
1 parent 4ead989 commit 8dd6925
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 2 deletions.
25 changes: 24 additions & 1 deletion docs/dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,34 +83,57 @@ git clone https://github.com/lenML/ChatTTS-Forge.git --depth=1
- **ffmpeg** 或 **libav**(推荐使用 ffmpeg)
### 安装 ffmpeg
### 脚本安装
执行此脚本即可自动安装后音频后端
```
python -m scripts.download_audio_backend
```
### 手动安装
若安装脚本失效,可参考下面的指南自行手动安装
**Mac(使用 [Homebrew](http://brew.sh))**:
```bash
brew install ffmpeg
brew install rubberband
```

**Linux(使用 aptitude)**:

```bash
apt-get install ffmpeg libavcodec-extra
apt-get install rubberband-cli
```

**Windows**:

> 若你已经安装有 ffmpeg,并可以在命令行中调用,那么无需进行下面的下载和安装
下载 ffmpeg

1.[此处](https://www.gyan.dev/ffmpeg/builds/ffmpeg-git-full.7z)下载并解压 ffmpeg 的 Windows 二进制文件。
2. 将 ffmpeg 的`/bin`文件夹中的 .exe 文件解压到 `项目目录/ffmpeg` 文件夹内

下载 rubberband

1.[此处](https://breakfastquay.com/files/releases/rubberband-3.3.0-gpl-executable-windows.zip)下载并解压 rubberband 的 Windows 二进制文件。
2. 将 压缩包中 `rubberband-3.3.0-gpl-executable-windows` 文件夹下的 .exe/.dll 文件解压到 `项目目录/ffmpeg` 文件夹内

文件 (window) 目录应该如下

```
./ffmpeg
├── ffmpeg.exe
├── ffprobe.exe
├── ffplay.exe
├── ffplay.exe
├── rubberband.exe
├── rubberband-r3.exe
├── sndfile.dll
└── put_ffmpeg_here
```

Expand Down
112 changes: 112 additions & 0 deletions scripts/download_audio_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import os
import shutil
import urllib.request
import zipfile
from tqdm import tqdm
from pathlib import Path
import tempfile
from modules.utils.constants import ROOT_DIR

BIN_DIR_PATH = Path("./ffmpeg")

if not os.path.exists(BIN_DIR_PATH):
os.makedirs(BIN_DIR_PATH)

ffmpeg_path = os.path.join(ROOT_DIR, "ffmpeg")
os.environ["PATH"] = ffmpeg_path + os.pathsep + os.environ["PATH"]

ffmpeg_installed = shutil.which("ffmpeg") is not None
rubberband_installed = shutil.which("rubberband") is not None


class Downloader:
def __init__(self):
self.temp_dir = tempfile.TemporaryDirectory()

def download_and_extract(self, url, extract_path):
zip_path = os.path.join(self.temp_dir.name, url.split("/")[-1])
with DownloadProgressBar(
unit="B", unit_scale=True, miniters=1, desc=url.split("/")[-1]
) as t:
urllib.request.urlretrieve(url, zip_path, reporthook=t.update_to)

with zipfile.ZipFile(zip_path, "r") as zip_ref:
zip_ref.extractall(extract_path)

def install_ffmpeg_on_windows(self):
print("windows系统,安装ffmpeg...")

if not ffmpeg_installed:
ffmpeg_url = "https://www.gyan.dev/ffmpeg/builds/ffmpeg-git-full.7z"
self.download_and_extract(ffmpeg_url, self.temp_dir.name)

if not rubberband_installed:
rubberband_url = "https://breakfastquay.com/files/releases/rubberband-3.3.0-gpl-executable-windows.zip"
self.download_and_extract(rubberband_url, self.temp_dir.name)

need_files = [
"ffmpeg.exe",
"ffplay.exe",
"ffprobe.exe",
"rubberband-r3.exe",
"rubberband.exe",
"sndfile.dll",
]

# 遍历查找,移动到 BIN_DIR_PATH
for root, dirs, files in os.walk(self.temp_dir.name):
for file in files:
if file in need_files:
shutil.move(os.path.join(root, file), BIN_DIR_PATH)

print("安装完成.")

def install_ffmpeg_on_mac(self):
if shutil.which("brew") is None:
print("请先安装brew.")
return
if not ffmpeg_installed:
print("安装ffmpeg...")
os.system("brew install ffmpeg -y")
else:
print("ffmpeg已安装.")
if not rubberband_installed:
print("安装rubberband...")
os.system("brew install rubberband -y")
else:
print("rubberband已安装.")
print("安装完成.")

def install_ffmpeg_on_linux(self):
if shutil.which("apt-get") is None:
print("请先安装apt-get.")
return
if not ffmpeg_installed:
print("安装ffmpeg...")
os.system("apt-get install ffmpeg libavcodec-extra -y")
if not rubberband_installed:
print("安装rubberband...")
os.system("apt-get install rubberband-cli -y")
print("安装完成.")

def __del__(self):
self.temp_dir.cleanup()


class DownloadProgressBar(tqdm):
def update_to(self, b=1, bsize=1, tsize=None):
if tsize is not None:
self.total = tsize
self.update(b * bsize - self.n)


if __name__ == "__main__":
downloader = Downloader()

if os.name == "posix":
if os.uname().sysname == "Darwin":
downloader.install_ffmpeg_on_mac()
elif os.uname().sysname == "Linux":
downloader.install_ffmpeg_on_linux()
elif os.name == "nt":
downloader.install_ffmpeg_on_windows()
1 change: 0 additions & 1 deletion scripts/download_ffmpeg.py

This file was deleted.

0 comments on commit 8dd6925

Please sign in to comment.