Skip to content

Commit

Permalink
feat: preset ass config (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tohrusky authored Aug 19, 2024
1 parent b48dec8 commit b183c82
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ license = "GPL-3.0-only"
name = "yuisub"
readme = "README.md"
repository = "https://github.com/TensoRaws/yuisub"
version = "0.0.4"
version = "0.0.5"

# Requirements
[tool.poetry.dependencies]
Expand Down
7 changes: 6 additions & 1 deletion tests/test_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ def test_audio() -> None:
sub.save(util.projectPATH / "assets" / "test.audio.ass")


@pytest.mark.skipif(os.environ.get("GITHUB_ACTIONS") == "true", reason="Skipping test when running on CI")
def test_bilingual() -> None:
sub = load(util.TEST_ENG_SRT)
_ = bilingual(sub, sub)


@pytest.mark.skipif(os.environ.get("GITHUB_ACTIONS") == "true", reason="Skipping test when running on CI")
def test_bilingual_2() -> None:
sub = load(util.TEST_ENG_SRT)

sub_zh = translate(
sub=sub,
Expand Down
2 changes: 1 addition & 1 deletion yuisub/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def anime_prompt(bangumi_url: str | None = None) -> str:
return (
"""
你的目标是把动漫新番的台词翻译成中文,要翻译得自然、流畅和地道,使用贴合二次元的表达方式。
请注意,由于字幕可能是通过 AI 生成的,所以会有一些错误的地方,请你在翻译时尽量保持逻辑性
字幕可能是通过 AI 生成的,请你在翻译时尽量保持逻辑性和连贯性
此外,我可能会给你一些动漫相关的信息。请注意,当人名等专有名词出现时,严格按照我提供的信息进行翻译。
"""
Expand Down
66 changes: 45 additions & 21 deletions yuisub/sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@
from yuisub.llm import Translator
from yuisub.prompt import ORIGIN

PRESET_STYLES: dict[str, SSAStyle] = {
"zh": SSAStyle(
alignment=Alignment.BOTTOM_CENTER,
primarycolor=Color(215, 215, 215),
fontsize=18,
fontname="Microsoft YaHei",
bold=True,
shadow=0,
outline=1,
outlinecolor=Color(198, 107, 107),
),
"origin": SSAStyle(
alignment=Alignment.BOTTOM_CENTER,
primarycolor=pysubs2.Color(249, 246, 240),
fontsize=10,
fontname="Microsoft YaHei",
shadow=0,
outline=0.5,
),
}


def load(sub_path: Path | str, encoding: str = "utf-8") -> SSAFile:
"""
Expand All @@ -24,7 +45,14 @@ def load(sub_path: Path | str, encoding: str = "utf-8") -> SSAFile:


@retry(wait=wait_random(min=3, max=5), stop=stop_after_attempt(5))
def translate(sub: SSAFile, model: str, api_key: str, base_url: str, bangumi_url: str | None = None) -> SSAFile:
def translate(
sub: SSAFile,
model: str,
api_key: str,
base_url: str,
bangumi_url: str | None = None,
styles: dict[str, SSAStyle] | None = None,
) -> SSAFile:
"""
Translate subtitle file to Chinese
Expand All @@ -33,6 +61,7 @@ def translate(sub: SSAFile, model: str, api_key: str, base_url: str, bangumi_url
:param api_key: llm api_key
:param base_url: llm base_url
:param bangumi_url: anime bangumi url
:param styles: subtitle styles, default is PRESET_STYLES
:return:
"""

Expand All @@ -57,43 +86,38 @@ async def _wait_tasks() -> None:
asyncio.run(_wait_tasks())

# generate Chinese subtitle
if styles is None:
styles = PRESET_STYLES

sub_zh = deepcopy(sub)
sub_zh.styles = styles
for i, _ in enumerate(sub):
sub_zh[i].text = trans_list[i]
sub_zh[i].style = "zh"

return sub_zh


def bilingual(sub_origin: SSAFile, sub_zh: SSAFile) -> SSAFile:
def bilingual(
sub_origin: SSAFile,
sub_zh: SSAFile,
styles: dict[str, SSAStyle] | None = None,
) -> SSAFile:
"""
Generate bilingual subtitle file
:param sub_origin: Origin subtitle
:param sub_zh: Chinese subtitle
:param styles: subtitle styles, default is PRESET_STYLES
:return:
"""

# generate bilingual subtitle
if styles is None:
styles = PRESET_STYLES

sub_bilingual = SSAFile()
sub_bilingual.styles = {
"zh": SSAStyle(
alignment=Alignment.BOTTOM_CENTER,
primarycolor=Color(255, 192, 203),
fontsize=16,
fontname="Microsoft YaHei",
bold=True,
shadow=0,
outline=0.2,
),
"origin": SSAStyle(
alignment=Alignment.BOTTOM_CENTER,
primarycolor=pysubs2.Color(249, 246, 240),
fontsize=12,
fontname="Microsoft YaHei",
shadow=0,
outline=0.5,
),
}
sub_bilingual.styles = styles

for e in sub_origin:
e.style = "origin"
Expand Down

0 comments on commit b183c82

Please sign in to comment.