20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)

本文主要是介绍20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)
2023/8/9 19:02


由于喜欢看纪录片等外文视频,通过剪映/PR2023/AUTOSUB识别字幕之后,可以通过google翻译识别为简体中文的DOCX文档。
DOCX文档转换为TXT文档之后,还需要修饰其中的字幕序号才能得到最终所需要的简体中文SRT文档。


google.py

#f=open("./1574/%03d.ts"%(n+1),"wb")
f=open("12.txt","wb")


#f = open("p:\\ts\\1574.txt")
f1 = open("1.txt")
#for n in range(1,4000):
for n in range(1,4560):
    line = f1.readline()
    #f.write(response.content) 
    #f.write(line)
    f.decode().write(line)

f.close()


google12.py
J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\py>python google12.py > test.srt

f_path=r'1.txt'

temp = 1
xuhao = 1;

with open(f_path) as f:
    lines = f.readlines()

for line in lines:
    if temp == 1:
        print(str(xuhao))
        temp=0
    else:
        if len(line) == 1:
            #print("jiangedian!")
            temp=1
            xuhao = xuhao+1
        print(line.rstrip())


txt2srt3all.py
【处理目录下的全部ANSI编码的TXT字幕为SRT字幕,但是不处理字目录!】

# coding=utf-8
import os

# 获取当前目录
path = os.getcwd()
# 查看当前目录下所有文件
files = os.listdir(path)

# 遍历所有文件
for file in files:
    # 判断文件是否为 txt 文件
    if file.endswith('.txt'):
        # 构建新的文件名
        #new_file = file.replace('.txt', '.json')
        #new_file = file.replace('.txt', '.srt')
        new_file = file.replace('.txt', '.cn.srt')
        # 重命名文件
        #os.rename(os.path.join(path, file), os.path.join(path, new_file))
        
        
        
        f2=open(new_file,"wb")
        
        
        #f_path=r'C:\Users\Admin\Desktop\shapenetcore_partanno_segmentation_benchmark_v0_normal_2\00000001\0.txt'
        #f_path=r'1.txt'
        #f_path=file
        
        temp = 1
        xuhao = 1;
        
        #with open(f_path) as f:
        with open(file) as f:
            lines = f.readlines()
        
        for line in lines:
            if temp == 1:
                #print(str(xuhao))
                #f.decode().write(line)
                #f2.decode().write(str(xuhao))
                #f2.write(str(xuhao))
                f2.write(str(xuhao).encode())
                f2.write(str('\n').encode())
                temp=0
            else:
                if len(line) == 1:
                    #print("jiangedian!")
                    temp=1
                    xuhao = xuhao+1
                    #print(line.rstrip())
                    #f.decode().write(line)
                    #f2.decode().write(line.rstrip())
                    #f2.write(line.rstrip())
                f2.write(line.encode())

        #f=open(new_file,"wb")
        f2.close()

 


LOG:
J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\ansi的TXT>dir
 驱动器 J 中的卷是 18680688682
 卷的序列号是 2A59-69C0

 J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\ansi的TXT 的目录

2023/08/09  19:11    <DIR>          .
2023/08/09  19:11    <DIR>          ..
2023/08/09  12:22            67,713 8月7日.txt
2023/08/09  12:22           113,997 AC3EN2.剪影.txt
2023/08/09  12:22            67,713 path_to_your_word_file.txt
2023/08/09  12:22            75,347 Red.Eye.2005.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.5.1-FGT.eng9.txt
2023/08/09  19:11             1,715 txt2srt3all.py
2023/08/07  22:29             1,671 txt2srt3xuhao56.py
               6 个文件        328,156 字节
               2 个目录 50,770,313,216 可用字节

J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\ansi的TXT>python txt2srt3all.py

J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\ansi的TXT>dir
 驱动器 J 中的卷是 18680688682
 卷的序列号是 2A59-69C0

 J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\ansi的TXT 的目录

