Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ COPY frontend/ frontend/
RUN cd frontend && pnpm install --frozen-lockfile && pnpm build

# Copy source
COPY assets/ assets/
COPY src/ src/

EXPOSE 7860
Expand Down
25 changes: 25 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
BilingualSub
Copyright 2024-2026 Maple (mapleee723@gmail.com)

Licensed under the Apache License, Version 2.0.

== Trademark Notice ==

"BilingualSub" is the product name of this project. Per Section 6 of the
Apache License 2.0, this license does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor, except
as required for reasonable and customary use in describing the origin of the
Work.

If you create a modified version of this software, you must not distribute
it under the name "BilingualSub" without explicit written permission from
the copyright holder. This includes, but is not limited to, the product
name displayed in video intros, watermarks, and user interfaces.

== Third-Party Fonts ==

This project bundles font files under the SIL Open Font License, Version 1.1.
See assets/fonts/LICENSE for details.

- LINE Seed Sans: Copyright (c) 2022 LINE Corporation
- Noto Sans TC: Copyright (c) 2012-2024 Google LLC
98 changes: 98 additions & 0 deletions assets/fonts/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
The font files in this directory are distributed under the SIL Open Font License, Version 1.1.

=== LINE Seed Sans ===

Copyright (c) 2022 LINE Corporation
Licensed under the SIL Open Font License, Version 1.1.
https://github.com/line/seed

=== Noto Sans TC ===

Copyright (c) 2012-2024 Google LLC
Licensed under the SIL Open Font License, Version 1.1.
https://github.com/notofonts/noto-fonts

=== SIL Open Font License, Version 1.1 ===

PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.

The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply to
any document created using the fonts or their derivatives.

DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.

"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).

"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).

"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.

"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.

PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:

1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.

2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.

3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.

4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.

5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.

TERMINATION
This license becomes null and void if any of the above conditions are
not met.

DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
Binary file added assets/fonts/LINESeedSans_Bd.ttf
Binary file not shown.
Binary file added assets/fonts/LINESeedSans_Rg.ttf
Binary file not shown.
84 changes: 57 additions & 27 deletions src/bilingualsub/utils/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,30 @@

import ffmpeg

# ---------------------------------------------------------------------------
# Bundled font paths — assets/fonts/ next to the project root
# ---------------------------------------------------------------------------

_ASSETS_DIR = Path(__file__).resolve().parent.parent.parent.parent / "assets" / "fonts"
_FONT_EN_REGULAR = _ASSETS_DIR / "LINESeedSans_Rg.ttf"
_FONT_EN_BOLD = _ASSETS_DIR / "LINESeedSans_Bd.ttf"
_FONT_ZH_REGULAR = _ASSETS_DIR / "NotoSansTC-Regular.ttf"
_FONT_ZH_BOLD = _ASSETS_DIR / "NotoSansTC-Bold.ttf"


def _font_arg(fontfile: Path, fallback_name: str) -> str:
"""Return a drawtext font option.

Uses ``fontfile='<path>'`` when the bundled file exists, otherwise falls
back to ``font='<name>'`` so the filter chain still works without the
assets directory (e.g. inside Docker where system fonts are available).
"""
if fontfile.exists():
# fontfile= needs an absolute path; resolve() guarantees that.
escaped = str(fontfile.resolve()).replace("'", "'").replace(":", r"\:")
return f"fontfile='{escaped}'"
return f"font='{fallback_name}'"


