Skip to content

Commit

Permalink
Merge pull request #10 from thelastfantasy/v1.2.0-release-2
Browse files Browse the repository at this point in the history
fix: streamline build process by executing PyInstaller from project r…
  • Loading branch information
thelastfantasy authored Oct 19, 2024
2 parents 3900cac + b53915a commit 511cb15
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 133 deletions.
183 changes: 55 additions & 128 deletions .github/workflows/create-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,14 @@ jobs:
uses: actions/checkout@v3
with:
token: ${{ secrets.PAT_TOKEN }}
submodules: "recursive"

- name: Validate branch name and extract version
id: validate_title
run: |
branch_name="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
echo "Branch name: $branch_name"
echo '1' $GITHUB_REF
echo '2' $GITHUB_REF
echo '3' $GITHUB_REF_NAME
echo '4' $GITHUB_HEAD_REF
echo '5' $GITHUB_BASE_REF
if [[ "$branch_name" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9-]+)?$ ]]; then
version=$(echo "$branch_name" | grep -oE '^v[0-9]+\.[0-9]+\.[0-9]+')
echo "version=$version" >> "$GITHUB_OUTPUT"
Expand All @@ -50,143 +45,75 @@ jobs:
- name: Create a tag
if: ${{ steps.validate_title.outputs.version != '' }}
run: |
git tag ${{ steps.validate_title.outputs.version }}
git push origin ${{ steps.validate_title.outputs.version }}
# Job 2: Compile using matrix for different platforms
compile:
needs: validate_and_tag
if: ${{ needs.validate_and_tag.outputs.version != '' }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
include:
- os: ubuntu-latest
python-version: "3.x"
- os: windows-latest
python-version: "3.x"
architecture: "x64"
- os: macos-latest
python-version: "3.x"

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
architecture: ${{ matrix.architecture || '' }}

- name: Install Dependencies
run: |
if [ -f src/requirements.txt ]; then
pip install -r src/requirements.txt
fi
- name: Install PyInstaller
run: |
pip install pyinstaller
- name: Compile for ${{ matrix.os }}
run: |
cd src
pyinstaller --onefile sub_adjust.py
pyinstaller --onefile sub_converter.py
cd ..
if [ "${{ matrix.os }}" == "ubuntu-latest" ]; then
mkdir -p build/linux
mv src/dist/sub_adjust build/linux/
mv src/dist/sub_converter build/linux/
elif [ "${{ matrix.os }}" == "windows-latest" ]; then
mkdir -p build/windows
mv src/dist/sub_adjust.exe build/windows/
mv src/dist/sub_converter.exe build/windows/
elif [ "${{ matrix.os }}" == "macos-latest" ]; then
mkdir -p build/macos
mv src/dist/sub_adjust build/macos/
mv src/dist/sub_converter build/macos/
version=${{ steps.validate_title.outputs.version }}
# 检查本地是否存在该 Tag
tag_exists=$(git tag -l "$version")
if [ -z "$tag_exists" ]; then
# 本地不存在该 Tag,创建 Tag
git tag "$version"
# 检查远程是否存在该 Tag
remote_tag_exists=$(git ls-remote --tags origin | grep "refs/tags/$version" || true)
if [ -z "$remote_tag_exists" ]; then
# 如果远程也不存在,推送 Tag
git push origin "$version"
echo "Tag $version created and pushed successfully."
else
echo "Tag $version already exists in remote. Skipping push."
fi
else
echo "Tag $version already exists locally. Skipping tag creation and push."
fi
shell: bash

# Job 3: Create release
create_release:
needs: [validate_and_tag, compile]
if: ${{ needs.validate_and_tag.outputs.version != '' }}
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

# Tar compiled Linux binaries
- name: Tar Linux binaries
run: |
tar -czvf "sub_adjust-v${{ needs.validate_and_tag.outputs.version }}-linux-x64.tar.gz" -C build/linux .
# Tar compiled macOS binaries
- name: Tar macOS binaries
run: |
tar -czvf "sub_adjust-v${{ needs.validate_and_tag.outputs.version }}-macos-x64.tar.gz" -C build/macos .
# Zip compiled Windows binaries
- name: Zip Windows binaries
- name: List all files in the workspace
run: |
zip -r "sub_adjust-v${{ needs.validate_and_tag.outputs.version }}-win-x64.zip" build/windows
echo "Workspace directory: $GITHUB_WORKSPACE"
ls -la $GITHUB_WORKSPACE
echo "Listing all files recursively:"
find $GITHUB_WORKSPACE
# Zip source code
- name: Zip source code
- name: Create release assets archive
run: |
zip -r "sub_adjust-v${{ needs.validate_and_tag.outputs.version }}-source.zip" .
output_zip="sub-adjust_${{ steps.validate_title.outputs.version }}.zip"
assets_file="$GITHUB_WORKSPACE/.github/workflows/PackageAssets.txt"
# 创建包含指定文件的压缩包
while IFS= read -r asset; do
echo "Adding asset: $asset"
zip -j "$output_zip" "$GITHUB_WORKSPACE/$asset"
done < "$assets_file"
shell: bash

# Create GitHub release
# Create a release and upload the zip file
- name: Create GitHub release
id: create_release_step
uses: actions/create-release@v1
id: create_release
if: ${{ steps.validate_title.outputs.version != '' }}
uses: actions/create-release@latest
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
with:
tag_name: ${{ needs.validate_and_tag.outputs.version }}
release_name: Release ${{ needs.validate_and_tag.outputs.version }}
tag_name: ${{ steps.validate_title.outputs.version }}
release_name: ${{ steps.validate_title.outputs.version }}
body: |
Automatically created release for version ${{ needs.validate_and_tag.outputs.version }}.
Automatically created release for version ${{ steps.validate_title.outputs.version }}.
### PR Description:
${{ github.event.pull_request.body }}
draft: false
prerelease: false
commitish: ${{ github.sha }}

# Upload Linux binaries tar.gz file to release
- name: Upload Linux binaries tar.gz file to release
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release_step.outputs.upload_url }}
asset_path: ./sub_adjust-v${{ needs.validate_and_tag.outputs.version }}-linux-x64.tar.gz
asset_name: sub_adjust-v${{ needs.validate_and_tag.outputs.version }}-linux-x64.tar.gz
asset_content_type: application/gzip

