-
-
Notifications
You must be signed in to change notification settings - Fork 633
/
Copy pathebook2audiobook.cmd
executable file
·248 lines (221 loc) · 6.8 KB
/
ebook2audiobook.cmd
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
@echo off
setlocal enabledelayedexpansion
:: Capture all arguments into ARGS
set "ARGS=%*"
set "NATIVE=native"
set "FULL_DOCKER=full_docker"
set "SCRIPT_MODE=%NATIVE%"
set "SCRIPT_DIR=%~dp0"
set "PYTHON_VERSION=3.12"
set "PYTHON_ENV=python_env"
set "PYTHONUTF8=1"
set "PYTHONIOENCODING=utf-8"
set "CURRENT_ENV="
set /p VERSION=<VERSION.txt
set "PROGRAMS_LIST=calibre-normal-cjk ffmpeg nodejs espeak-ng sox"
set "TMP=%SCRIPT_DIR%\tmp"
set "TEMP=%SCRIPT_DIR%\tmp"
set "ESPEAK_DATA_PATH=%USERPROFILE%\scoop\apps\espeak-ng\current\eSpeak NG\espeak-ng-data"
set "SCOOP_HOME=%USERPROFILE%\scoop"
set "SCOOP_SHIMS=%SCOOP_HOME%\shims"
set "SCOOP_APPS=%SCOOP_HOME%\apps"
set "CONDA_URL=https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Windows-x86_64.exe"
set "CONDA_INSTALL_DIR=%USERPROFILE%\Miniforge3"
set "CONDA_INSTALLER=Miniforge3-Windows-x86_64.exe"
set "CONDA_ENV=%CONDA_INSTALL_DIR%\condabin\conda.bat"
set "CONDA_PATH=%CONDA_INSTALL_DIR%\condabin"
set "NODE_PATH=%SCOOP_HOME%\apps\nodejs\current"
set "PATH=%SCOOP_SHIMS%;%SCOOP_APPS%;%CONDA_PATH%;%NODE_PATH%;%PATH%" 2>&1 >nul
set "SCOOP_CHECK=0"
set "CONDA_CHECK=0"
set "PROGRAMS_CHECK=0"
set "DOCKER_CHECK=0"
set "HELP_FOUND=%ARGS:--help=%"
:: Refresh environment variables (append registry Path to current PATH)
for /f "tokens=2,*" %%A in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path') do (
set "PATH=%%B;%PATH%"
)
cd /d "%SCRIPT_DIR%"
:: Check if running inside Docker
if defined CONTAINER (
set "SCRIPT_MODE=%FULL_DOCKER%"
goto main
)
goto scoop_check
:scoop_check
where /Q scoop
if %errorlevel% neq 0 (
echo Scoop is not installed.
set "SCOOP_CHECK=1"
)
goto conda_check
exit /b
:conda_check
where /Q conda
if %errorlevel% neq 0 (
call rmdir /s /q "%CONDA_INSTALL_DIR%" 2>nul
echo Miniforge3 is not installed.
set "CONDA_CHECK=1"
goto programs_check
)
:: Check if running in a Conda environment
if defined CONDA_DEFAULT_ENV (
set "CURRENT_ENV=%CONDA_PREFIX%"
)
:: Check if running in a Python virtual environment
if defined VIRTUAL_ENV (
set "CURRENT_ENV=%VIRTUAL_ENV%"
)
for /f "delims=" %%i in ('where /Q python') do (
if defined CONDA_PREFIX (
if /i "%%i"=="%CONDA_PREFIX%\Scripts\python.exe" (
set "CURRENT_ENV=%CONDA_PREFIX%"
break
)
) else if defined VIRTUAL_ENV (
if /i "%%i"=="%VIRTUAL_ENV%\Scripts\python.exe" (
set "CURRENT_ENV=%VIRTUAL_ENV%"
break
)
)
)
if not "%CURRENT_ENV%"=="" (
echo Current python virtual environment detected: %CURRENT_ENV%.
echo This script runs with its own virtual env and must be out of any other virtual environment when it's launched.
goto failed
)
goto programs_check
exit /b
:programs_check
set "missing_prog_array="
for %%p in (%PROGRAMS_LIST%) do (
set "prog=%%p"
if "%%p"=="nodejs" set "prog=node"
if "%%p"=="calibre-normal-cjk" set "prog=calibre"
where /Q !prog!
if !errorlevel! neq 0 (
echo %%p is not installed.
set "missing_prog_array=!missing_prog_array! %%p"
)
)
if not "%missing_prog_array%"=="" (
set "PROGRAMS_CHECK=1"
goto install_components
)
goto dispatch
exit /b
:install_components
:: Install Scoop if not already installed
if not "%SCOOP_CHECK%"=="0" (
echo Installing Scoop...
call powershell -command "Set-ExecutionPolicy RemoteSigned -scope CurrentUser"
call powershell -command "iwr -useb get.scoop.sh | iex"
where /Q scoop
if !errorlevel! neq 0 (
echo Scoop installation failed.
goto failed
)
call scoop install git
call scoop bucket add muggle https://github.com/hu3rror/scoop-muggle.git
call scoop bucket add extras
call scoop bucket add versions
echo Scoop installed successfully.
if "%PROGRAMS_CHECK%"=="0" (
set "SCOOP_CHECK=0"
)
)
:: Install missing packages one by one
if not "%PROGRAMS_CHECK%"=="0" (
echo Installing missing programs...
if "%SCOOP_CHECK%"=="0" (
call scoop bucket add muggle b https://github.com/hu3rror/scoop-muggle.git
call scoop bucket add extras
call scoop bucket add versions
)
for %%p in (%missing_prog_array%) do (
call scoop install %%p
set "prog=%%p"
if "%%p"=="nodejs" (
set "prog=node"
)
if "%%p"=="calibre-normal-cjk" set "prog=calibre"
where /Q !prog!
if !errorlevel! neq 0 (
echo %%p installation failed...
goto failed
)
)
call powershell -command "[System.Environment]::SetEnvironmentVariable('Path', [System.Environment]::GetEnvironmentVariable('Path', 'User') + '%SCOOP_SHIMS%;%SCOOP_APPS%;%CONDA_PATH%;%NODE_PATH%;', 'User')"
set "SCOOP_CHECK=0"
set "PROGRAMS_CHECK=0"
set "missing_prog_array="
)
:: Install Conda if not already installed
if not "%CONDA_CHECK%"=="0" (
echo Installing Miniforge...
call powershell -Command "Invoke-WebRequest -Uri %CONDA_URL% -OutFile "%CONDA_INSTALLER%"
call start /wait "" "%CONDA_INSTALLER%" /InstallationType=JustMe /RegisterPython=0 /S /D=%UserProfile%\Miniforge3
where /Q conda
if !errorlevel! neq 0 (
echo Conda installation failed.
goto failed
)
call conda config --set auto_activate_base false
call conda update conda -y
del "%CONDA_INSTALLER%"
set "CONDA_CHECK=0"
echo Conda installed successfully.
)
goto dispatch
exit /b
:dispatch
if "%SCOOP_CHECK%"=="0" (
if "%PROGRAMS_CHECK%"=="0" (
if "%CONDA_CHECK%"=="0" (
if "%DOCKER_CHECK%"=="0" (
goto main
) else (
goto failed
)
)
)
)
echo PROGRAMS_CHECK: %PROGRAMS_CHECK%
echo CONDA_CHECK: %CONDA_CHECK%
echo DOCKER_CHECK: %DOCKER_CHECK%
goto install_components
exit /b
:main
echo v%VERSION% %SCRIPT_MODE% mode
:: Prevent coqui-tts "dot" bug
fc ".\patches\tokenizer.py" ".\python_env\Lib\site-packages\TTS\tts\layers\xtts\tokenizer.py" >nul
if %errorlevel% neq 0 (
call copy /Y ".\patches\tokenizer.py" ".\python_env\Lib\site-packages\TTS\tts\layers\xtts\"
)
if "%SCRIPT_MODE%"=="%FULL_DOCKER%" (
call python %SCRIPT_DIR%\app.py --script_mode %SCRIPT_MODE% %ARGS%
) else (
if not exist "%SCRIPT_DIR%\%PYTHON_ENV%" (
call conda create --prefix "%SCRIPT_DIR%\%PYTHON_ENV%" python=%PYTHON_VERSION% -y
call %CONDA_ENV% activate base
call conda activate "%SCRIPT_DIR%\%PYTHON_ENV%"
call python -m pip cache purge
call python -m pip install --upgrade pip
for /f "usebackq delims=" %%p in ("requirements.txt") do (
echo Installing %%p...
call python -m pip install --upgrade --no-cache-dir --progress-bar=on "%%p"
)
echo All required packages are installed.
) else (
call %CONDA_ENV% activate base
call conda activate "%SCRIPT_DIR%\%PYTHON_ENV%"
)
call python "%SCRIPT_DIR%\app.py" --script_mode %SCRIPT_MODE% %ARGS%
call conda deactivate
)
exit /b
:failed
echo ebook2audiobook is not correctly installed or run.
exit /b
endlocal
pause