本文整理汇总了Python中pysrt.SubRipFile.save方法的典型用法代码示例。如果您正苦于以下问题:Python SubRipFile.save方法的具体用法?Python SubRipFile.save怎么用?Python SubRipFile.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pysrt.SubRipFile
的用法示例。
在下文中一共展示了SubRipFile.save方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: to_srt
# 需要导入模块: from pysrt import SubRipFile [as 别名]
# 或者: from pysrt.SubRipFile import save [as 别名]
def to_srt(df, filename):
out = SubRipFile(encoding='utf-8')
for i, r in df.iterrows():
begin = convert_time(r['begin'])
end = convert_time(r['end'])
out.append(SubRipItem(0, begin, end, r['text']))
out.save(filename)
示例2: get_captions
# 需要导入模块: from pysrt import SubRipFile [as 别名]
# 或者: from pysrt.SubRipFile import save [as 别名]
def get_captions(client_name, clip_id):
h = httplib2.Http()
g_url = 'http://%s/JSON.php?clip_id=%s' % ( client_name, clip_id)
print "Fetching URL: %s" % g_url
try:
response, j = h.request(g_url)
except httplib.BadStatusLine as exception:
return None
dirname = os.getcwd() + "/data/granicus/srt/%s/" % client_name
filename = dirname + "%s.srt" % clip_id
subs = SubRipFile()
if response.get('status') == '200':
captions = []
try:
j = json.loads(j, strict=False)[0]
except ValueError:
ts = re.sub('([{,]\s+)([a-z]+)(: ")', lambda s: '%s"%s"%s' % (s.groups()[0], s.groups()[1], s.groups()[2]), j).replace("\\", "")
try:
j = json.loads(ts, strict=False)[0]
except UnicodeDecodeError:
ts = unicode(ts, errors='ignore')
j = json.loads(ts, strict=False)[0]
except:
j = False
sub_count = 0
for item in j:
if item["type"] == "text":
cap = item["text"]
offset = round(float(item["time"]), 3)
captions.append({'time': offset, 'text': cap})
end = get_cap_end(j, sub_count)
if end:
subtitle = SubRipItem(index=sub_count, start=SubRipTime(seconds=offset), end=SubRipTime(seconds=end), text=cap)
subs.append(subtitle)
sub_count = sub_count + 1
try:
subs.save(path=filename, encoding="utf-8")
except IOError:
p = subprocess.Popen('mkdir -p %s' % dirname, shell=True, stdout=subprocess.PIPE)
t = p.wait()
subs.save(path=filename, encoding="utf-8")
s3_url = push_to_s3(filename, '%s/%s.srt' % (client_name, clip_id))
return (captions, s3_url)
else:
return ([], '')
示例3: save
# 需要导入模块: from pysrt import SubRipFile [as 别名]
# 或者: from pysrt.SubRipFile import save [as 别名]
def save(self, path):
if path.endswith('srt'):
verify_dependencies(['pysrt'])
from pysrt import SubRipFile, SubRipItem
from datetime import time
out = SubRipFile()
for elem in self._elements:
start = time(*self._to_tup(elem.onset))
end = time(*self._to_tup(elem.onset + elem.duration))
out.append(SubRipItem(0, start, end, elem.text))
out.save(path)
else:
with open(path, 'w') as f:
f.write('onset\ttext\tduration\n')
for elem in self._elements:
f.write('{}\t{}\t{}\n'.format(elem.onset,
elem.text,
elem.duration))
示例4: SubRipFile
# 需要导入模块: from pysrt import SubRipFile [as 别名]
# 或者: from pysrt.SubRipFile import save [as 别名]
srt_log_path = current_directory + '/comment_log/' + chat_channel + '.srt'
bot = irc_bot.irc_bot(username, oauth, chat_channel, chat_server[0], chat_server[1], twitchclient_version = twitchclient_version)
outsrt = SubRipFile()
text = ''
while 1:
raw_msg_list = bot.get_message()
if len(raw_msg_list) > 0:
if len(text) > 0:
end = SubRipTime.from_time(datetime.now())
item = SubRipItem(0, start, end, text)
outsrt.append(item)
start = SubRipTime.from_time(datetime.now())
text = ''
timestamp = get_timestamp(timestamp_format)
for item in raw_msg_list:
if record_raw:
log_add(raw_log_path, timestamp + ' ' + item + '\n')
username, message = irc_bot.parse_user(item)
if username != '':
safe_print(chat_channel + " " + username + ": " + message)
log_add(log_path, timestamp + ' ' + username + ': ' + message + '\n')
text += username + ": " + message + '\n'
outsrt.clean_indexes()
outsrt.save(srt_log_path, encoding='utf-8')