pyJianYingDraft 在 import_srt 字幕添加花字效果
随着视频编辑需求的不断增长,自动化处理字幕的功能也变得尤为重要。在使用pyJianYingDraft进行视频编辑时,字幕添加和特效应用是常见的操作。然而,原有的 import_srt
方法并未提供直接支持字幕花字效果的功能。因此,如何通过修改源码文件来实现这一功能,成为了用户的一大需求。
本文将探讨如何通过修改pyJianYingDraft的源码,实现将花字效果自动应用到字幕中的方法。通过此方法,可以更加高效、便捷地为视频中的每一条字幕添加特效。
文章目录
- 项目介绍
- 工作流程
- 总结
项目介绍
需要在剪映草稿中使用 import_srt
方法添加字幕花字效果,这里使用原有的函数方法无法使用。
官网的Demo是下面这样,但是无法在 import_srt
添加效果,如果想实现自动生成效果那么需要修改代码。
# 创建一个带气泡效果的文本片段并添加到轨道中
text_segment = draft.TextSegment("据说pyJianYingDraft效果还不错?", video_segment.target_timerange, # 文本片段的首尾与上方视频片段一致font=draft.FontType.文轩体, # 设置字体为文轩体style=draft.TextStyle(color=(1.0, 1.0, 0.0)), # 字体颜色为黄色clip_settings=draft.ClipSettings(transform_y=-0.8) # 位置在屏幕下方
)
text_segment.add_animation(draft.TextOutro.故障闪动, duration=tim("1s")) # 添加出场动画“故障闪动”, 设置时长为1s
text_segment.add_bubble("361595", "6742029398926430728") # 添加文本气泡效果, 相应素材元数据的获取参见readme中"提取素材元数据"部分
text_segment.add_effect("7296357486490144036") # 添加花字效果, 相应素材元数据的获取参见readme中"提取素材元数据"部分
script.add_segment(text_segment)# 保存草稿(覆盖掉原有的draft_content.json)
script.dump(DUMP_PATH)
工作流程
这里需要修改源码文件 Lib\site-packages\pyJianYingDraft\script_file.py
文件。
414行的 def import_srt
替换成下面的代码
def import_srt(self, srt_path: str, track_name: str, *,time_offset: Union[str, float] = 0.0,style_reference: Optional[TextSegment] = None,text_style: TextStyle = TextStyle(size=5, align=1, auto_wrapping=True),clip_settings: Optional[ClipSettings] = ClipSettings(transform_y=-0.8)) -> "ScriptFile":"""从SRT文件中导入字幕,并为每个字幕添加华字效果或其他特效"""if style_reference is None and clip_settings is None:raise ValueError("未提供样式参考时请提供`clip_settings`参数")time_offset = tim(time_offset)if track_name not in self.tracks:self.add_track(TrackType.text, track_name, relative_index=999) # 在所有文本轨道的最上层with open(srt_path, "r", encoding="utf-8-sig") as srt_file:lines = srt_file.readlines()def __add_text_segment(text: str, t_range: Timerange) -> None:"""为每个字幕片段创建TextSegment并添加华字特效"""if style_reference:seg = TextSegment.create_from_template(text, t_range, style_reference)if clip_settings is not None:seg.clip_settings = deepcopy(clip_settings)else:seg = TextSegment(text, t_range, style=text_style, clip_settings=clip_settings)# 在字幕片段上添加"华字"效果(可替换为其他效果)hua_zi_effect_id = "7296357486490144036" # 这里用的是示例IDseg.add_effect(hua_zi_effect_id) # 添加华字效果# 添加到轨道self.add_segment(seg, track_name)index = 0text: str = ""text_trange: Timerangeread_state: Literal["index", "timestamp", "content"] = "index"while index < len(lines):line = lines[index].strip()if read_state == "index":if len(line) == 0:index += 1continueif not line.isdigit():raise ValueError("Expected a number at line %d, got '%s'" % (index + 1, line))index += 1read_state = "timestamp"elif read_state == "timestamp":# 读取时间戳start_str, end_str = line.split(" --> ")start, end = srt_tstamp(start_str), srt_tstamp(end_str)text_trange = Timerange(start + time_offset, end - start)index += 1read_state = "content"elif read_state == "content":# 内容结束, 生成片段if len(line) == 0:__add_text_segment(text.strip(), text_trange)text = ""read_state = "index"else:text += line + "\n"index += 1# 添加最后一个片段if len(text) > 0:__add_text_segment(text.strip(), text_trange)return self
调用方法是
# 导入字幕并应用华字效果
script.import_srt(srt_path, track_name="subtitle",text_style=draft.TextStyle( # Corrected heresize=15.0, color=(1.0, 0.0, 0.0)) # 自定义字幕样式
)
# 假设"华字"效果ID为"7296357486490144036"
# 获取字幕轨道
subtitle_track = script.tracks["subtitle"]# 为每个字幕片段添加华字效果
for segment in subtitle_track.segments:segment.add_effect("7296357486490144036") # 添加华字效果
总结
在本文中,通过修改 import_srt
方法,成功实现了自动将花字效果添加到字幕中的功能。通过替换 import_srt
函数的源码,利用 TextSegment
类创建字幕片段,并为每个片段添加了花字效果。修改后的方法不仅简化了字幕特效的应用过程,还提高了工作效率,特别适合需要批量处理字幕的用户。
未来,随着pyJianYingDraft的不断更新,可能会有更多特效可以集成到字幕导入中,进一步提升视频编辑的自动化水平。通过此种方式,更多复杂的视频特效应用也许能更加灵活地实现,满足不同场景的需求。