Srt2html
From ActiveArchives
Script to convert an srt to wrapped div titles with data-start, data-end attributes.
Usage
python srt2html titles.srt
Source
#!/usr/bin/env python #-*- coding:utf-8 -*- import re srt_timecodes = re.compile( r"""^ (^ ((?P<seq>\d+)\r?\n)? (?P<start> ((\d\d):)? (\d\d): (\d\d) ([,.]\d{1,3})?) \s* --> \s* (?P<end> ((\d\d):)? (\d\d): (\d\d) ([,.]\d{1,3})?)? \s*) $""", re.X|re.M ) def spliterator (pattern, text): """ Utility function for splitting on a "header" pattern yields (match, text), where text is that text between match and the next match """ lastm = None cur = 0 for m in pattern.finditer(text): if lastm: yield lastm, text[cur:m.start()] lastm = m cur = m.end() if lastm: yield m, text[cur:] def process (text): for m, body in spliterator(srt_timecodes, text): mvars = m.groupdict() start = mvars.get("start") end = mvars.get("end") print '<div class="title" data-start="{}" data-end="{}">'.format(start, end) print body.strip().encode("utf-8") print '</div>\n' if __name__ == "__main__": import sys, codecs text = codecs.open(sys.argv[1], "r", "utf-8").read() process(text)
Example
Input:
00:02:21,766 --> 00:03:19,393 I have to begin with many thanks to Femke and Laurence, because it really has been a great pleasure for me to have been here this weekend. It’s nearly five years since I came to an event like this, believe it or not, and I really cannot say enough how much I have enjoyed it, and how stimulating I have found it. So yes, a big thank you to both for getting me here. And as you say, it’s ten years since I wrote Zeros + Ones, and you are marking ten years of this festival too, so it’s an interesting moment to think about a lot of the issues that have come up over the weekend. This is a more or less spontaneous report, very much an ‘open performance’, to use Simon Yuill’s words, and not to be taken as any kind of definitive account of what has happened this weekend. But still I hope it can bring a few of the many and varied strands of this event together, not to form a true conclusion, but perhaps to provide some kind of digestif after a wonderful meal. 00:03:19,393 --> 00:03:48,937 I thought I should begin as Femke very wisely began, with the theme of cooking. Femke gave us a recipe at the beginning of the weekend, really a kind of recipe for the whole event, with cooking as an example of the fact that there are many models, many activities, many things that we do in our everyday lives, which might inform and expand our ideas about technology and how we work with them. 00:03:48,937 --> 00:04:19,317 So, I too will begin with this idea of cooking, which is as Femke said a very magical, transformative experience. Femke’s clip from the Cathérine Deneuve film was a really lovely instance of the kind of deep elemental, magical chemistry which goes on in cooking.
Output:
<div class="title" data-start="00:02:21,766" data-end="00:03:19,393"> I have to begin with many thanks to Femke and Laurence, because it really has been a great pleasure for me to have been here this weekend. It’s nearly five years since I came to an event like this, believe it or not, and I really cannot say enough how much I have enjoyed it, and how stimulating I have found it. So yes, a big thank you to both for getting me here. And as you say, it’s ten years since I wrote Zeros + Ones, and you are marking ten years of this festival too, so it’s an interesting moment to think about a lot of the issues that have come up over the weekend. This is a more or less spontaneous report, very much an ‘open performance’, to use Simon Yuill’s words, and not to be taken as any kind of definitive account of what has happened this weekend. But still I hope it can bring a few of the many and varied strands of this event together, not to form a true conclusion, but perhaps to provide some kind of digestif after a wonderful meal. </div> <div class="title" data-start="00:03:19,393" data-end="00:03:48,937"> I thought I should begin as Femke very wisely began, with the theme of cooking. Femke gave us a recipe at the beginning of the weekend, really a kind of recipe for the whole event, with cooking as an example of the fact that there are many models, many activities, many things that we do in our everyday lives, which might inform and expand our ideas about technology and how we work with them. </div> <div class="title" data-start="00:03:48,937" data-end="00:04:19,317"> So, I too will begin with this idea of cooking, which is as Femke said a very magical, transformative experience. Femke’s clip from the Cathérine Deneuve film was a really lovely instance of the kind of deep elemental, magical chemistry which goes on in cooking. </div>