本文整理汇总了Python中pysrt.SubRipFile.from_string方法的典型用法代码示例。如果您正苦于以下问题:Python SubRipFile.from_string方法的具体用法?Python SubRipFile.from_string怎么用?Python SubRipFile.from_string使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pysrt.SubRipFile
的用法示例。
在下文中一共展示了SubRipFile.from_string方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: convert
# 需要导入模块: from pysrt import SubRipFile [as 别名]
# 或者: from pysrt.SubRipFile import from_string [as 别名]
def convert(content, input_format, output_format):
"""
Convert transcript `content` from `input_format` to `output_format`.
Accepted input formats: sjson, srt.
Accepted output format: srt, txt.
"""
assert input_format in ('srt', 'sjson')
assert output_format in ('txt', 'srt', 'sjson')
if input_format == output_format:
return content
if input_format == 'srt':
if output_format == 'txt':
text = SubRipFile.from_string(content.decode('utf8')).text
return HTMLParser().unescape(text)
elif output_format == 'sjson':
raise NotImplementedError
if input_format == 'sjson':
if output_format == 'txt':
text = json.loads(content)['text']
return HTMLParser().unescape("\n".join(text))
elif output_format == 'srt':
return generate_srt_from_sjson(json.loads(content), speed=1.0)
示例2: test_windows1252
# 需要导入模块: from pysrt import SubRipFile [as 别名]
# 或者: from pysrt.SubRipFile import from_string [as 别名]
def test_windows1252(self):
srt_string = codecs.open(self.windows_path, encoding='windows-1252').read()
srt_file = SubRipFile.from_string(srt_string, encoding='windows-1252', eol='\r\n')
self.assertEquals(len(srt_file), 1332)
self.assertEquals(srt_file.eol, '\r\n')
self.assertRaises(UnicodeDecodeError, SubRipFile.open,
self.utf8_path, encoding='ascii')
示例3: convert
# 需要导入模块: from pysrt import SubRipFile [as 别名]
# 或者: from pysrt.SubRipFile import from_string [as 别名]
def convert(content, input_format, output_format):
"""
Convert transcript `content` from `input_format` to `output_format`.
Accepted input formats: sjson, srt.
Accepted output format: srt, txt, sjson.
Raises:
TranscriptsGenerationException: On parsing the invalid srt content during conversion from srt to sjson.
"""
assert input_format in ('srt', 'sjson')
assert output_format in ('txt', 'srt', 'sjson')
if input_format == output_format:
return content
if input_format == 'srt':
if output_format == 'txt':
text = SubRipFile.from_string(content.decode('utf8')).text
return HTMLParser().unescape(text)
elif output_format == 'sjson':
try:
# With error handling (set to 'ERROR_RAISE'), we will be getting
# the exception if something went wrong in parsing the transcript.
srt_subs = SubRipFile.from_string(
# Skip byte order mark(BOM) character
content.decode('utf-8-sig'),
error_handling=SubRipFile.ERROR_RAISE
)
except Error as ex: # Base exception from pysrt
raise TranscriptsGenerationException(text_type(ex))
return json.dumps(generate_sjson_from_srt(srt_subs))
if input_format == 'sjson':
if output_format == 'txt':
text = json.loads(content)['text']
text_without_none = [line if line else '' for line in text]
return HTMLParser().unescape("\n".join(text_without_none))
elif output_format == 'srt':
return generate_srt_from_sjson(json.loads(content), speed=1.0)
示例4: GetSrtCaptions
# 需要导入模块: from pysrt import SubRipFile [as 别名]
# 或者: from pysrt.SubRipFile import from_string [as 别名]
def GetSrtCaptions(self):
"""Retrieves and parses the actual ASR captions track's data.
Given the URL of an ASR captions track, this retrieves it in the SRT format
and uses the pysrt library to parse it into a format we can manipulate.
Raises:
Error: The ASR caption track could not be retrieved.
"""
response_headers, body = self.http.request("%s?fmt=srt" % self.track_url, "GET", headers=self.headers)
if response_headers["status"] == "200":
self.srt_captions = SubRipFile.from_string(body)
else:
raise Error("Received HTTP response %s when requesting %s?fmt=srt." % (response_headers["status"], self.track_url))
示例5: generate_subs_from_source
# 需要导入模块: from pysrt import SubRipFile [as 别名]
# 或者: from pysrt.SubRipFile import from_string [as 别名]
def generate_subs_from_source(speed_subs, subs_type, subs_filedata, item, language='en'):
"""Generate transcripts from source files (like SubRip format, etc.)
and save them to assets for `item` module.
We expect, that speed of source subs equal to 1
:param speed_subs: dictionary {speed: sub_id, ...}
:param subs_type: type of source subs: "srt", ...
:param subs_filedata:unicode, content of source subs.
:param item: module object.
:param language: str, language of translation of transcripts
:returns: True, if all subs are generated and saved successfully.
"""
_ = item.runtime.service(item, "i18n").ugettext
if subs_type.lower() != 'srt':
raise TranscriptsGenerationException(_("We support only SubRip (*.srt) transcripts format."))
try:
srt_subs_obj = SubRipFile.from_string(subs_filedata)
except Exception as ex:
msg = _("Something wrong with SubRip transcripts file during parsing. Inner message is {error_message}").format(
error_message=ex.message
)
raise TranscriptsGenerationException(msg)
if not srt_subs_obj:
raise TranscriptsGenerationException(_("Something wrong with SubRip transcripts file during parsing."))
sub_starts = []
sub_ends = []
sub_texts = []
for sub in srt_subs_obj:
sub_starts.append(sub.start.ordinal)
sub_ends.append(sub.end.ordinal)
sub_texts.append(sub.text.replace('\n', ' '))
subs = {
'start': sub_starts,
'end': sub_ends,
'text': sub_texts}
for speed, subs_id in speed_subs.iteritems():
save_subs_to_store(
generate_subs(speed, 1, subs),
subs_id,
item,
language
)
return subs
示例6: get_transcript_format
# 需要导入模块: from pysrt import SubRipFile [as 别名]
# 或者: from pysrt.SubRipFile import from_string [as 别名]
def get_transcript_format(transcript_content):
"""
Returns transcript format.
Arguments:
transcript_content (str): Transcript file content.
"""
try:
sjson_obj = json.loads(transcript_content)
except ValueError:
# With error handling (set to 'ERROR_RAISE'), we will be getting
# the exception if something went wrong in parsing the transcript.
srt_subs = SubRipFile.from_string(transcript_content, error_handling=SubRipFile.ERROR_RAISE)
if len(srt_subs) > 0:
return TranscriptFormat.SRT
return TranscriptFormat.SJSON
示例7: generate_subs_from_source
# 需要导入模块: from pysrt import SubRipFile [as 别名]
# 或者: from pysrt.SubRipFile import from_string [as 别名]
def generate_subs_from_source(speed_subs, subs_type, subs_filedata, item):
"""Generate transcripts from source files (like SubRip format, etc.)
and save them to assets for `item` module.
We expect, that speed of source subs equal to 1
:param speed_subs: dictionary {speed: sub_id, ...}
:param subs_type: type of source subs: "srt", ...
:param subs_filedata:unicode, content of source subs.
:param item: module object.
:returns: True, if all subs are generated and saved successfully.
"""
if subs_type != 'srt':
raise TranscriptsGenerationException("We support only SubRip (*.srt) transcripts format.")
try:
srt_subs_obj = SubRipFile.from_string(subs_filedata)
except Exception as e:
raise TranscriptsGenerationException(
"Something wrong with SubRip transcripts file during parsing. "
"Inner message is {}".format(e.message)
)
if not srt_subs_obj:
raise TranscriptsGenerationException("Something wrong with SubRip transcripts file during parsing.")
sub_starts = []
sub_ends = []
sub_texts = []
for sub in srt_subs_obj:
sub_starts.append(sub.start.ordinal)
sub_ends.append(sub.end.ordinal)
sub_texts.append(sub.text.replace('\n', ' '))
subs = {
'start': sub_starts,
'end': sub_ends,
'text': sub_texts}
for speed, subs_id in speed_subs.iteritems():
save_subs_to_store(
generate_subs(speed, 1, subs),
subs_id,
item
)
return subs
示例8: get_srt_data
# 需要导入模块: from pysrt import SubRipFile [as 别名]
# 或者: from pysrt.SubRipFile import from_string [as 别名]
def get_srt_data(source):
captions = SubRipFile.from_string(source.srt_data)
for c in captions:
start = c.start.to_time()
end = c.end.to_time()
offset = start.second + (start.minute * 60) + (start.hour * 60 * 60) + (start.microsecond / 1000000) #it can't possibly be more than hours.
end_offset = end.second + (end.minute * 60) + (end.hour * 60 * 60) + (end.microsecond / 1000000)
note, created = Note.objects.get_or_create(
text = c.text,
offset = end_offset,
#end_offset = end_offset,
user = source.user,
user_name = source.user.username,
video = source.video,
private = False,
import_source = source,
import_source_name = source.name,
source = 'SRT File',
original_source = 'SRT File',
source_link = source.url, #they're probably not going to have one of these...
type = "caption"
)
示例9: test_compare_from_string_and_from_path
# 需要导入模块: from pysrt import SubRipFile [as 别名]
# 或者: from pysrt.SubRipFile import from_string [as 别名]
def test_compare_from_string_and_from_path(self):
unicode_content = codecs.open(self.utf8_path, encoding="utf_8").read()
iterator = izip(SubRipFile.open(self.utf8_path), SubRipFile.from_string(unicode_content))
for file_item, string_item in iterator:
self.assertEquals(unicode(file_item), unicode(string_item))
示例10: test_utf8
# 需要导入模块: from pysrt import SubRipFile [as 别名]
# 或者: from pysrt.SubRipFile import from_string [as 别名]
def test_utf8(self):
unicode_content = codecs.open(self.utf8_path, encoding="utf_8").read()
self.assertEquals(len(SubRipFile.from_string(unicode_content)), 1332)
self.assertRaises(UnicodeDecodeError, SubRipFile.from_string, open(self.windows_path).read())