add subtitle can set font now

This commit is contained in:
sun-guannan
2025-07-19 09:52:37 +08:00
parent 512b1a57e4
commit ae0de1dca3
3 changed files with 19 additions and 0 deletions

View File

@@ -12,6 +12,7 @@ def add_subtitle_impl(
track_name: str = "subtitle", track_name: str = "subtitle",
time_offset: float = 0, time_offset: float = 0,
# Font style parameters # Font style parameters
font: str = None,
font_size: float = 8.0, font_size: float = 8.0,
bold: bool = False, bold: bool = False,
italic: bool = False, italic: bool = False,
@@ -153,6 +154,7 @@ def add_subtitle_impl(
track_name=track_name, track_name=track_name,
time_offset=int(time_offset * 1000000), # Convert seconds to microseconds time_offset=int(time_offset * 1000000), # Convert seconds to microseconds
text_style=text_style, text_style=text_style,
font=font,
clip_settings=clip_settings, clip_settings=clip_settings,
style_reference=style_reference, style_reference=style_reference,
border=text_border, border=text_border,

View File

@@ -229,6 +229,7 @@ def add_subtitle():
time_offset = data.get('time_offset', 0.0) # Default 0 seconds time_offset = data.get('time_offset', 0.0) # Default 0 seconds
# Font style parameters # Font style parameters
font = data.get('font', None)
font_size = data.get('font_size', 5.0) # Default size 5.0 font_size = data.get('font_size', 5.0) # Default size 5.0
bold = data.get('bold', False) # Default not bold bold = data.get('bold', False) # Default not bold
italic = data.get('italic', False) # Default not italic italic = data.get('italic', False) # Default not italic
@@ -276,6 +277,7 @@ def add_subtitle():
track_name=track_name, track_name=track_name,
time_offset=time_offset, time_offset=time_offset,
# Font style parameters # Font style parameters
font = font,
font_size=font_size, font_size=font_size,
bold=bold, bold=bold,
italic=italic, italic=italic,

View File

@@ -6,6 +6,8 @@ from copy import deepcopy
from typing import Optional, Literal, Union, overload from typing import Optional, Literal, Union, overload
from typing import Type, Dict, List, Any from typing import Type, Dict, List, Any
from core.capcut_api.pyJianYingDraft.metadata.font_meta import Font_type
from . import util from . import util
from . import exceptions from . import exceptions
from .template_mode import ImportedTrack, EditableTrack, ImportedMediaTrack, ImportedTextTrack, Shrink_mode, Extend_mode, import_track from .template_mode import ImportedTrack, EditableTrack, ImportedMediaTrack, ImportedTextTrack, Shrink_mode, Extend_mode, import_track
@@ -466,6 +468,7 @@ class Script_file:
def import_srt(self, srt_content: str, track_name: str, *, def import_srt(self, srt_content: str, track_name: str, *,
time_offset: Union[str, float] = 0.0, time_offset: Union[str, float] = 0.0,
style_reference: Optional[Text_segment] = None, style_reference: Optional[Text_segment] = None,
font: Optional[str] = None,
text_style: Text_style = Text_style(size=5, align=1), text_style: Text_style = Text_style(size=5, align=1),
clip_settings: Optional[Clip_settings] = Clip_settings(transform_y=-0.8), clip_settings: Optional[Clip_settings] = Clip_settings(transform_y=-0.8),
border: Optional[Text_border] = None, border: Optional[Text_border] = None,
@@ -480,6 +483,7 @@ class Script_file:
srt_content (`str`): SRT字幕内容或本地文件路径 srt_content (`str`): SRT字幕内容或本地文件路径
track_name (`str`): 导入到的文本轨道名称, 若不存在则自动创建 track_name (`str`): 导入到的文本轨道名称, 若不存在则自动创建
style_reference (`Text_segment`, optional): 作为样式参考的文本片段, 若提供则使用其样式. style_reference (`Text_segment`, optional): 作为样式参考的文本片段, 若提供则使用其样式.
font (`Optional[str]`, optional): 字体, 默认为None.
time_offset (`Union[str, float]`, optional): 字幕整体时间偏移, 单位为微秒, 默认为0. time_offset (`Union[str, float]`, optional): 字幕整体时间偏移, 单位为微秒, 默认为0.
text_style (`Text_style`, optional): 字幕样式, 默认模仿剪映导入字幕时的样式, 会被`style_reference`覆盖. text_style (`Text_style`, optional): 字幕样式, 默认模仿剪映导入字幕时的样式, 会被`style_reference`覆盖.
clip_settings (`Clip_settings`, optional): 图像调节设置, 默认模仿剪映导入字幕时的设置, 会覆盖`style_reference`的设置除非指定为`None`. clip_settings (`Clip_settings`, optional): 图像调节设置, 默认模仿剪映导入字幕时的设置, 会覆盖`style_reference`的设置除非指定为`None`.
@@ -495,6 +499,13 @@ class Script_file:
if style_reference is None and clip_settings is None: if style_reference is None and clip_settings is None:
raise ValueError("未提供样式参考时请提供`clip_settings`参数") raise ValueError("未提供样式参考时请提供`clip_settings`参数")
if font:
try:
font_type = getattr(Font_type, font)
except:
available_fonts = [attr for attr in dir(Font_type) if not attr.startswith('_')]
raise ValueError(f"Unsupported font: {font}, please use one of the fonts in Font_type: {available_fonts}")
time_offset = tim(time_offset) time_offset = tim(time_offset)
# 检查 track_name 是否存在于 self.tracks 或 self.imported_tracks # 检查 track_name 是否存在于 self.tracks 或 self.imported_tracks
track_exists = (track_name in self.tracks) or any(track.name == track_name for track in self.imported_tracks) track_exists = (track_name in self.tracks) or any(track.name == track_name for track in self.imported_tracks)
@@ -531,9 +542,13 @@ class Script_file:
seg.effect = deepcopy(effect) seg.effect = deepcopy(effect)
# 设置固定宽高 # 设置固定宽高
seg.fixed_width = fixed_width seg.fixed_width = fixed_width
# 设置字体
if font_type:
seg.font = font_type.value
else: else:
seg = Text_segment(text, t_range, style=text_style, clip_settings=clip_settings, seg = Text_segment(text, t_range, style=text_style, clip_settings=clip_settings,
border=border, background=background, border=border, background=background,
font = font_type,
fixed_width=fixed_width) fixed_width=fixed_width)
# 添加气泡和花字效果 # 添加气泡和花字效果
if bubble: if bubble: