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

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

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

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

import re

def convert_timecode(line):
    if "-->" in line:
        # Split the line into two parts using the arrow
        parts = line.strip().split(" --> ")

        # Process each part separately
        new_parts = []
        for i, part in enumerate(parts):
            if i == 0:
                # For the first timecode, insert a comma between the first two characters
                part = part[0] + "," + part[1:]
            else:
                # For the second timecode, remove the first character
                part = part[1:]

            # Remove the last digit from the milliseconds part
            hours, 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 dot
            new_part = f"{hours}:{minutes}:{seconds}.{milliseconds}"
            new_parts.append(new_part)

        # Join the parts back together using a comma
        new_line = ",".join(new_parts)
        return new_line + "\n"
    else:
        # If the line doesn't contain an arrow, return it unchanged
        return 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.kler.cn/news/134035.html

相关文章:

  • 16. @PostConstruct注解和开关原理(验证码开关、IP开关)
  • 流量4----4
  • 【Java 进阶篇】JQuery 事件绑定:`on` 与 `off` 的奇妙舞曲
  • fully_connected与linear
  • C++学习 --vector
  • Linux 零拷贝splice函数
  • 【C++】入门三
  • DeepMind发布新模型Mirasol3B:更高效处理音频、视频数据
  • 竞赛选题 深度学习花卉识别 - python 机器视觉 opencv
  • ExoPlayer架构详解与源码分析(9)——TsExtractor
  • 【Synopsys Bug记录】DC综合报错(显示warning:Unable to resolve reference)
  • DrugMAP: molecular atlas and pharma-information of all drugs学习
  • transform学习资料
  • LLM大模型4位量化实战【GPTQ】
  • Linux 配置RealVNC远程访问
  • 中国制库:创新引领,效率突破,塑造行业新标准
  • redis三种集群方式
  • 21、嵌套路由实战操作
  • Smart Tomcat的使用
  • FastJsonAPI
  • 系列二十六、idea安装javap -c
  • Linux服务器挂载另一台服务器的文件夹(mount)
  • 力扣刷题篇之位运算
  • 力扣83. 删除排序链表中的重复元素(java常规解法 + 建立虚拟头节点)
  • springBoot 配置druid多数据源 MySQL+SQLSERVER
  • sqli-labs关卡20(基于http头部报错盲注)通关思路
  • vite vue3安装element-plus
  • 【开源】基于Vue.js的开放实验室管理系统的设计和实现
  • 【Java 进阶篇】Ajax 入门:打开前端异步交互的大门
  • 【Kingbase FlySync】命令模式:部署双轨并行,并实现切换同步