class FFmpegError(Exception):
"""Exception raised when FFmpeg operations fail."""
Expand Down Expand Up @@ -96,7 +120,7 @@ def _append_watermark_drawtext(vf_filter: str, watermark_text: str) -> str:
safe_text = _escape_drawtext(watermark_text)
watermark_drawtext = (
f"drawtext=text='{safe_text}'"
":font='Arial'"
f":{_font_arg(_FONT_EN_REGULAR, 'Arial')}"
":fontsize=16"
":fontcolor=white@0.6"
":shadowcolor=black@0.8"
Expand Down Expand Up @@ -492,19 +516,24 @@ def _block_enable(start: float) -> str:

def _dt(
text: str,
font: str,
font_spec: str,
fontsize: int,
fontcolor: str,
x: str,
y: str,
enable_expr: str,
fade_start: float,
) -> str:
"""Build a drawtext filter block.

``font_spec`` is the output of ``_font_arg()`` — either
``fontfile='<path>'`` or ``font='<name>'``.
"""
safe = _escape_drawtext(text)
alpha_expr = f"if(lt(t,{fade_start:.1f}),0,min((t-{fade_start:.1f})/0.3,1))"
return (
f"drawtext=text='{safe}'"
f":font='{font}'"
f":{font_spec}"
f":fontsize={fontsize}"
f":fontcolor={fontcolor}"
f":x={x}"
Expand All @@ -514,19 +543,20 @@ def _dt(
":fix_bounds=1"
)

# Y positions scaled to video height
y_eyebrow = int(height * 0.14)
y_chinese_label = y_eyebrow + int(height / 25)
y_channel = y_chinese_label + int(height / 22)
y_channel_url = y_channel + int(height / 20)
y_title = (y_channel_url if channel_url else y_channel) + int(height / 22)
y_video_url = y_title + int(height / 28)
y_decl_zh_1 = y_video_url + int(height / 20)
y_decl_zh_2 = y_decl_zh_1 + int(height / 40)
y_decl_zh_3 = y_decl_zh_2 + int(height / 40)
y_decl_en_1 = y_decl_zh_3 + int(height / 30)
y_decl_en_2 = y_decl_en_1 + int(height / 44)
y_decl_en_3 = y_decl_en_2 + int(height / 44)
# Y positions: vertically centered with generous spacing
# Total content height ~50% of frame, starting at ~28%
y_eyebrow = int(height * 0.22)
y_chinese_label = y_eyebrow + int(height * 0.04)
y_channel = y_chinese_label + int(height * 0.05)
y_channel_url = y_channel + int(height * 0.055)
y_title = (y_channel_url if channel_url else y_channel) + int(height * 0.06)
y_video_url = y_title + int(height * 0.04)
y_decl_zh_1 = y_video_url + int(height * 0.06)
y_decl_zh_2 = y_decl_zh_1 + int(height * 0.03)
y_decl_zh_3 = y_decl_zh_2 + int(height * 0.03)
y_decl_en_1 = y_decl_zh_3 + int(height * 0.04)
y_decl_en_2 = y_decl_en_1 + int(height * 0.028)
y_decl_en_3 = y_decl_en_2 + int(height * 0.028)

x_left = str(left_margin)
x_brand = f"w-tw-{int(width * 0.04)}"
Expand All @@ -545,7 +575,7 @@ def _next_start() -> float:
blocks.append(
_dt(
"ORIGINAL VIDEO FROM",
"Arial",
_font_arg(_FONT_EN_REGULAR, "Arial"),
max(1, int(height / 54)),
"white@0.3",
x_left,
Expand All @@ -560,7 +590,7 @@ def _next_start() -> float:
blocks.append(
_dt(
"原始影片來自",
"serif",
_font_arg(_FONT_ZH_REGULAR, "serif"),
max(1, int(height / 42)),
"white@0.6",
x_left,
Expand All @@ -575,7 +605,7 @@ def _next_start() -> float:
blocks.append(
_dt(
channel,
"Arial",
_font_arg(_FONT_EN_BOLD, "Arial"),
max(1, int(height / 17)),
"white@1.0",
x_left,
Expand All @@ -591,7 +621,7 @@ def _next_start() -> float:
blocks.append(
_dt(
channel_url,
"Arial",
_font_arg(_FONT_EN_REGULAR, "Arial"),
max(1, int(height / 49)),
"white@0.35",
x_left,
Expand All @@ -606,7 +636,7 @@ def _next_start() -> float:
blocks.append(
_dt(
video_title,
"serif",
_font_arg(_FONT_ZH_REGULAR, "serif"),
max(1, int(height / 34)),
"white@0.7",
x_left,
Expand All @@ -621,7 +651,7 @@ def _next_start() -> float:
blocks.append(
_dt(
video_url,
"Arial",
_font_arg(_FONT_EN_REGULAR, "Arial"),
max(1, int(height / 45)),
"white@0.5",
x_left,
Expand All @@ -633,7 +663,7 @@ def _next_start() -> float:

# Chinese declaration (3 lines)
decl_zh = [
"翻譯字幕由開源專案 BilingualSub 產生",
"翻譯字幕使用開源專案 BilingualSub 製作",
"所有內容及著作權屬於原始創作者所有",
"如需移除,請聯繫上傳者", # noqa: RUF001
]
Expand All @@ -643,7 +673,7 @@ def _next_start() -> float:
blocks.append(
_dt(
line,
"serif",
_font_arg(_FONT_ZH_REGULAR, "serif"),
max(1, int(height / 45)),
"white@0.45",
x_left,
Expand All @@ -655,7 +685,7 @@ def _next_start() -> float:

# English declaration (3 lines)
decl_en = [
"Subtitles generated by BilingualSub (open source)",
"Subtitles created with BilingualSub (open source)",
"All content and copyrights belong to the original creator",
"For removal requests, please contact the uploader",
]
Expand All @@ -665,7 +695,7 @@ def _next_start() -> float:
blocks.append(
_dt(
line,
"Arial",
_font_arg(_FONT_EN_REGULAR, "Arial"),
max(1, int(height / 49)),
"white@0.35",
x_left,
Expand All @@ -679,7 +709,7 @@ def _next_start() -> float:
blocks.append(
_dt(
"BilingualSub",
"Arial",
_font_arg(_FONT_EN_REGULAR, "Arial"),
max(1, int(height / 54)),
"white@0.25",
x_brand,
Expand Down
Loading