123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- """
- Defines subtitle formatters used by autosub.s
- """
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals
- import json
- import pysrt
- import six
- def srt_formatter(subtitles, padding_before=0, padding_after=0):
- """
- Serialize a list of subtitles according to the SRT format, with optional time padding.
- """
- sub_rip_file = pysrt.SubRipFile()
- for i, ((start, end), text) in enumerate(subtitles, start=1):
- item = pysrt.SubRipItem()
- item.index = i
- item.text = six.text_type(text)
- item.start.seconds = max(0, start - padding_before)
- item.end.seconds = end + padding_after
- sub_rip_file.append(item)
- return '\n'.join(six.text_type(item) for item in sub_rip_file)
- def vtt_formatter(subtitles, padding_before=0, padding_after=0):
- """
- Serialize a list of subtitles according to the VTT format, with optional time padding.
- """
- text = srt_formatter(subtitles, padding_before, padding_after)
- text = 'WEBVTT\n\n' + text.replace(',', '.')
- return text
- def json_formatter(subtitles):
- """
- Serialize a list of subtitles as a JSON blob.
- """
- subtitle_dicts = [
- {
- 'start': start,
- 'end': end,
- 'content': text,
- }
- for ((start, end), text)
- in subtitles
- ]
- return json.dumps(subtitle_dicts)
- def raw_formatter(subtitles):
- """
- Serialize a list of subtitles as a newline-delimited string.
- """
- return ' '.join(text for (_rng, text) in subtitles)
- FORMATTERS = {
- 'srt': srt_formatter,
- 'vtt': vtt_formatter,
- 'json': json_formatter,
- 'raw': raw_formatter,
- }
|