formatters.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """
  2. Defines subtitle formatters used by autosub.s
  3. """
  4. # -*- coding: utf-8 -*-
  5. from __future__ import unicode_literals
  6. import json
  7. import pysrt
  8. import six
  9. def srt_formatter(subtitles, padding_before=0, padding_after=0):
  10. """
  11. Serialize a list of subtitles according to the SRT format, with optional time padding.
  12. """
  13. sub_rip_file = pysrt.SubRipFile()
  14. for i, ((start, end), text) in enumerate(subtitles, start=1):
  15. item = pysrt.SubRipItem()
  16. item.index = i
  17. item.text = six.text_type(text)
  18. item.start.seconds = max(0, start - padding_before)
  19. item.end.seconds = end + padding_after
  20. sub_rip_file.append(item)
  21. return '\n'.join(six.text_type(item) for item in sub_rip_file)
  22. def vtt_formatter(subtitles, padding_before=0, padding_after=0):
  23. """
  24. Serialize a list of subtitles according to the VTT format, with optional time padding.
  25. """
  26. text = srt_formatter(subtitles, padding_before, padding_after)
  27. text = 'WEBVTT\n\n' + text.replace(',', '.')
  28. return text
  29. def json_formatter(subtitles):
  30. """
  31. Serialize a list of subtitles as a JSON blob.
  32. """
  33. subtitle_dicts = [
  34. {
  35. 'start': start,
  36. 'end': end,
  37. 'content': text,
  38. }
  39. for ((start, end), text)
  40. in subtitles
  41. ]
  42. return json.dumps(subtitle_dicts)
  43. def raw_formatter(subtitles):
  44. """
  45. Serialize a list of subtitles as a newline-delimited string.
  46. """
  47. return ' '.join(text for (_rng, text) in subtitles)
  48. FORMATTERS = {
  49. 'srt': srt_formatter,
  50. 'vtt': vtt_formatter,
  51. 'json': json_formatter,
  52. 'raw': raw_formatter,
  53. }