# Upload macOS binaries tar.gz file to release
- name: Upload macOS binaries tar.gz file to release
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release_step.outputs.upload_url }}
asset_path: ./sub_adjust-v${{ needs.validate_and_tag.outputs.version }}-macos-x64.tar.gz
asset_name: sub_adjust-v${{ needs.validate_and_tag.outputs.version }}-macos-x64.tar.gz
asset_content_type: application/gzip

# Upload Windows binaries zip file to release
- name: Upload Windows binaries zip file to release
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release_step.outputs.upload_url }}
asset_path: ./sub_adjust-v${{ needs.validate_and_tag.outputs.version }}-win-x64.zip
asset_name: sub_adjust-v${{ needs.validate_and_tag.outputs.version }}-win-x64.zip
asset_content_type: application/zip

# Upload source zip file to release
- name: Upload source zip file to release
# Upload the zip file as the release asset
- name: Upload zip file to release
if: ${{ steps.validate_title.outputs.version != '' }}
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
with:
upload_url: ${{ steps.create_release_step.outputs.upload_url }}
asset_path: ./sub_adjust-v${{ needs.validate_and_tag.outputs.version }}-source.zip
asset_name: sub_adjust-v${{ needs.validate_and_tag.outputs.version }}-source.zip
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./sub-adjust_${{ steps.validate_title.outputs.version }}.zip
asset_name: sub-adjust_${{ steps.validate_title.outputs.version }}.zip
asset_content_type: application/zip
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@
功能没sushi强大,但是主打一个轻巧、快速、简单。

