srt文件与视频文件合成在一起导入剪映

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
python 实现 PC端剪映字幕转换SRT格式工具代码-Python 实现,# -*- coding: utf-8 -*- import getpass import os import json import re def get_time(time_int): # 使用正则表达式处理时间格式化问题 if time_int == 0: return '00:00:00,000' p = re.compile(r'(\d*)(\d{3})\d{3}') pl = p.findall(str(time_int))[0] if pl[0] == '': hms = '00:00:00' else: h = 0 m = 0 s = int(pl[0]) while s >= 60: m += 1 s -= 60 while m >= 60: h += 1 m -= 60 while h >= 24: exit('暂不支持超过24小时的字幕文件转换') hms = ':'.join((str(h).zfill(2), str(m).zfill(2), str(s).zfill(2))) return ','.join((hms, pl[1])) def format_time(start, end): # 拼接时间格式化后的字符串 return ' --> '.join((get_time(start), get_time(end))) def main(): # 取得电脑的用户名 username = getpass.getuser() # 拼接取得json文件夹所在地址 json_root_path = 'C:/Users/' + username + '/AppData/Local/JianyingPro/User Data/Projects/com.lveditor.draft/' # 拿到最后一次打开的json文件(内含字幕信息) if os.path.exists(json_root_path): with open(os.path.join(json_root_path, 'root_draft_meta_info.json'), 'r', encoding='utf-8') as f: json_path = (json.load(f)['all_draft_store'][0]['draft_fold_path']) # 打开json文件并将其转换为srt文件 if os.path.exists(json_path): with open(os.path.join(json_path, 'draft_content.json'), 'r', encoding='utf-8') as f: j = json.load(f) l1 = [] l2 = [] for i in j['tracks'][1]['segments']: start_time = int(i['target_timerange']['start']) end_time = int(i['target_timerange']['start'] + i['target_timerange']['duration']) l1.append(format_time(start_time, end_time)) for i in j['materials']['texts']: l2.append(i['content']) idx = 0 # 可以在此处自定义新建的srt字幕路径及文件名 with open('测试.srt', 'w', encoding='utf-8') as srt: while idx < len(l1):
要在 Android 应用中显示 SRT 字幕,需要进行以下步骤: 1. 解析 SRT 文件SRT 文件是一种简单的文本文件,可以使用 Java 中的 BufferedReader 和 InputStreamReader 类来读取文件并将其解析为字符串或其他格式。你可以使用正则表达式或字符串操作将每个字幕的时间戳、文本和样式等信息分离出来。 2. 将字幕与视频同步:要将字幕与视频同步,你需要使用视频播放器的 API 来确定当前播放位置,并将相应的字幕显示在屏幕上。你可以使用 Android 中的 MediaPlayer 类或 ExoPlayer 库来播放视频,并使用 Handler 或 Timer 来定期检查播放位置并更新字幕。 3. 显示字幕:一旦你获得了每个字幕的信息并将其与视频同步,你需要将其显示在屏幕上。你可以使用 Android 中的 TextView 或自定义视图来显示字幕,并使用 SpannableString 类来设置样式。 这里是一个简单的代码示例,演示了如何解析 SRT 文件并将其显示在 TextView 中: ``` private void displaySubtitles(String subtitlesFile) { try { BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream(subtitlesFile), "UTF-8")); String line; StringBuilder subtitleText = new StringBuilder(); while ((line = reader.readLine()) != null) { if (line.matches("\\d+")) { // This is the subtitle index subtitleText.append("\n"); } else if (line.matches("\\d{2}:\\d{2}:\\d{2},\\d{3} --> \\d{2}:\\d{2}:\\d{2},\\d{3}")) { // This is the subtitle timecode } else if (!line.isEmpty()) { // This is the subtitle text subtitleText.append(line).append(" "); } else { // This is the end of the subtitle TextView subtitleView = findViewById(R.id.subtitle_view); SpannableString subtitleSpannable = new SpannableString(subtitleText.toString()); // Set the subtitle style here subtitleView.setText(subtitleSpannable); subtitleText = new StringBuilder(); } } reader.close(); } catch (IOException e) { e.printStackTrace(); } } ``` 请注意,此示例仅演示了如何解析 SRT 文件并将其显示在 TextView 中,并不包括与视频同步的代码。要将其与视频同步,你需要在适当的时间调用 `displaySubtitles()` 方法,并将相应的字幕显示在屏幕上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值