Enigma2 plugin to to play various online streams (mostly Latvian).

ttaf.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python3
  2. """
  3. Usage:
  4. ttaf2srt subtitlefilettafinput.xml > output.srt
  5. From https://github.com/haraldF/ttaf2srt
  6. edited for 'SWR - Pälzisch im Abgang' subtitles
  7. www.swr.de/paelzisch-im-abgang/
  8. and 'Tatort' subtitles.
  9. """
  10. """
  11. From https://github.com/haraldF/ttaf2srt
  12. ttaf2srt
  13. Simple python script to convert ttaf subtitles to srt subtitles.
  14. Note - only tested on German 'Tatort' subtitles.
  15. Note2 - if using vlc or mplayer, make sure to specify 'utf8' as encoding, otherwise, special characters will not render correctly.
  16. """
  17. import sys
  18. from xml.dom import minidom
  19. def dumpText(item):
  20. for child in item.childNodes:
  21. if child.nodeType == child.TEXT_NODE:
  22. print(child.nodeValue, end="")
  23. elif child.nodeType == child.ELEMENT_NODE:
  24. if child.nodeName == "tt:br":
  25. print()
  26. elif child.nodeName == "tt:span":
  27. print("<font color=\"" + styles[child.getAttribute("style")] + "\">", end="")
  28. dumpText(child)
  29. print("</font>", end="")
  30. else:
  31. print("Unknown Node: " + child.nodeName, file=sys.stderr)
  32. def dumpHeader(item, subCount):
  33. print(subCount)
  34. begin = item.getAttribute("begin")
  35. end = item.getAttribute("end")
  36. # ### this is a silly hack - for some reason, my ttaf files all start at hour 10? Resetting
  37. # the hour makes it work again
  38. begin = '0' + begin[1:]
  39. end = '0' + end[1:]
  40. print(begin + " --> " + end)
  41. def parseStyles(styles):
  42. result = {}
  43. for style in styles:
  44. result[style.getAttribute('xml:id')] = style.getAttribute('tts:color')
  45. return result
  46. def ttaf2srt(fname):
  47. with open(fname) as f:
  48. xmldoc = f.read().replace('\n', ' ').replace('\r', '')
  49. xmldoc = minidom.parseString(xmldoc)
  50. header = xmldoc.getElementsByTagName('tt:head')
  51. if len(header):
  52. styling = header[0].getElementsByTagName('tt:styling')
  53. if len(styling):
  54. styles = parseStyles(styling[0].getElementsByTagName('tt:style'))
  55. body = xmldoc.getElementsByTagName('tt:body')
  56. itemlist = body[0].getElementsByTagName('tt:p')
  57. subCount = 0
  58. for item in itemlist:
  59. if item.hasAttribute('xml:id'):
  60. dumpHeader(item, subCount)
  61. subCount += 1
  62. color = styles[item.getAttribute("style")]
  63. if color:
  64. print("<font color=\"" + color + "\">", end="")
  65. dumpText(item)
  66. if color:
  67. print("</font>", end="")
  68. print("\n")