1
0
mirror of https://github.com/spaam/svtplay-dl.git synced 2024-11-24 12:15:40 +01:00

utils: forgot to remove the subtiles from the old file

This commit is contained in:
Johan Andersson 2014-04-21 19:54:28 +02:00
parent 7600da2299
commit 89567e297f

View File

@ -5,8 +5,6 @@ import sys
import socket
import logging
import re
import xml.etree.ElementTree as ET
import json
import time
import unicodedata
try:
@ -107,168 +105,6 @@ def check_redirect(url):
else:
return url
def timestr(msec):
"""
Convert a millisecond value to a string of the following
format:
HH:MM:SS,SS
with 10 millisecond precision. Note the , seperator in
the seconds.
"""
sec = float(msec) / 1000
hours = int(sec / 3600)
sec -= hours * 3600
minutes = int(sec / 60)
sec -= minutes * 60
output = "%02d:%02d:%05.2f" % (hours, minutes, sec)
return output.replace(".", ",")
def norm(name):
if name[0] == "{":
_, tag = name[1:].split("}")
return tag
else:
return name
def subtitle_tt(options, subtitle):
i = 1
data = ""
skip = False
tree = ET.ElementTree(ET.fromstring(subtitle))
for node in tree.iter():
tag = norm(node.tag)
if tag == "p":
if skip:
data = data + "\n"
begin = node.attrib["begin"]
if not ("dur" in node.attrib):
duration = node.attrib["duration"]
else:
duration = node.attrib["dur"]
if not ("end" in node.attrib):
begin2 = begin.split(":")
duration2 = duration.split(":")
sec = float(begin2[2]) + float(duration2[2])
end = "%02d:%02d:%06.3f" % (int(begin[0]), int(begin[1]), sec)
else:
end = node.attrib["end"]
data += '%s\n%s --> %s\n' % (i, begin.replace(".",","), end.replace(".",","))
data += '%s\n' % node.text.strip(' \t\n\r')
skip = True
i += 1
if tag == "br":
if node.tail:
data += '%s\n\n' % node.tail.strip(' \t\n\r')
skip = False
filename = re.search(r"(.*)\.[a-z0-9]{2,3}$", options.output)
if filename:
options.output = "%s.srt" % filename.group(1)
else:
options.output = "%s.srt" % options.output
log.info("Subtitle: %s", options.output)
fd = open(options.output, "w")
if is_py2:
data = data.encode('utf8')
fd.write(data)
fd.close()
def subtitle_json(options, data):
data = json.loads(data)
number = 1
subs = ""
for i in data:
subs += "%s\n%s --> %s\n" % (number, timestr(int(i["startMillis"])), timestr(int(i["endMillis"])))
subs += "%s\n\n" % i["text"].encode("utf-8")
number += 1
filename = re.search(r"(.*)\.[a-z0-9]{2,3}$", options.output)
if filename:
options.output = "%s.srt" % filename.group(1)
else:
options.output = "%s.srt" % options.output
log.info("Subtitle: %s", options.output)
fd = open(options.output, "w")
fd.write(subs)
fd.close()
def subtitle_sami(options, data):
tree = ET.XML(data)
subt = tree.find("Font")
subs = ""
n = 0
for i in subt.getiterator():
if i.tag == "Subtitle":
n = i.attrib["SpotNumber"]
if i.attrib["SpotNumber"] == "1":
subs += "%s\n%s --> %s\n" % (i.attrib["SpotNumber"], i.attrib["TimeIn"], i.attrib["TimeOut"])
else:
subs += "\n%s\n%s --> %s\n" % (i.attrib["SpotNumber"], i.attrib["TimeIn"], i.attrib["TimeOut"])
else:
if int(n) > 0:
subs += "%s\n" % i.text
filename = re.search(r"(.*)\.[a-z0-9]{2,3}$", options.output)
if filename:
options.output = "%s.srt" % filename.group(1)
else:
options.output = "%s.srt" % options.output
log.info("Subtitle: %s", options.output)
fd = open(options.output, "w")
if is_py2:
subs = subs.encode('utf8')
fd.write(subs)
fd.close()
def subtitle_smi(options, data):
recomp = re.compile(r'<SYNC Start=(\d+)>\s+<P Class=\w+>(.*)<br>\s+<SYNC Start=(\d+)>\s+<P Class=\w+>', re.M|re.I|re.U)
number = 1
subs = ""
for i in recomp.finditer(str(data)):
subs += "%s\n%s --> %s\n" % (number, timestr(i.group(1)), timestr(i.group(3)))
text = "%s\n\n" % i.group(2)
subs += text.replace("<br>", "\n")
number += 1
filename = re.search(r"(.*)\.[a-z0-9]{2,3}$", options.output)
if filename:
options.output = "%s.srt" % filename.group(1)
else:
options.output = "%s.srt" % options.output
log.info("Subtitle: %s", options.output)
fd = open(options.output, "w")
fd.write(subs)
fd.close()
def subtitle_wsrt(options, data):
recomp = re.compile(r"(\d+)\r\n([\d:\.]+ --> [\d:\.]+)?([^\r\n]+)?\r\n([^\r\n]+)\r\n(([^\r\n]*)\r\n)?")
srt = ""
for i in recomp.finditer(data):
sub = "%s\n%s\n%s\n" % (i.group(1), i.group(2).replace(".", ","), i.group(4))
if len(i.group(6)) > 0:
sub += "%s\n" % i.group(6)
sub += "\n"
sub = re.sub('<[^>]*>', '', sub)
srt += sub
filename = re.search(r"(.*)\.[a-z0-9]{2,3}$", options.output)
if filename:
options.output = "%s.srt" % filename.group(1)
else:
options.output = "%s.srt" % options.output
log.info("Subtitle: %s", options.output)
fd = open(options.output, "w")
fd.write(srt)
fd.close()
def select_quality(options, streams):
available = sorted(streams.keys(), key=int)