python获取字幕剧本(绿箭侠第一季)

前情提要

网上找到了喜爱的美剧的字幕文件

双击打开*.srt文件默认是播放器的形式

我想练习英语,又想好好品一品这部剧

于是我用记事本打开了文件

开始了文本提取之旅(只为学习和爱好兴趣实践,无任何商业意图)

绿箭侠字幕资料

链接:https://pan.baidu.com/s/1QGba-p6mhhMD8B--r2LYqQ

提取码:ikc8

明确任务

当前的文件有

绿箭侠.Arrow.S01E22.1080p.BluRay.x264.chs&eng.srt
绿箭侠.Arrow.S01E22.1080p.BluRay.x264.chs&eng.ass
绿箭侠.Arrow.S01E22.1080p.BluRay.x264.chs.srt

需要把中英字幕的文件筛选出来(.chs&eng.srt)

此文件具体内容,了解它的格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1
00:00:02,090 --> 00:00:04,720
我是奥利弗·奎恩
My name is Oliver Queen.

2
00:00:04,720 --> 00:00:06,250
五年里 我被困孤岛
For 5 years, I was stranded

3
00:00:06,260 --> 00:00:08,560
只有一个目标
on an island with only one goal--

4
00:00:08,560 --> 00:00:09,910
生存
survive.

ok,了解了规律,开始写

注意并不是所有的情况都是4行一起(那样的话%5取余来取)

很多情况下不满足4行一起的,3行或5行

如果出现了不是4行的(行数%5取余方法会让后续结果全部出错)

所以 以”–>”为开始行标志,空行作为结束行标志,tag 变量控制

很简单了,上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os


def getFileName(path): #获取指定目录下的所有chs&eng.srt后缀的文件名
result_list = []
f_list = os.listdir(path)
for i in f_list:
if i[-11::] == "chs&eng.srt":
result_list.append(i)
return result_list


if __name__ == '__main__':

path = 'C://Users//asus//Desktop//arrow' #文件夹对应目录
file_name_list = getFileName(path)

path = os.getcwd() #获取.py文件夹的目录
filename = path + "绿箭侠第一季.txt"
new = open(filename,"w",errors='ignore')

new.write("绿箭侠第一季字幕文档版(学英语)\n\n")
for i in file_name_list:
file_name = os.path.join("C://Users//asus//Desktop//arrow",i)
with open(file_name,"r",errors='ignore') as file: #开始了一集的字幕读取读取
new.write( "\n\n\n第"+i[14:16]+"集"+"\n\n")
tag = False #某行是否需要写到结果文件
for line in file.readlines():
if "-->" in line:
tag = True
continue
elif not line.replace('\n',''):
new.write(line)
tag = False
continue
if tag:
print(line.strip())
new.write(line)
new.close()

结果呈现

绿箭侠第一季字幕文档版(学英语)

第01集

1

本字幕由 YYeTs 人人影视 原创翻译 “WEB&蓝光版美剧专业户”再加工制作 仅供学习 禁止用于任何商业盈利行为 如需其它剧集字幕 请登陆新浪微博(http://weibo.com/MJZM100) 或发邮件索取 lj20090303@163.com

他们找到我的那座岛 The name of the island they found me on

名为炼狱 is Lian Yu.

汉语意为试炼地狱 It’s mandarin for “Purgatory.”

我在此困居了五年 I’ve been stranded here for 5 years.

每个寒冷黑夜我都期盼着有人前来相救 I’ve dreamt of my rescue every cold black night since then.

这五年里 我只有一个念头一个目标 For 5 years, I have had only one thought, one goal–

活下去 survive…

活下去且有朝一日重返家园 survive and one day return home.

这座岛上危机四伏 The island held many dangers.

为了生存 我不断突破自己 To live, I had to make myself more than what I was,

将自己打造成一柄利器 to forge myself into a weapon.

现在踏上归途的我已不是那个遇难的男孩 I am returning not the boy who was shipwrecked

而是一个为我饱受荼毒的城市 but the man who will bring justice to those

带来正义的男人 who have poisoned my city.

我是奥利弗·奎恩 My name is Oliver Queen.

第一季 第一集

绿 箭

最新新闻 失踪亿万富翁重现人间

奥利弗·奎恩还活着 Oliver Queen is alive.

这位斯特林市居民于五天前 The Starling City resident was found by fishermen

于北中国海 发现奥利弗·奎恩

在北中国海被渔民发现 in the North China Sea 5 days ago,

五年前的海难造成”金牌奎恩”号沉船 5 years after he was missing and presumed dead

发现奥利弗·奎恩

奎恩也因此失踪 following the accident at sea

并被认定为已经死亡 which claimed “The Queen’s Gambit.”

WEBG镜头记录

奎恩曾是八卦小报红人 Queen was a regular tabloid presence

也是斯特林市夜店的熟客 and a fixture at the Starling City club scene.

就在他失踪前夕 Shortly before his disappearance,

他刚从被高度关注的 he was acquitted of assault charges stemming

酒后狗仔争执案的袭击指控中 from a highly publicized drunken altercation

开脱出来 with paparazzi.

发现奥利弗·奎恩

奎恩为本市亿万富翁罗伯特·奎恩之子 Queen is the son of Starling City billionaire Robert Queen,

遇难时罗伯特也在船上 现已正式确认 who was also on board but now officially confirmed

罗伯特·奎恩 1948-2007

其遇难身亡 as deceased.

他身上20%的面积都被疤痕组织覆盖 20% of his body is covered in scar tissue.

背部和手臂均有二级烧伤 Second-degree burns on his back and arms.

X光片显示至少有12处骨折 X-rays show at least 12 fractures

未完好愈合 that never properly healed.

他提过发生的事吗 Has he said anything about what happened?

没有 他只字未提 No. He’s barely said anything.

莫伊拉 我希望你做好心理准备 Moira, I’d like you to prepare yourself.

眼前的他可能不再是你当年失踪的儿子了 The Oliver you lost might not be the one they found.

奥利弗 Oliver.

妈妈 Mom.

我英俊的小伙子 My beautiful boy.

我来吧 I’ve got it.

你的屋子和你离开前一模一样 Your room is exactly as you left it.

我一直不忍去动任何东西 I never had the heart to change a thing.

奥利弗 Oliver.

再见到你真是太高兴了 It’s damn good to see you.

我是沃尔特 It’s Walter…

沃尔特·斯蒂尔 Walter Steele.

你还记得沃尔特吗 你父亲在公司的朋友 You remember Walter, your father’s friend from the company.

很高兴见到你 蕾莎 It’s good to see you, Raisa.

欢迎回家 奥利弗先生 Welcome home, Mr. Oliver.

……

未完待续

文件较大,空耗服务器资源,影响浏览效果,结果文件见于如下百度云盘:

链接:https://pan.baidu.com/s/1HGIe7SN5EHAbFscgPnIsZw
提取码:tpo4


python获取字幕剧本(绿箭侠第一季)
https://blog.wangxk.cc/2019/05/02/python获取字幕剧本(绿箭侠第一季)/
作者
Mike
发布于
2019年5月2日
许可协议