Guest User

Untitled

a guest
Nov 16th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. import re
  5. import sys
  6. import json
  7.  
  8. def parseTime(timeFormat):
  9. timeFormat = timeFormat.replace(",", ".", 1)
  10. array = timeFormat.split(":")
  11. retVal = 0
  12. i = 0
  13. for j in range(len(array)-1, -1, -1):
  14. retVal = retVal + float(array[j]) * pow(60, i)
  15. i += 1
  16. return retVal
  17.  
  18. def main():
  19. with open(sys.argv[1], "r") as srt_file:
  20. srt_text = srt_file.read()
  21.  
  22. srt_lines = re.findall("[0-9]*\n[0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3} --> [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}\n(?s).*?(?=\n{2,}|$)", srt_text)
  23. bcc_lines = []
  24.  
  25. for srt_line in srt_lines:
  26. bcc_line = {"from": 0, "to": 0, "location": 2, "content": ""}
  27. temp = srt_line.split('\n', 2)
  28. bcc_line["content"] = temp[2]
  29. temp = temp[1].split(' --> ', 1)
  30. bcc_line["from"] = parseTime(temp[0])
  31. bcc_line["to"] = parseTime(temp[1])
  32. bcc_lines.append(bcc_line)
  33.  
  34. bcc_text = {
  35. "font_size": 0.4,
  36. "font_color": "#FFFFFF",
  37. "background_alpha": 0.5,
  38. "background_color": "#9C27B0",
  39. "Stroke": "none",
  40. "body": bcc_lines
  41. }
  42. bcc_text = json.dumps(bcc_text, ensure_ascii=False)
  43.  
  44. if sys.argv[1].count(".srt") == 1:
  45. bcc_filename = sys.argv[1].replace(".srt", ".bcc", 1)
  46. else:
  47. bcc_filename = sys.argv[1] + ".bcc"
  48. with open(bcc_filename, "w+") as bcc_file:
  49. bcc_file.write(bcc_text)
  50.  
  51. if __name__ == "__main__":
  52. main()
Add Comment
Please, Sign In to add comment