@echo off REM ========================================== REM Douyin Web GUI Launcher (Stable Version) REM ========================================== REM Save original code page for /f "tokens=2 delims=:" %%a in ('chcp') do set "_ORIG_CP=%%a" set "_ORIG_CP=%_ORIG_CP: =%" chcp 65001 >nul 2>&1 setlocal enabledelayedexpansion REM Change to script directory cd /d "%~dp0" if errorlevel 1 ( echo [ERROR] Cannot change to script directory pause goto :cleanup ) REM Save current directory to variable (for delayed expansion) set "WORK_DIR=%~dp0" set "WORK_DIR=!WORK_DIR:~0,-1!" REM Create log file set "LOG_FILE=%~dp0webgui_startup.log" ( echo ======================================== echo Start time: %date% %time% echo ======================================== ) >> "%LOG_FILE%" 2>&1 echo. echo ======================================== echo Douyin Web GUI Launcher echo ======================================== echo. REM [1/6] Check Python environment echo [1/6] Checking Python environment... python --version >> "%LOG_FILE%" 2>&1 if errorlevel 1 ( echo [ERROR] Python not detected, please install Python first echo Download: https://www.python.org/downloads/ echo [ERROR] Python not installed >> "%LOG_FILE%" pause goto :cleanup ) echo [1/6] Python environment check... OK echo. REM [2/6] Check required files echo [2/6] Checking required files... if not exist "web_gui.py" ( echo [ERROR] web_gui.py not found echo Current directory: !WORK_DIR! echo [ERROR] web_gui.py does not exist >> "%LOG_FILE%" pause goto :cleanup ) echo [2/6] File check... OK echo. REM [3/6] Stop old processes echo [3/6] Stopping old processes... REM Silently stop old processes, redirect all output to nul to avoid garbled text powershell -NoProfile -ExecutionPolicy Bypass -Command "$ErrorActionPreference='SilentlyContinue'; Get-CimInstance Win32_Process -ErrorAction SilentlyContinue | Where-Object {$_.CommandLine -like '*web_gui.py*' -and $_.Name -like 'python*'} | ForEach-Object {Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue}" >nul 2>&1 timeout /t 2 /nobreak >nul echo [3/6] Stopping old processes... OK echo. REM [4/6] Port configuration set "WEB_PORT=8000" if defined PORT set "WEB_PORT=!PORT!" if defined WEB_GUI_PORT set "WEB_PORT=!WEB_GUI_PORT!" echo [4/6] Port configuration: !WEB_PORT! echo. REM [5/6] Start service echo [5/6] Starting Web GUI service (Port: !WEB_PORT!)... echo. REM Set Python environment variables set PYTHONIOENCODING=utf-8 set PYTHONUNBUFFERED=1 REM Determine Python executable path set "PYTHON_EXE=" set "USE_VENV=0" REM Prefer virtual environment Python if exist ".venv\Scripts\python.exe" ( set "PYTHON_EXE=.venv\Scripts\python.exe" set "USE_VENV=1" echo Virtual environment detected, using venv Python ) else if exist ".venv\Scripts\activate.bat" ( REM If python.exe not found, try using activate.bat set "USE_VENV=2" echo Virtual environment detected, using activate.bat ) else ( set "PYTHON_EXE=python" set "USE_VENV=0" echo Using system Python ) REM Start service (in new window, keep window open) if !USE_VENV! equ 1 ( REM Use virtual environment python.exe directly (use full path) set "PYTHON_CMD=!WORK_DIR!\.venv\Scripts\python.exe web_gui.py" ) else if !USE_VENV! equ 2 ( REM Use activate.bat set "PYTHON_CMD=call !WORK_DIR!\.venv\Scripts\activate.bat && python web_gui.py" ) else ( REM Use system Python set "PYTHON_CMD=python web_gui.py" ) REM Start command with proper quoting start "Douyin Web GUI" /D "!WORK_DIR!" cmd /k "cd /d !WORK_DIR! && echo Starting Web GUI service... && echo. && !PYTHON_CMD!" REM Wait for service to start echo Starting service, please wait... set /a count=0 :wait_loop timeout /t 2 /nobreak >nul set /a count+=1 REM Check if port is listening (use temporary variable to pass port number) set "CHECK_PORT=!WEB_PORT!" powershell -NoProfile -Command "$listener = Get-NetTCPConnection -LocalPort %CHECK_PORT% -State Listen -ErrorAction SilentlyContinue; if ($listener) { exit 0 } else { exit 1 }" >nul 2>&1 if not errorlevel 1 ( echo [5/6] Web GUI service started goto :open_browser ) REM Timeout check (wait up to 30 seconds) if !count! geq 15 ( echo [WARNING] Service startup timeout (waited 30 seconds) echo Please check the error message in the new "Douyin Web GUI" window echo Or check log file: %LOG_FILE% goto :open_browser ) goto :wait_loop :open_browser REM [6/6] Open browser echo. echo [6/6] Opening browser... timeout /t 1 /nobreak >nul start "" "http://127.0.0.1:!WEB_PORT!" echo. echo ======================================== echo Startup Complete! echo ======================================== echo. echo Web GUI URL: http://127.0.0.1:!WEB_PORT! echo Log file: %LOG_FILE% echo. echo Notes: echo - Web GUI service is running in a new window, do not close that window echo - Closing this window will not stop the Web GUI service echo - To stop the service, close the "Douyin Web GUI" window echo - Or run "StopWebGUIService.bat" echo. REM Ask if view log set /p view_log="View startup log? (Y/N): " if /i "!view_log!"=="Y" ( if exist "%LOG_FILE%" ( notepad "%LOG_FILE%" ) ) :cleanup endlocal if defined _ORIG_CP chcp %_ORIG_CP% >nul 2>&1 exit /b 0