chore(workflow): update build process with PyInstaller spec file

Update the GitHub Actions workflow to use the PyInstaller spec file for building the application, ensuring proper inclusion of FFmpeg executables and improving the build process.
This commit is contained in:
wood chen 2024-11-16 10:17:36 +08:00
parent 93a01d21cf
commit 0decff023e
3 changed files with 44 additions and 11 deletions

View File

@ -63,7 +63,8 @@ jobs:
- name: Build with PyInstaller - name: Build with PyInstaller
shell: cmd shell: cmd
run: | run: |
pyinstaller --name video2gif --onefile --windowed --add-data "ffmpeg/ffmpeg.exe;ffmpeg" --add-data "ffmpeg/ffprobe.exe;ffmpeg" --add-data "README.md;." --clean --noconfirm --log-level=INFO gui.py pyinstaller --clean video2gif.spec
- name: Upload Release Asset - name: Upload Release Asset
uses: softprops/action-gh-release@v1 uses: softprops/action-gh-release@v1

46
gui.py
View File

@ -6,19 +6,43 @@ import platform
import os import os
import webbrowser import webbrowser
from threading import Thread from threading import Thread
import ffmpeg
import traceback import traceback
# 在类定义前添加 FFmpeg 路径设置 # 在类定义前添加 FFmpeg 路径设置
if getattr(sys, "frozen", False): if getattr(sys, "frozen", False):
# 运行在 PyInstaller 打包后的环境 # 运行在 PyInstaller 打包后的环境
ffmpeg_path = os.path.join(sys._MEIPASS, "ffmpeg") ffmpeg_path = os.path.join(
sys._MEIPASS,
"ffmpeg",
"ffmpeg.exe" if platform.system().lower() == "windows" else "ffmpeg",
)
ffprobe_path = os.path.join(
sys._MEIPASS,
"ffmpeg",
"ffprobe.exe" if platform.system().lower() == "windows" else "ffprobe",
)
else: else:
# 运行在开发环境 # 运行在开发环境
ffmpeg_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "ffmpeg") ffmpeg_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"ffmpeg",
"ffmpeg.exe" if platform.system().lower() == "windows" else "ffmpeg",
)
ffprobe_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"ffmpeg",
"ffprobe.exe" if platform.system().lower() == "windows" else "ffprobe",
)
if ffmpeg_path not in os.environ["PATH"]: # 设置 ffmpeg-python 包使用的 FFmpeg 路径
os.environ["PATH"] = ffmpeg_path + os.pathsep + os.environ["PATH"] import ffmpeg
ffmpeg._run.DEFAULT_FFMPEG_PATH = ffmpeg_path
ffmpeg._run.DEFAULT_FFPROBE_PATH = ffprobe_path
# 添加到系统 PATH
if os.path.dirname(ffmpeg_path) not in os.environ["PATH"]:
os.environ["PATH"] = os.path.dirname(ffmpeg_path) + os.pathsep + os.environ["PATH"]
class VideoToGifConverter: class VideoToGifConverter:
@ -267,6 +291,16 @@ class VideoToGifConverter:
def convert_video_to_gif(self, video_path): def convert_video_to_gif(self, video_path):
try: try:
# 打印环境信息
print(f"FFmpeg path: {ffmpeg_path}")
print(f"FFprobe path: {ffprobe_path}")
print(f"System PATH: {os.environ['PATH']}")
# 验证 FFmpeg 是否可用
try:
version_info = ffmpeg.probe(ffmpeg._run.DEFAULT_FFMPEG_PATH)
print(f"FFmpeg version info: {version_info}")
except Exception as e:
print(f"FFmpeg probe error: {e}")
# 验证输入 # 验证输入
if not self.validate_inputs(): if not self.validate_inputs():
return False return False
@ -362,6 +396,8 @@ class VideoToGifConverter:
except Exception as e: except Exception as e:
error_msg = str(e) error_msg = str(e)
print(f"Conversion error: {error_msg}") print(f"Conversion error: {error_msg}")
print(f"Error type: {type(e)}")
traceback.print_exc()
messagebox.showerror("错误", f"转换失败:\n{error_msg}") messagebox.showerror("错误", f"转换失败:\n{error_msg}")
return False return False

View File

@ -1,7 +1,4 @@
# video2gif.spec # video2gif.spec
import sys
from PyInstaller.utils.hooks import collect_data_files
block_cipher = None block_cipher = None
a = Analysis( a = Analysis(
@ -13,7 +10,7 @@ a = Analysis(
('ffmpeg/ffprobe.exe', 'ffmpeg'), ('ffmpeg/ffprobe.exe', 'ffmpeg'),
('README.md', '.') ('README.md', '.')
], ],
hiddenimports=['tkinter', 'tkinter.ttk', 'tkinterdnd2'], hiddenimports=['ffmpeg', 'ffmpeg-python'],
hookspath=[], hookspath=[],
hooksconfig={}, hooksconfig={},
runtime_hooks=[], runtime_hooks=[],
@ -45,5 +42,4 @@ exe = EXE(
target_arch=None, target_arch=None,
codesign_identity=None, codesign_identity=None,
entitlements_file=None, entitlements_file=None,
icon='icon.ico' # 如果你有图标的话
) )