欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

基于python修改srt字幕的时间轴

时间:2021-04-21 09:40:05|栏目:Python代码|点击:

这篇文章主要介绍了基于python修改srt字幕的时间轴,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

代码如下

# -*- coding: utf-8 -*-
# @时间 : 2020-01-19 02:53
# @作者 : 陈祥安
# @文件名 : run.py.py
# @公众号: Python学习开发

import os
import datetime

temp = os.path.dirname(__file__)
srt_file_path = os.path.join(temp, "15.srt")


def read_srt_file_gen():
  with open(srt_file_path, "r") as fs:
    for data in fs.readlines():
      yield data


def read_srt_file():
  with open(srt_file_path, "r") as fs:
    data = fs.read()
  return data


def start():
  new_data_str = read_srt_file()
  for item in read_srt_file_gen():
    if "--> " in item:
      time_arr = item.split('--> ')
      start_time = time_arr[0].replace(" ", "")
      end_time = time_arr[1].replace("\n", "")
      _new_start_time = datetime.datetime.strptime(start_time + "0", "%H:%M:%S,%f") - datetime.timedelta(
        seconds=1)
      _new_end_time = datetime.datetime.strptime(end_time + "0", "%H:%M:%S,%f") - datetime.timedelta(
        seconds=1)
      new_start_time = datetime.datetime.strftime(_new_start_time, "%H:%M:%S,%f")[:-3]
      new_end_time = datetime.datetime.strftime(_new_end_time, "%H:%M:%S,%f")[:-3]
      new_data_str = new_data_str.replace(start_time, new_start_time).replace(end_time, new_end_time)
  return new_data_str


if __name__ == '__main__':
  print(start())

上一篇:PyTorch中Tensor的维度变换实现

栏    目:Python代码

下一篇:基于Python-turtle库绘制路飞的草帽骷髅旗、美国队长的盾牌、高达的源码

本文标题:基于python修改srt字幕的时间轴

本文地址:http://www.codeinn.net/misctech/105984.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有