Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- import re
- import sys
- import json
- def parseTime(timeFormat):
- timeFormat = timeFormat.replace(",", ".", 1)
- array = timeFormat.split(":")
- retVal = 0
- i = 0
- for j in range(len(array)-1, -1, -1):
- retVal = retVal + float(array[j]) * pow(60, i)
- i += 1
- return retVal
- def main():
- with open(sys.argv[1], "r") as srt_file:
- srt_text = srt_file.read()
- 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)
- bcc_lines = []
- for srt_line in srt_lines:
- bcc_line = {"from": 0, "to": 0, "location": 2, "content": ""}
- temp = srt_line.split('\n', 2)
- bcc_line["content"] = temp[2]
- temp = temp[1].split(' --> ', 1)
- bcc_line["from"] = parseTime(temp[0])
- bcc_line["to"] = parseTime(temp[1])
- bcc_lines.append(bcc_line)
- bcc_text = {
- "font_size": 0.4,
- "font_color": "#FFFFFF",
- "background_alpha": 0.5,
- "background_color": "#9C27B0",
- "Stroke": "none",
- "body": bcc_lines
- }
- bcc_text = json.dumps(bcc_text, ensure_ascii=False)
- if sys.argv[1].count(".srt") == 1:
- bcc_filename = sys.argv[1].replace(".srt", ".bcc", 1)
- else:
- bcc_filename = sys.argv[1] + ".bcc"
- with open(bcc_filename, "w+") as bcc_file:
- bcc_file.write(bcc_text)
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment