zoukankan      html  css  js  c++  java
  • 字幕文件时间偏移

    有一些视频资料的字幕不同步,需要批量修改时间,写了如下脚本:
    内容如下
    00:00:06.400 --> 00:00:08.700
    Bypassing Network Filters
    比如延迟一秒:
    00:00:07.400 --> 00:00:09.700
    Bypassing Network Filters

    # coding:utf-8
    import os
    import re
    import datetime
    
    
    def listFiles(dirPath):
        fileList = []
        for root, dirs, files in os.walk(dirPath):
            for fileObj in files:
                fileList.append(os.path.join(root, fileObj))
        return fileList
    
    
    def main():
        # fileDir = "/home/huanmsf/Documents/test/"
        fileDir = "/home/huanmsf/Documents/PEN300/media/captions/"
        fileList = listFiles(fileDir)
    
        for fileObj in fileList:
            f = open(fileObj, 'r+')
            all_the_lines = f.readlines()
            f.seek(0)
            f.truncate()
            for line in all_the_lines:
                # str1 = 'original string'
                # str2 = 'replaced string'
                # f.write(line.replace(str1, str2))
                print(line)
                date_all = re.findall(r"(d{2}:d{2}:d{2}.d{3})", line)
                if len(date_all) > 0:
                    a1 = datetime.datetime.strptime(date_all[0], "%H:%M:%S.%f")
                    astr1 = (a1 + datetime.timedelta(seconds=5)).time().__str__()
                    if len(astr1) > 12:
                        astr1 = astr1[:-3]
                    if len(astr1) == 8:
                        astr1 = astr1 + ".000"
                    a2 = datetime.datetime.strptime(date_all[1], "%H:%M:%S.%f")
                    astr2 = (a2 + datetime.timedelta(seconds=5)).time().__str__()
                    if len(astr2) > 12:
                        astr2 = astr2[:-3]
                    if len(astr2) == 8:
                        astr2 = astr2 + ".000"
                    timePart = astr1 + " --> " + astr2 + "
    "
                    print("=======================>")
                    print(timePart)
                    f.write(timePart)
                else:
                    f.write(line)
            f.close()
    
    
    if __name__ == '__main__':
        main()
    
    
  • 相关阅读:
    Linux命令笔记
    拆功放板笔记
    从输入 URL 到页面加载完的过程中都发生了什么---优化
    python学习笔记(三)
    python学习笔记(二)
    python学习笔记(一)
    公交wifi运营平台分析
    testNG小试牛刀
    maven小项目注册服务(三)--web模块
    用maven进行测试
  • 原文地址:https://www.cnblogs.com/lanqie/p/14431999.html
Copyright © 2011-2022 走看看