当前位置: 首页 > news >正文

Python实现视频字幕时间轴格式转换

自己喜欢收藏电影,有时网上能找到的中文字幕文件都不满足自己电影版本。在自己下载的压制版电影中已内封非中文srt字幕时,可以选择自己将srt的时间轴转为ass并替换ass中的时间轴。自己在频繁
复制粘贴改格式的时候想起可以用Python代码完成转换这一操作,借助ChatGPT并自己稍作修改后用代码实现了。自己测试可用。

没指定srt文件的路径,使用前首先需要将srt后缀先改为txt文本格式,代码默认输入为“input.txt”,输出“output.txt”。运行时待转换的txt文件需要和py文件在同一目录内。
 

import redef convert_timecode(line):if "-->" in line:# Split the line into two parts using the arrowparts = line.strip().split(" --> ")# Process each part separatelynew_parts = []for i, part in enumerate(parts):if i == 0:# For the first timecode, insert a comma between the first two characterspart = part[0] + "," + part[1:]else:# For the second timecode, remove the first characterpart = part[1:]# Remove the last digit from the milliseconds parthours, minutes, seconds_milliseconds = part.split(":")seconds, milliseconds = seconds_milliseconds.split(",")milliseconds = milliseconds[:-1]  # Remove the last digit# Replace the last colon before the milliseconds part with a dotnew_part = f"{hours}:{minutes}:{seconds}.{milliseconds}"new_parts.append(new_part)# Join the parts back together using a commanew_line = ",".join(new_parts)return new_line + "\n"else:# If the line doesn't contain an arrow, return it unchangedreturn line# Replace 'input.txt' with the name of your input file, and 'output.txt' with the name of your output file.
with open('input.txt', 'r', encoding='utf-8') as infile, open('output.txt', 'w', encoding='utf-8') as outfile:for line in infile:outfile.write(convert_timecode(line))

不过还是需要手动对照翻译复制粘贴进行调轴,就是比以前手动改时间轴格式方便了些。不知道有没有一键将srt直接按照格式转为ass的软件,甚至实现普通字幕改特效字幕。

转换前srt格式时间轴

转换后ass格式时间轴

http://www.lryc.cn/news/234588.html

相关文章:

  • 蓝桥杯 枚举
  • C#的MessagePack(unity)--02
  • MySQL数据库管理--- mysql数据库迁移-v查看报错sql
  • 基于秃鹰算法优化概率神经网络PNN的分类预测 - 附代码
  • 向pycdc项目提的一个pr
  • Spring学习③__Bean管理
  • 《视觉SLAM十四讲》-- 后端 2
  • 安装插件时Vscode XHR Failed 报错ERR_CERT_AUTHORITY_INVALID
  • ON_WM_TIMER()
  • 【Unity】单例模式及游戏声音管理类应用
  • 视频剪辑技巧:轻松搞定视频随机合并,一篇文章告知所有秘诀
  • torch.stack
  • 手搓哈希表、列表、队列,只为了用C语言快速求解华容道游戏,我不是大佬,只是一个游戏算法爱好者
  • MySQL 的执行原理(一)
  • 2023_“数维杯”问题B:棉秸秆热解的催化反应-详细解析含代码
  • django理解01
  • 限制Domain Admin登录非域控服务器和用户计算机
  • 原来机械硬盘比内存慢10万倍
  • ElementUI的Dialog弹窗实现拖拽移动功能
  • 生成式AI模型量化简明教程
  • 机器人制作开源方案 | 智能快递付件机器人
  • PostgreSQL技术大讲堂 - 第34讲:调优工具pgBagder部署
  • 《Python日志新宠——Loguru,轻松记录,笑对Bug!》
  • NET8 ORM 使用AOT SqlSugar
  • CCRC认证是什么?
  • linux内核面试题(2)
  • YOLOV5----修改损失函数-ShuffleAttention
  • Kafka(四)消费者消费消息
  • Python uiautomation获取微信内容!聊天记录、聊天列表、全都可获取
  • Java通过Lettuce访问Redis主从,哨兵,集群