diff --git a/Dockerfile b/Dockerfile index 9e1d61b..0e8076e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/NOTICE b/NOTICE new file mode 100644 index 0000000..22cf9c2 --- /dev/null +++ b/NOTICE @@ -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 diff --git a/assets/fonts/LICENSE b/assets/fonts/LICENSE new file mode 100644 index 0000000..4a8384d --- /dev/null +++ b/assets/fonts/LICENSE @@ -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. diff --git a/assets/fonts/LINESeedSans_Bd.ttf b/assets/fonts/LINESeedSans_Bd.ttf new file mode 100644 index 0000000..4cad980 Binary files /dev/null and b/assets/fonts/LINESeedSans_Bd.ttf differ diff --git a/assets/fonts/LINESeedSans_Rg.ttf b/assets/fonts/LINESeedSans_Rg.ttf new file mode 100644 index 0000000..3ca994b Binary files /dev/null and b/assets/fonts/LINESeedSans_Rg.ttf differ diff --git a/src/bilingualsub/utils/ffmpeg.py b/src/bilingualsub/utils/ffmpeg.py index 2ed0777..e00fbd0 100644 --- a/src/bilingualsub/utils/ffmpeg.py +++ b/src/bilingualsub/utils/ffmpeg.py @@ -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=''`` when the bundled file exists, otherwise falls + back to ``font=''`` 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.""" @@ -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" @@ -492,7 +516,7 @@ def _block_enable(start: float) -> str: def _dt( text: str, - font: str, + font_spec: str, fontsize: int, fontcolor: str, x: str, @@ -500,11 +524,16 @@ def _dt( enable_expr: str, fade_start: float, ) -> str: + """Build a drawtext filter block. + + ``font_spec`` is the output of ``_font_arg()`` — either + ``fontfile=''`` or ``font=''``. + """ 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}" @@ -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)}" @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -633,7 +663,7 @@ def _next_start() -> float: # Chinese declaration (3 lines) decl_zh = [ - "翻譯字幕由開源專案 BilingualSub 產生", + "翻譯字幕使用開源專案 BilingualSub 製作", "所有內容及著作權屬於原始創作者所有", "如需移除,請聯繫上傳者", # noqa: RUF001 ] @@ -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, @@ -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", ] @@ -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, @@ -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,