From 8dd6925699bc1d01196f84bf76f2c0cee40f89ee Mon Sep 17 00:00:00 2001 From: zhzluke96 Date: Mon, 24 Jun 2024 18:18:49 +0800 Subject: [PATCH] :hammer: add download_audio_backend.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 增加音频后端下载脚本 相关 #68 --- docs/dependencies.md | 25 ++++++- scripts/download_audio_backend.py | 112 ++++++++++++++++++++++++++++++ scripts/download_ffmpeg.py | 1 - 3 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 scripts/download_audio_backend.py delete mode 100644 scripts/download_ffmpeg.py diff --git a/docs/dependencies.md b/docs/dependencies.md index b6af187..aa5e4a9 100644 --- a/docs/dependencies.md +++ b/docs/dependencies.md @@ -83,27 +83,46 @@ 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) 目录应该如下 ``` @@ -111,6 +130,10 @@ apt-get install ffmpeg libavcodec-extra ├── ffmpeg.exe ├── ffprobe.exe ├── ffplay.exe +├── ffplay.exe +├── rubberband.exe +├── rubberband-r3.exe +├── sndfile.dll └── put_ffmpeg_here ``` diff --git a/scripts/download_audio_backend.py b/scripts/download_audio_backend.py new file mode 100644 index 0000000..09a3389 --- /dev/null +++ b/scripts/download_audio_backend.py @@ -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() diff --git a/scripts/download_ffmpeg.py b/scripts/download_ffmpeg.py deleted file mode 100644 index 4640904..0000000 --- a/scripts/download_ffmpeg.py +++ /dev/null @@ -1 +0,0 @@ -# TODO