2023/08/09  19:11    <DIR>          .
2023/08/09  19:11    <DIR>          ..
2023/08/09  19:11            71,024 8月7日.cn.srt
2023/08/09  12:22            67,713 8月7日.txt
2023/08/09  19:11           120,955 AC3EN2.剪影.cn.srt
2023/08/09  12:22           113,997 AC3EN2.剪影.txt
2023/08/09  19:11            71,024 path_to_your_word_file.cn.srt
2023/08/09  12:22            67,713 path_to_your_word_file.txt
2023/08/09  19:11            81,213 Red.Eye.2005.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.5.1-FGT.eng9.cn.srt
2023/08/09  12:22            75,347 Red.Eye.2005.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.5.1-FGT.eng9.txt
2023/08/09  19:11             1,715 txt2srt3all.py
2023/08/07  22:29             1,671 txt2srt3xuhao56.py
              10 个文件        672,372 字节
               2 个目录 50,769,960,960 可用字节

J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\ansi的TXT>

 


utf8txt2srt3all.py
【处理目录下的全部UTF8编码的TXT字幕为SRT字幕,但是不处理字目录!】

# coding=utf-8
import os

# 获取当前目录
path = os.getcwd()
# 查看当前目录下所有文件
files = os.listdir(path)

# 遍历所有文件
for file in files:
    # 判断文件是否为 txt 文件
    if file.endswith('.txt'):
        # 构建新的文件名
        #new_file = file.replace('.txt', '.json')
        #new_file = file.replace('.txt', '.srt')
        new_file = file.replace('.txt', '.cn.srt')
        # 重命名文件
        #os.rename(os.path.join(path, file), os.path.join(path, new_file))
        
        
        #f2=open(new_file,"wb")
        #with open(new_file, "w", encoding="UTF-8") as txt_file:
        #f2 = open(new_file, "wb", encoding="UTF-8")
        f2 = open(new_file, "w", encoding="UTF-8")
        
        temp = 1
        xuhao = 1;
        
        #with open(f_path) as f:
        #with open(file) as f:
        #with open(new_file, "w", encoding="UTF-8") as txt_file:
        #with open(file, "w", encoding="UTF-8") as f:
        with open(file, "r", encoding="UTF-8") as f:
            lines = f.readlines()
        
        for line in lines:
            if temp == 1:
                #f2.write(str(xuhao).encode())
                #f2.write(str('\n').encode())
                f2.write(str(xuhao))
                f2.write(str('\n'))
                temp=0
            else:
                if len(line) == 1:
                    temp=1
                    xuhao = xuhao+1
                #f2.write(line.encode())
                f2.write(line)

        f2.close()


参考资料:
https://pythonjishu.com/nwbuyryewwscpxl/
使用Python对文件进行批量改名的方法


python docx utf8 读写
https://deepinout.com/python/python-qa/t_how-to-read-and-write-unicode-utf-8-files-in-python.html
如何在Python中读写Unicode(UTF-8)文件?

 

UTF8的脚本的调试记录,写法有很大的差异的!

Microsoft Windows [版本 10.0.19045.2311]
(c) Microsoft Corporation。保留所有权利。

C:\Users\Administrator>cd J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕 (DOCX)\utf8i的TXT

C:\Users\Administrator>j:

J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT>dir
 驱动器 J 中的卷是 18680688682
 卷的序列号是 2A59-69C0

 J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT 的目录

2023/08/09  19:14    <DIR>          .
2023/08/09  19:14    <DIR>          ..
2023/08/09  12:27            78,650 8月7日.txt
2023/08/09  12:27           133,327 AC3EN2.剪影.txt
2023/08/09  12:27            78,650 path_to_save_txt+utf8_file.txt
2023/08/09  12:27            78,650 path_to_your_word_file.txt
2023/08/09  19:11             1,715 txt2srt3all.py
               5 个文件        370,992 字节
               2 个目录 50,769,956,864 可用字节

J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT>
J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT>python txt2srt3all.py
Traceback (most recent call last):
  File "J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT\txt2srt3all.py", line 34, in <module>
    lines = f.readlines()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xb7 in position 82: illegal multibyte sequence

J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT>python txt2srt3all.py

J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT>python utf8txt2srt3all.py
Traceback (most recent call last):
  File "J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT\utf8txt2srt3all.py", line 23, in <module>
    f2 = open(new_file, "wb", encoding="UTF-8")
ValueError: binary mode doesn't take an encoding argument

J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT>python utf8txt2srt3all.py
Traceback (most recent call last):
  File "J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT\utf8txt2srt3all.py", line 33, in <module>
    lines = f.readlines()
