diff --git a/README.md b/README.md index 6474bb9..82bdcb8 100644 --- a/README.md +++ b/README.md @@ -72,9 +72,12 @@ FunClip v2.2.1 keeps the supported Gradio 4 runtime and renders built-in subtitl FunClip's current model and subtitle compatibility paths require `funasr>=1.4.9`. This includes the MOSS vLLM adapter, long-audio generation controls, normalized `sentence_info` speaker segments, and the earlier SenseVoice and realtime fixes. If you installed FunClip before this requirement was updated, run `python -m pip install -U "funasr>=1.4.9"` before starting the Gradio service. [Release notes](https://github.com/modelscope/FunASR/releases/tag/v1.4.9) · [PyPI](https://pypi.org/project/funasr/1.4.9/) -### imagemagick install (Optional) +### Burn Chinese subtitles into a video + +The current source tree includes Noto Sans CJK SC Medium and its [font license](font/OFL.txt). The built-in Pillow renderer needs no separate font download, ImageMagick installation, or security policy changes. This applies to subtitle rendering only; model weights and video encoding dependencies are separate. + +ImageMagick is only needed for the legacy demo or a custom MoviePy 1.x `TextClip` workflow. -The built-in subtitle renderer in v2.2.1 uses Pillow and the bundled font, so standard subtitle clipping no longer requires ImageMagick. Install ImageMagick only for the legacy `funclip/test/imagemagick_test.py` example or your own MoviePy `TextClip` workflow. ## Use FunClip diff --git a/README_zh.md b/README_zh.md index 5b96814..a73aa43 100644 --- a/README_zh.md +++ b/README_zh.md @@ -74,31 +74,11 @@ FunClip v2.2.1 使用 Pillow 渲染可选字幕颜色,并继续在 Gradio 4 FunClip 当前模型与字幕兼容路径需要 `funasr>=1.4.9`,其中包括 MOSS 的 vLLM 适配器、长音频生成上限、归一化的 `sentence_info` 说话人分段,以及此前的 SenseVoice 和实时修复。如果你之前已经安装过 FunClip,请先执行 `python -m pip install -U "funasr>=1.4.9"`,再启动 Gradio 服务。[发布说明](https://github.com/modelscope/FunASR/releases/tag/v1.4.9) · [PyPI](https://pypi.org/project/funasr/1.4.9/) -### 安装 ImageMagick(可选) +### 在视频中烧录中文字幕 -内置字幕渲染器使用 Pillow 和仓库自带字体,不要求安装 ImageMagick。只有使用旧版或自定义 MoviePy `TextClip` 工作流时才需要以下配置。 +当前源码自带 Noto Sans CJK SC Medium 字体和[字体许可证](font/OFL.txt)。内置字幕渲染器使用 Pillow,不需要另行下载字体、安装 ImageMagick 或修改其安全策略。此说明仅针对字幕渲染;模型权重和视频编码依赖仍需按上文准备。 -- Ubuntu -```shell -apt-get -y update && apt-get -y install ffmpeg imagemagick -sed -i 's/none/read,write/g' /etc/ImageMagick-6/policy.xml -``` -- MacOS -```shell -brew install imagemagick -sed -i '' 's/none/read,write/g' "$(brew --prefix imagemagick)/etc/ImageMagick-7/policy.xml" -``` -- Windows - -首先下载并安装imagemagick https://imagemagick.org/script/download.php#windows - -然后确定您的Python安装位置,在其中的`site-packages\moviepy\config_defaults.py`文件中修改`IMAGEMAGICK_BINARY`为imagemagick的exe路径 - -2. 下载你需要的字体文件,这里我们提供一个默认的黑体字体文件 - -```shell -wget https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ClipVideo/STHeitiMedium.ttc -O font/STHeitiMedium.ttc -``` +只有自行使用 MoviePy 1.x 的 `TextClip`(例如旧版 ImageMagick 示例)才需要单独配置 ImageMagick。 ## 使用FunClip diff --git a/font/NotoSansCJKsc-Medium.otf b/font/NotoSansCJKsc-Medium.otf new file mode 100644 index 0000000..00b01df Binary files /dev/null and b/font/NotoSansCJKsc-Medium.otf differ diff --git a/font/OFL.txt b/font/OFL.txt new file mode 100644 index 0000000..d952d62 --- /dev/null +++ b/font/OFL.txt @@ -0,0 +1,92 @@ +This Font Software is licensed under the SIL Open Font License, +Version 1.1. + +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +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/font/README.md b/font/README.md new file mode 100644 index 0000000..514d8fd --- /dev/null +++ b/font/README.md @@ -0,0 +1,5 @@ +# Subtitle font + +FunClip bundles the unmodified [Noto Sans CJK SC Medium](https://github.com/notofonts/noto-cjk/blob/523d033d6cb47f4a80c58a35753646f5c3608a78/Sans/OTF/SimplifiedChinese/NotoSansCJKsc-Medium.otf) from Noto CJK release `Sans2.004`. Its embedded copyright is © 2014-2021 Adobe. + +The font is distributed under the [SIL Open Font License 1.1](OFL.txt), separately from the project's MIT license. Keep the font's copyright information and [upstream license](https://github.com/notofonts/noto-cjk/blob/523d033d6cb47f4a80c58a35753646f5c3608a78/LICENSE) with redistributed copies. diff --git a/font/STHeitiMedium.ttc b/font/STHeitiMedium.ttc deleted file mode 100644 index 24df082..0000000 Binary files a/font/STHeitiMedium.ttc and /dev/null differ diff --git a/funclip/subtitle_renderer.py b/funclip/subtitle_renderer.py index 74205f3..a3b3d7e 100644 --- a/funclip/subtitle_renderer.py +++ b/funclip/subtitle_renderer.py @@ -9,7 +9,7 @@ DEFAULT_FONT_PATH = ( - Path(__file__).resolve().parents[1] / "font" / "STHeitiMedium.ttc" + Path(__file__).resolve().parents[1] / "font" / "NotoSansCJKsc-Medium.otf" ) diff --git a/funclip/test/imagemagick_test.py b/funclip/test/imagemagick_test.py index a5ce990..849034b 100644 --- a/funclip/test/imagemagick_test.py +++ b/funclip/test/imagemagick_test.py @@ -6,7 +6,10 @@ from moviepy.editor import VideoFileClip, concatenate_videoclips from moviepy.video.compositing import CompositeVideoClip - generator = lambda txt: TextClip(txt, font='./font/STHeitiMedium.ttc', fontsize=48, color='white') + from pathlib import Path + + font = Path(__file__).resolve().parents[2] / "font" / "NotoSansCJKsc-Medium.otf" + generator = lambda txt: TextClip(txt, font=str(font), fontsize=48, color='white') subs = [((0, 2), 'sub1中文字幕'), ((2, 4), 'subs2'), ((4, 6), 'subs3'), diff --git a/scripts/verify_release_cli.py b/scripts/verify_release_cli.py index 80621fd..7786741 100644 --- a/scripts/verify_release_cli.py +++ b/scripts/verify_release_cli.py @@ -1,4 +1,4 @@ -"""Exercise the stage-2 CLI in both archives produced by build_release_assets.py.""" +"""Exercise the stage-2 CLI and subtitle renderer from both built archives.""" import argparse import hashlib import os @@ -12,6 +12,7 @@ ROOT = Path(__file__).resolve().parents[1] TEST = Path("tests/test_cli_video_clip.py") +SUBTITLES = Path("tests/test_subtitle_renderer.py") CASE = TEST.as_posix() + "::CLIClipTests::test_stage_two_exports_the_selected_video_and_subtitle" @@ -36,10 +37,12 @@ def verify(dist): with tarfile.open(artifact, "r:gz") as archive: archive.extractall(tmp, filter="data") root = Path(tmp) / prefix - if (root / TEST).read_bytes() != (ROOT / TEST).read_bytes(): - raise ValueError(f"Packaged CLI regression differs from checkout in {artifact.name}") - print(f"Testing stage-2 export from {artifact.name}", flush=True) - subprocess.run([sys.executable, "-I", "-m", "pytest", "-q", CASE], + for test in (TEST, SUBTITLES): + if (root / test).read_bytes() != (ROOT / test).read_bytes(): + raise ValueError(f"{test} differs from checkout in {artifact.name}") + print(f"Testing stage-2 export and subtitle rendering from {artifact.name}", flush=True) + subprocess.run([sys.executable, "-I", "-m", "pytest", "-q", "-o", "pythonpath=.", + CASE, SUBTITLES.as_posix()], cwd=root, env=env, check=True, timeout=180) diff --git a/tests/test_subtitle_renderer.py b/tests/test_subtitle_renderer.py index 57d684d..035518e 100644 --- a/tests/test_subtitle_renderer.py +++ b/tests/test_subtitle_renderer.py @@ -1,14 +1,41 @@ +import hashlib from pathlib import Path import numpy as np -from moviepy.editor import ColorClip, CompositeVideoClip +from moviepy.editor import ColorClip, CompositeVideoClip, VideoFileClip from moviepy.video.tools.subtitles import SubtitlesClip +from PIL import ImageFont -from funclip.subtitle_renderer import make_text_clip +from funclip.subtitle_renderer import DEFAULT_FONT_PATH, make_text_clip ROOT = Path(__file__).resolve().parents[1] -FONT_PATH = ROOT / "font" / "STHeitiMedium.ttc" +FONT_PATH = DEFAULT_FONT_PATH + + +def test_bundled_font_and_license_match_the_upstream_release(): + # Pinned unmodified files from notofonts/noto-cjk Sans2.004 (see font/README.md). + for path, sha256 in ( + (FONT_PATH, "ca094f6b0001fb048ca39ddd797a0cdb0179e1e55c6561e111c49c3e6a61d7b7"), + (FONT_PATH.with_name("OFL.txt"), "6a73f9541c2de74158c0e7cf6b0a58ef774f5a780bf191f2d7ec9cc53efe2bf2"), + ): + assert hashlib.sha256(path.read_bytes()).hexdigest() == sha256 + + +def test_default_chinese_subtitles_survive_video_encoding(tmp_path): + font = ImageFont.truetype(str(FONT_PATH), 40) + masks = [bytes(font.getmask(c)) for c in ("字", "幕", "\U0010ffff")] + assert len(set(masks)) == 3 # Two distinct glyphs, neither the missing-glyph box. + + path = tmp_path / "subtitles.mp4" + with ColorClip((192, 96), color=(0, 0, 0), duration=1) as background: + with make_text_clip("字幕", font_size=40, color="red") as text: + with CompositeVideoClip([background, text.set_duration(1).set_pos("center")]) as video: + video.write_videofile(str(path), fps=4, codec="libx264", audio=False, logger=None) + with VideoFileClip(str(path), audio=False) as video: + frame = video.get_frame(0.5) + red = (frame[:, :, 0] > 160) & (frame[:, :, 1] < 80) & (frame[:, :, 2] < 80) + assert red[:, :96].sum() > 50 and red[:, 96:].sum() > 50 def _foreground_rgb(color):