本文整理汇总了Python中pysrt.open方法的典型用法代码示例。如果您正苦于以下问题:Python pysrt.open方法的具体用法?Python pysrt.open怎么用?Python pysrt.open使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pysrt
的用法示例。
在下文中一共展示了pysrt.open方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: extract_labels
# 需要导入模块: import pysrt [as 别名]
# 或者: from pysrt import open [as 别名]
def extract_labels(srt, samples):
subs = pysrt.open(srt)
labels = np.zeros(samples)
for sub in subs:
start = timeToPos(sub.start)
end = timeToPos(sub.end)+1
for i in range(start, end):
if i < len(labels):
labels[i] = 1
return labels
示例2: __init__
# 需要导入模块: import pysrt [as 别名]
# 或者: from pysrt import open [as 别名]
def __init__(self, media, path):
self.media = media
self.path = path
self.subs = pysrt.open(self.path, encoding=self._find_encoding())
示例3: _find_encoding
# 需要导入模块: import pysrt [as 别名]
# 或者: from pysrt import open [as 别名]
def _find_encoding(self):
data = None
with open(self.path, "rb") as f:
data = f.read()
det = chardet.detect(data)
return det.get("encoding")
示例4: write_to_file
# 需要导入模块: import pysrt [as 别名]
# 或者: from pysrt import open [as 别名]
def write_to_file(self, output_path: Opt[str] = None):
util.touch(output_path)
sub_rip_file = pysrt.open(output_path, encoding='utf-8')
for index, sub in enumerate(self.subtitles):
start_time = pysrt.SubRipTime.from_ordinal(sub.start_time * 1000)
end_time = pysrt.SubRipTime.from_ordinal(sub.end_time * 1000)
next_sub = pysrt.SubRipItem(index=index, text=sub.text, start=start_time, end=end_time)
sub_rip_file.append(next_sub)
sub_rip_file.save(output_path, encoding='utf-8')
return output_path
示例5: generateDatasets
# 需要导入模块: import pysrt [as 别名]
# 或者: from pysrt import open [as 别名]
def generateDatasets(train_files, cut_data, len_mfcc, step_mfcc, hop_len, freq):
X, Y = [], []
for tf in train_files:
train_data, labels = generateSingleDataset(tf, cut_data, len_mfcc, step_mfcc, hop_len, freq)
X.append(train_data)
Y.append(labels)
X = np.concatenate(X)
Y = np.concatenate(Y)
if cut_data:
filename = STORE_DIR + 'dataset_CUT_' + str(freq) + '_' + str(hop_len) + '_' + str(len_mfcc) + '_' + str(step_mfcc) + '_' + str(X.shape[0]) + '_' + str(X.shape[1]) + '_' + str(X.shape[2]) + '.pickle'
else:
filename = STORE_DIR + 'dataset_' + str(freq) + '_' + str(hop_len) + '_' + str(len_mfcc) + '_' + str(step_mfcc) + '_' + str(X.shape[0]) + '_' + str(X.shape[1]) + '_' + str(X.shape[2]) + '.pickle'
print filename
with open(filename, 'w') as f:
pickle.dump([X, Y], f)
return X, Y
# Generate a dataset from all available files
示例6: sotreResults
# 需要导入模块: import pysrt [as 别名]
# 或者: from pysrt import open [as 别名]
def sotreResults(results, v):
# %%
import pickle
with open('test_results_'+v+'.pickle', 'w') as f:
pickle.dump(results, f)
# %%
# Plot stored training statistics. Look for the best model
示例7: load_srt
# 需要导入模块: import pysrt [as 别名]
# 或者: from pysrt import open [as 别名]
def load_srt(srt_dir, srt_cache_path):
"""
return: A python dict, the keys are the video names, the entries are lists,
each contains all the text from a .srt file
sub_times are the start time of the sentences.
"""
if os.path.exists(srt_cache_path):
print("Found srt data cache, loading ...")
return load_json(srt_cache_path)
print("Loading srt files from %s ..." % srt_dir)
srt_paths = glob.glob(os.path.join(srt_dir, "*.srt"))
name2sub_text = {}
name2sub_time = {}
for i in tqdm(range(len(srt_paths))):
subs = pysrt.open(srt_paths[i], encoding="iso-8859-1")
if len(subs) == 0:
subs = pysrt.open(srt_paths[i])
text_list = []
sub_time_list = []
for j in range(len(subs)):
cur_sub = subs[j]
cur_str = cur_sub.text
cur_str = "(<UNKNAME>:)" + cur_str if cur_str[0] != "(" else cur_str
cur_str = cur_str.replace("\n", " ")
text_list.append(cur_str)
sub_time_list.append(
60 * cur_sub.start.minutes + cur_sub.start.seconds + 0.001 * cur_sub.start.milliseconds)
key_str = os.path.splitext(os.path.basename(srt_paths[i]))[0]
name2sub_text[key_str] = text_list
name2sub_time[key_str] = sub_time_list
srt_data = {"sub_text": name2sub_text, "sub_time": name2sub_time}
save_json(srt_data, srt_cache_path)
return srt_data
示例8: find_summary_regions
# 需要导入模块: import pysrt [as 别名]
# 或者: from pysrt import open [as 别名]
def find_summary_regions(srt_filename, duration=30, language="english"):
""" Find important sections
Args:
srt_filename(str): Name of the SRT FILE
duration(int): Time duration
language(str): Language of subtitles (default to English)
Returns:
list: segment of subtitles as "summary"
"""
srt_file = pysrt.open(srt_filename)
enc = chardet.detect(open(srt_filename, "rb").read())['encoding']
srt_file = pysrt.open(srt_filename, encoding=enc)
# generate average subtitle duration
subtitle_duration = time_regions(
map(srt_segment_to_range, srt_file)) / len(srt_file)
# compute number of sentences in the summary file
n_sentences = duration / subtitle_duration
summary = summarize(srt_file, n_sentences, language)
total_time = time_regions(summary)
too_short = total_time < duration
if too_short:
while total_time < duration:
n_sentences += 1
summary = summarize(srt_file, n_sentences, language)
total_time = time_regions(summary)
else:
while total_time > duration:
n_sentences -= 1
summary = summarize(srt_file, n_sentences, language)
total_time = time_regions(summary)
return summary
示例9: parseSrtAndSetItems
# 需要导入模块: import pysrt [as 别名]
# 或者: from pysrt import open [as 别名]
def parseSrtAndSetItems(self):
try:
self.items = pysrt.open(self.filePath, encoding='utf8')
return 0
except:
return 11