io.UnsupportedOperation: not readable

J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT>python utf8txt2srt3all.py
Traceback (most recent call last):
  File "J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT\utf8txt2srt3all.py", line 38, in <module>
    f2.write(str(xuhao).encode())
TypeError: write() argument must be str, not bytes

J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT>python utf8txt2srt3all.py
Traceback (most recent call last):
  File "J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT\utf8txt2srt3all.py", line 40, in <module>
    f2.write(str('\n').encode())
TypeError: write() argument must be str, not bytes

J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT>python utf8txt2srt3all.py

J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT>python utf8txt2srt3all.py

J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT>python utf8txt2srt3all.py

J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT>dir
 驱动器 J 中的卷是 18680688682
 卷的序列号是 2A59-69C0

 J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT 的目录

2023/08/09  19:29    <DIR>          .
2023/08/09  19:29    <DIR>          ..
2023/08/09  19:29            75,580 8月7日.cn.srt
2023/08/09  12:27            78,650 8月7日.txt
2023/08/09  19:29           128,367 AC3EN2.剪影.cn.srt
2023/08/09  12:27           133,327 AC3EN2.剪影.txt
2023/08/09  19:29            75,580 path_to_save_txt+utf8_file.cn.srt
2023/08/09  12:27            78,650 path_to_save_txt+utf8_file.txt
2023/08/09  19:29            75,580 path_to_your_word_file.cn.srt
2023/08/09  12:27            78,650 path_to_your_word_file.txt
2023/08/09  19:29            86,176 Red.Eye.2005.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.5.1-FGT.eng9.cn.srt
2023/08/09  19:28            89,228 Red.Eye.2005.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.5.1-FGT.eng9.txt
2023/08/09  19:11             1,715 txt2srt3all.py
2023/08/09  19:24             1,568 utf8txt2srt3all.py
              12 个文件        903,071 字节
               2 个目录 50,767,888,384 可用字节

J:\!!!!文档整理20230625\en2cn\20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)\utf8i的TXT>

 

这篇关于20230809在WIN10下使用python3处理Google翻译获取的SRT格式字幕(DOCX)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/237797

相关文章

Python+FFmpeg实现视频自动化处理的完整指南

《Python+FFmpeg实现视频自动化处理的完整指南》本文总结了一套在Python中使用subprocess.run调用FFmpeg进行视频自动化处理的解决方案,涵盖了跨平台硬件加速、中间素材处理... 目录一、 跨平台硬件加速:统一接口设计1. 核心映射逻辑2. python 实现代码二、 中间素材处

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Go异常处理、泛型和文件操作实例代码

《Go异常处理、泛型和文件操作实例代码》Go语言的异常处理机制与传统的面向对象语言(如Java、C#)所使用的try-catch结构有所不同,它采用了自己独特的设计理念和方法,:本文主要介绍Go异... 目录一:异常处理常见的异常处理向上抛中断程序恢复程序二:泛型泛型函数泛型结构体泛型切片泛型 map三:文

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

Springboot3 ResponseEntity 完全使用案例

《Springboot3ResponseEntity完全使用案例》ResponseEntity是SpringBoot中控制HTTP响应的核心工具——它能让你精准定义响应状态码、响应头、响应体,相比... 目录Spring Boot 3 ResponseEntity 完全使用教程前置准备1. 项目基础依赖(M

springboot的controller中如何获取applicatim.yml的配置值

《springboot的controller中如何获取applicatim.yml的配置值》本文介绍了在SpringBoot的Controller中获取application.yml配置值的四种方式,... 目录1. 使用@Value注解(最常用)application.yml 配置Controller 中

Java使用Spire.Barcode for Java实现条形码生成与识别

《Java使用Spire.BarcodeforJava实现条形码生成与识别》在现代商业和技术领域,条形码无处不在,本教程将引导您深入了解如何在您的Java项目中利用Spire.Barcodefor... 目录1. Spire.Barcode for Java 简介与环境配置2. 使用 Spire.Barco

Android使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求

C# 预处理指令(# 指令)的具体使用

《C#预处理指令(#指令)的具体使用》本文主要介绍了C#预处理指令(#指令)的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录1、预处理指令的本质2、条件编译指令2.1 #define 和 #undef2.2 #if, #el