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

ttaf2srt.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 - Palzisch 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. with open(sys.argv[1]) as f:
  47. xmldoc = f.read().replace('\n', ' ').replace('\r', '')
  48. xmldoc = minidom.parseString(xmldoc)
  49. header = xmldoc.getElementsByTagName('tt:head')
  50. if len(header):
  51. styling = header[0].getElementsByTagName('tt:styling')
  52. if len(styling):
  53. styles = parseStyles(styling[0].getElementsByTagName('tt:style'))
  54. body = xmldoc.getElementsByTagName('tt:body')
  55. itemlist = body[0].getElementsByTagName('tt:p')
  56. subCount = 0
  57. for item in itemlist:
  58. if item.hasAttribute('xml:id'):
  59. dumpHeader(item, subCount)
  60. subCount += 1
  61. color = styles[item.getAttribute("style")]
  62. if color:
  63. print("<font color=\"" + color + "\">", end="")
  64. dumpText(item)
  65. if color:
  66. print("</font>", end="")
  67. print("\n")