之前因为有学习英语的需求,看了一些美剧。但由于平常时间有限,所以将美剧视频压缩成了mp3,然后顺便将srt字幕转换成了lrc。
github上参考了 一个 srt转 lrc 的 python脚本。
楼主将自己完善的python字幕转换脚本 展示如下:
#coding=utf-8
# 如果要在python2的py文件里面写中文,则必须要添加一行声明文件编码的注释,否则python2会默认使用ASCII编码。
'''
Run it in srt file folder.
python 2.7
'''
import glob
import re
SRT_BLOCK_REGEX = re.compile(
r'(\d+)[\r\n]+' # srt --> num
r'(\d{2}:\d{2}:\d{2},\d{2,4})[\s]*-->[\s]*(\d{2}:\d{2}:\d{2},\d{2,4})[\r\n]+' # srt --> time
# r'(.+)'
r'(.*)[\r\n]+' # srt --> subtitles
r'(.*)' # srt --> subtitles ( sometimes srt has 2 subtitles )
)
# [^\S\r\n]*[\r\n]+
def srt_block_to_irc(block):
match = SRT_BLOCK_REGEX.search(block)
"""
IndentationError: unindent does not match any outer indentation level
///////////////////////////////////// python 的 注释 居然 也要 对齐的 我去
Traceback (most recent call last):
File "srt2lrc.py", line 100, in <module>
srt_file_to_irc(file_name)
File "srt2lrc.py", line 50, in srt_file_to_irc
blocks_out = [srt_block_to_irc(block) for block in blocks_in]
File "srt2lrc.py", line 24, in srt_block_to_irc
print('match groups 0: %s' %(match.group(0)) )
AttributeError: 'NoneType' object has no attribute 'group'
因为 下面的 print 函数 有问题 啊,每一次 调用 上面的 search(block) 有可能 出现 不匹配,于是 会传给 match = none
"""
if not match:
return None
#else:
# print('match groups 0: %s' %(match.group(0)) )
num, t1, t2, content1, content2 = match.groups()
# 匹配 每一个 正则表达式 单元 ---> 就是 正则表达式 里面 加了 括号的部分 时间部分 有11位 ,t1[3:11] 就是 截取 t[3] 到 不包括最后的 t1[11].其实最大是t[10],t[11]越界不存在的
"""
print( 'hell' )
print('match num 0: %s' %(num) )
print('match t1 0: %s' %( t1 ) )
print('match t2 0: %s' %( t2 ) )
print('match content1 0: %s' %(content1 ) )
print('match content2 0: %s' %(content2) )
"""
# ts = ts[3:-1].replace(',', '.')
# te = te[3:-1].replace(',', '.')
# co = content.replace('\n', ' ')
# python里面的索引的特征是包含起点,但是不包含结束的索引值,-1表示最后一个元素,但是-1是结尾的index,所以含义就是取 第三个元素 到 最后一个元素
# t1 = t1.split(',')[0] # 分裂后 取 第一个字符串
# t2 = t2.split(',')[0]
# print('match t1 0: %s' %( t1[10] ) )
t1 = t1[3:11].replace(',', '.')
# t2 = t2[3:-1].replace(',', '.')
# print('match t1 replace: %s' %( t1 ) )
# return '[%s]%s\n[%s]\n' % (ts, co, te)
return '[%s]%s%s\n\n' % (t1, content1, content2)
def srt_file_to_irc(fname):
# python 3
# with open(fname, encoding='UTF-8') as file_in:
# str_in = file_in.read()
with open(fname, 'rb') as file_in:
str_in = file_in.read().decode("utf-8") # the original srt file encode with utf-8 , so this place we decode it with utf-8
blocks_in = str_in.replace('\r\n', '\n').split('\n\n')
blocks_out = [srt_block_to_irc(block) for block in blocks_in]
# print('all blocks_out: \n %s' % (blocks_out) )
if not all(blocks_out):
err_info.append((fname, blocks_out.index(None), blocks_in[blocks_out.index(None)]))
blocks_out = filter(None, blocks_out)
str_out = ''.join(blocks_out)
# 卧槽 python 用 对齐方式 判断 作用域的 因为 有了中文注释,源文件是 ascii编码的。所以报错 说 出现 非ASCII字符
# SyntaxError: Non-ASCII character '\xe5' in file Test1.py on line 8, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
# 解决方法 python的默认编码文件是用的ASCII码,将文件存成了UTF-8,编译就可以通过。或在在py文件开头(必须是第一行)加入
# python3
# with io.open(fname.replace('srt', 'lrc'), 'w', encoding='UTF-8') as file_out:
# file_out.write(str_out )
with open(fname.replace('srt', 'lrc'), 'w') as file_out:
file_out.write(str_out.encode('utf-8'))
"""
Python–方法:
1.根据长度判断
长度为0时,表示空。(其中”判断条件”成立时(非零),则执行后面的语句)
lst = []
if len(lst):
print 'c'
else:
print 'cc'
2.根据逻辑判断
由于一个空 list 本身等同于 False
lst = []
if lst:
print 'c'
else:
print 'cc'
"""
if __name__ == '__main__':
err_info = []
for file_name in glob.glob('*.srt'):
srt_file_to_irc(file_name)
if err_info:
print('success, but some exceptions are ignored:')
for file_name, blocks_num, context in err_info:
print('\tfile: %s, block num: %s, context: %s' % (file_name, blocks_num, context))
else:
print('success')
只需将 此 python 脚本,放入待转换的 srt字幕 文件夹里面就行。
然后 命令行 运行该 python 脚本就行。可以 修改 python脚本 的正则表达式,修改srt 解码格式。
脚本文件下载
