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


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

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

    1. import re
    2. def convert_timecode(line):
    3. if "-->" in line:
    4. # Split the line into two parts using the arrow
    5. parts = line.strip().split(" --> ")
    6. # Process each part separately
    7. new_parts = []
    8. for i, part in enumerate(parts):
    9. if i == 0:
    10. # For the first timecode, insert a comma between the first two characters
    11. part = part[0] + "," + part[1:]
    12. else:
    13. # For the second timecode, remove the first character
    14. part = part[1:]
    15. # Remove the last digit from the milliseconds part
    16. hours, minutes, seconds_milliseconds = part.split(":")
    17. seconds, milliseconds = seconds_milliseconds.split(",")
    18. milliseconds = milliseconds[:-1] # Remove the last digit
    19. # Replace the last colon before the milliseconds part with a dot
    20. new_part = f"{hours}:{minutes}:{seconds}.{milliseconds}"
    21. new_parts.append(new_part)
    22. # Join the parts back together using a comma
    23. new_line = ",".join(new_parts)
    24. return new_line + "\n"
    25. else:
    26. # If the line doesn't contain an arrow, return it unchanged
    27. return line
    28. # Replace 'input.txt' with the name of your input file, and 'output.txt' with the name of your output file.
    29. with open('input.txt', 'r', encoding='utf-8') as infile, open('output.txt', 'w', encoding='utf-8') as outfile:
    30. for line in infile:
    31. outfile.write(convert_timecode(line))

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

    转换前srt格式时间轴

    转换后ass格式时间轴

  • 相关阅读:
    YOLO系列目标检测算法-YOLOv1
    Redis接口限流、分布式锁与幂等
    Minecraft
    宣布推荐计划有什么方法?
    C++信息学奥赛1191:流感传染
    室友看世界杯我在学redis事务
    OpenAI新模型发布,免费开放GPT-4o!但只开放一点点...
    带你深入理解面向对象三大特性 - 继承,多态
    (附源码)php校园二手交易网站 毕业设计 041148
    实现Promise所有核心功能和方法
  • 原文地址:https://blog.csdn.net/leavemyleave/article/details/134449374