python
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
python [2010/10/23 23:16] – ginko | python [2014/09/13 14:03] (current) – [Recipes] ginko | ||
---|---|---|---|
Line 110: | Line 110: | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
+ | * [[http:// | ||
+ | # -*- encoding: | ||
+ | |||
+ | """ | ||
+ | |||
+ | Takes google' | ||
+ | |||
+ | NB: to get google' | ||
+ | http:// | ||
+ | """ | ||
+ | |||
+ | # srt example | ||
+ | """ | ||
+ | 00: | ||
+ | Entre l’Australia et la South America, | ||
+ | dans l’Océan South Pacific…""" | ||
+ | |||
+ | # Google' | ||
+ | """<? | ||
+ | < | ||
+ | <text start=" | ||
+ | This is a matter of National Security.</ | ||
+ | |||
+ | import re, sys | ||
+ | |||
+ | # Pattern to identify a subtitle and grab start, duration and text. | ||
+ | pat = re.compile(r'<? | ||
+ | |||
+ | def parseLine(text): | ||
+ | """ | ||
+ | m = re.match(pat, | ||
+ | if m: | ||
+ | return (m.group(1), | ||
+ | else: | ||
+ | return None | ||
+ | |||
+ | def formatSrtTime(secTime): | ||
+ | """ | ||
+ | sec, micro = str(secTime).split(' | ||
+ | m, s = divmod(int(sec), | ||
+ | h, m = divmod(m, 60) | ||
+ | return " | ||
+ | |||
+ | def convertHtml(text): | ||
+ | """ | ||
+ | & | ||
+ | & | ||
+ | """ | ||
+ | return text.replace('& | ||
+ | |||
+ | def printSrtLine(i, | ||
+ | """ | ||
+ | return " | ||
+ | |||
+ | fileName = sys.argv[1] | ||
+ | |||
+ | def main(fileName): | ||
+ | """ | ||
+ | with open(sys.argv[1], | ||
+ | buf = [] | ||
+ | for line in infile: | ||
+ | buf.append(line.rstrip(' | ||
+ | # Split the buffer to get one string per tag. | ||
+ | buf = "" | ||
+ | i = 0 | ||
+ | srtfileName = fileName.replace(' | ||
+ | with open(srtfileName, | ||
+ | for text in buf: | ||
+ | parsed = parseLine(text) | ||
+ | if parsed: | ||
+ | i += 1 | ||
+ | outfile.write(printSrtLine(i, | ||
+ | print(' | ||
+ | |||
+ | if __name__ == " | ||
+ | main(fileName)</ | ||
===== Advocacy ===== | ===== Advocacy ===== | ||
* [[http:// | * [[http:// |
python.1287868561.txt.gz · Last modified: 2010/10/23 23:16 by ginko