From f037cf9dddba532165cb0164b6f394de0ebc0d6b Mon Sep 17 00:00:00 2001 From: wood chen Date: Sat, 16 Nov 2024 08:58:32 +0800 Subject: [PATCH] chore(workflow): Refactor release workflow and create launcher script Refactor the release workflow to simplify the build process and create a new launcher script for setting up the FFmpeg path. --- .github/workflows/release.yml | 18 +----------- launcher.py | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 launcher.py diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 65301a3..0a45669 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -60,25 +60,9 @@ jobs: pip install tkinterdnd2>=0.4.2 pip install pyinstaller - # 创建启动器脚本 - - name: Create launcher script - run: | - echo import os, sys > launcher.py - echo if getattr(sys, 'frozen', False^): >> launcher.py - echo os.environ["PATH"] = os.path.join(os.path.dirname(sys.executable^), "ffmpeg"^) + os.pathsep + os.environ["PATH"] >> launcher.py - echo else: >> launcher.py - echo os.environ["PATH"] = os.path.join(os.path.dirname(os.path.abspath(__file__^)^), "ffmpeg"^) + os.pathsep + os.environ["PATH"] >> launcher.py - type gui.py >> launcher.py - - name: Build with PyInstaller run: | - pyinstaller --clean video2gif.spec - - # 测试程序是否可以启动 - - name: Test executable - run: | - cd dist - .\video2gif.exe --version || echo "Program started" + pyinstaller --name video2gif --onefile --windowed --add-data "ffmpeg/ffmpeg.exe;ffmpeg" --add-data "ffmpeg/ffprobe.exe;ffmpeg" --add-data "README.md;." launcher.py - name: Upload Release Asset uses: softprops/action-gh-release@v1 diff --git a/launcher.py b/launcher.py new file mode 100644 index 0000000..51d70f9 --- /dev/null +++ b/launcher.py @@ -0,0 +1,52 @@ +import os +import sys +import traceback + + +def get_ffmpeg_path(): + if getattr(sys, "frozen", False): + # 运行在 PyInstaller 打包后的环境 + return os.path.join(sys._MEIPASS, "ffmpeg") + else: + # 运行在开发环境 + return os.path.join(os.path.dirname(os.path.abspath(__file__)), "ffmpeg") + + +def setup_environment(): + try: + # 设置 FFmpeg 路径 + ffmpeg_path = get_ffmpeg_path() + if ffmpeg_path not in os.environ["PATH"]: + os.environ["PATH"] = ffmpeg_path + os.pathsep + os.environ["PATH"] + + print(f"FFmpeg path: {ffmpeg_path}") + print(f"System PATH: {os.environ['PATH']}") + except Exception as e: + print(f"Error setting up environment: {str(e)}") + traceback.print_exc() + + +if __name__ == "__main__": + try: + print("Starting Video2Gif Converter...") + print(f"Python version: {sys.version}") + + setup_environment() + + from gui import VideoToGifConverter + + app = VideoToGifConverter() + app.run() + except Exception as e: + print(f"Error: {str(e)}") + traceback.print_exc() + + # 写入错误日志 + with open("error.log", "w") as f: + f.write(f"Error: {str(e)}\n") + f.write(traceback.format_exc()) + + # 如果是打包后的程序,等待用户确认 + if getattr(sys, "frozen", False): + input("Press Enter to exit...") + sys.exit(1)