# 说明
下载各自平台对应的版本,解压后运行可执行文件即可
解压后如果是首次运行,执行对应平台的install_dependencies_once.bat (Windows) 或 install_dependencies_once.sh (Linux & MacOS) 即可简单安装依赖工具

两款工具均只支持UTF-8编码字幕文件。
运行各个start_xxx.bat (Windows) 或 start_xxx.sh (Linux & MacOS) 即可启动对应工具的GUI版本。
sub_adjust命令行版本帮助请点击GUI界面右下角按钮查阅。

<font color="red">两款工具均只支持UTF-8编码字幕文件。</font>

sub_adjust:
调轴工具,支持srt、ass、ssa三种格式,支持批量操作(默认读取程序目录下的字幕文件,不含子目录),可双击GUI运行亦可命令行运行。
命令行版本帮助请点击GUI界面右下角。

sub_converter:
srt转ass字幕工具,支持批量操作(默认读取程序目录下的字幕文件,不含子目录),可使用自定义元数据,目前仅可GUI运行。
srt转ass字幕工具,支持批量操作(默认读取程序目录下的字幕文件,不含子目录),可使用自定义元数据,目前仅可GUI运行。

# 为什么不提供编译好的可执行文件?
编译后太大了,可以尝试自行编译

编译步骤:
1. 安装Git并克隆本项目源码
2. 执行install_dependencies_once.bat (Windows) 或 install_dependencies_once.sh (Linux & MacOS) 安装Python等依赖工具
3. 安装pyinstaller: `pip install -U pyinstaller`
4. 在项目目录下执行 `pyinstaller -w --onefile sub_adjust.py``pyinstaller -w --onefile sub_converter.py` 即可在`./dist`目录下生成可执行文件
7 changes: 7 additions & 0 deletions install_dependencies_once.bat
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ if %ERRORLEVEL% NEQ 0 (
exit /b 1
)

REM Check and install pip
where pip >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
echo pip未安装或未正确配置环境变量,请先安装pip并确保其路径已添加到环境变量中。
exit /b 1
)

REM Try install deps from requirements.txt
pip install -r requirements.txt
if %ERRORLEVEL% NEQ 0 (
Expand Down
6 changes: 4 additions & 2 deletions sub_adjust.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import webbrowser
from docopt import docopt
from loguru import logger
from utils import center_window, custom_messagebox, display_errors
from utils import center_window, custom_messagebox, display_errors, hide_console
import queue

__version__ = 'sub_adjust v1.2.0'
Expand All @@ -17,14 +17,15 @@
sub_adjust --offset <subtitle_shift_seconds> [--layers <layer_numbers>] [INPUTS...]
sub_adjust --version
sub_adjust (-h | --help)
sub_adjust
Options:
-t --offset <subtitle_shift_seconds> 字幕偏移量(单位:秒,1s = 1000ms),支持小数、负数、正数,负数为提前,正数为延后。
--layers <layer_numbers> 可选参数,将时间调整仅应用到此处设置的Layer中。默认为 all
--version 显示版本信息
-h --help 显示帮助信息
Examples (二进制版本):
Examples (二进制版本,需自行编译):
# 将字幕提前2.5秒
sub_adjust --offset -2.5 example.ass
Expand Down Expand Up @@ -351,6 +352,7 @@ def main():

# 否则启动GUI(没有提供 --offset 时)
else:
hide_console()
logger.info("正在启动GUI...")
start_ui()

Expand Down
10 changes: 10 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
import tkinter as tk
from tkinter import scrolledtext
import chardet
import sys

def hide_console():
"""在 Windows 下隐藏控制台窗口"""
if sys.platform == "win32":
import ctypes
whnd = ctypes.windll.kernel32.GetConsoleWindow()
if whnd != 0:
ctypes.windll.user32.ShowWindow(whnd, 0)
ctypes.windll.kernel32.CloseHandle(whnd)

def detect_encoding(file_path):
with open(file_path, 'rb') as f:
Expand Down

0 comments on commit 511cb15

Please sign in to comment.