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.
This commit is contained in:
wood chen 2024-11-16 08:58:32 +08:00
parent 79d10fcb89
commit f037cf9ddd
2 changed files with 53 additions and 17 deletions

View File

@ -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

52
launcher.py Normal file
View File

@ -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)