1
0
mirror of https://github.com/spaam/svtplay-dl.git synced 2024-11-28 06:04:17 +01:00
svtplay-dl/lib/svtplay_dl/fetcher/hds.py

316 lines
9.0 KiB
Python
Raw Normal View History

2013-03-02 21:26:28 +01:00
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
from __future__ import absolute_import
import base64
import struct
import logging
import binascii
import xml.etree.ElementTree as ET
from svtplay_dl.output import progressbar, progress_stream, ETA, output
from svtplay_dl.utils import get_http_data, is_py2_old, is_py2, is_py3
from svtplay_dl.utils.urllib import urlparse
from svtplay_dl.error import UIException
2014-04-21 16:50:24 +02:00
from svtplay_dl.fetcher import VideoRetriever
log = logging.getLogger('svtplay_dl')
2014-01-07 20:52:03 +01:00
if is_py2:
def bytes(string=None, encoding="ascii"):
if string is None:
return ""
return string
def _chr(temp):
return temp
if is_py3:
def _chr(temp):
return chr(temp)
class HDSException(UIException):
def __init__(self, url, message):
self.url = url
super(HDSException, self).__init__(message)
class LiveHDSException(HDSException):
def __init__(self, url):
super(LiveHDSException, self).__init__(
url, "This is a live HDS stream, and they are not supported.")
2014-04-27 13:19:34 +02:00
def hdsparse(options, manifest):
2014-12-08 23:07:02 +01:00
error, data = get_http_data(manifest)
if error:
log.error("Cant get manifest file")
return
2014-04-27 13:19:34 +02:00
streams = {}
bootstrap = {}
xml = ET.XML(data)
if is_py2_old:
bootstrapIter = xml.getiterator("{http://ns.adobe.com/f4m/1.0}bootstrapInfo")
mediaIter = xml.getiterator("{http://ns.adobe.com/f4m/1.0}media")
else:
bootstrapIter = xml.iter("{http://ns.adobe.com/f4m/1.0}bootstrapInfo")
mediaIter = xml.iter("{http://ns.adobe.com/f4m/1.0}media")
if xml.find("{http://ns.adobe.com/f4m/1.0}drmAdditionalHeader") is not None:
log.error("HDS DRM protected content.")
return
2014-04-27 13:19:34 +02:00
for i in bootstrapIter:
if "id" in i.attrib:
bootstrap[i.attrib["id"]] = i.text
else:
bootstrap["0"] = i.text
parse = urlparse(manifest)
querystring = parse.query
2014-04-27 13:19:34 +02:00
for i in mediaIter:
if len(bootstrap) == 1:
bootstrapid = bootstrap["0"]
else:
bootstrapid = bootstrap[i.attrib["bootstrapInfoId"]]
streams[int(i.attrib["bitrate"])] = HDS(options, i.attrib["url"], i.attrib["bitrate"], manifest=manifest, bootstrap=bootstrapid,
2014-08-28 07:56:47 +02:00
metadata=i.find("{http://ns.adobe.com/f4m/1.0}metadata").text, querystring=querystring)
2014-04-27 13:19:34 +02:00
return streams
2014-04-21 16:50:24 +02:00
class HDS(VideoRetriever):
def name(self):
return "hds"
2014-04-21 16:50:24 +02:00
def download(self):
if self.options.live and not self.options.force:
raise LiveHDSException(self.url)
querystring = self.kwargs["querystring"]
2014-04-27 13:19:34 +02:00
bootstrap = base64.b64decode(self.kwargs["bootstrap"])
2014-04-21 16:50:24 +02:00
box = readboxtype(bootstrap, 0)
antal = None
if box[2] == b"abst":
antal = readbox(bootstrap, box[0])
2014-04-27 13:19:34 +02:00
baseurl = self.kwargs["manifest"][0:self.kwargs["manifest"].rfind("/")]
2014-04-21 16:50:24 +02:00
file_d = output(self.options, self.options.output, "flv")
2014-08-21 22:10:16 +02:00
if hasattr(file_d, "read") is False:
return
2014-04-21 16:50:24 +02:00
2014-04-27 13:19:34 +02:00
metasize = struct.pack(">L", len(base64.b64decode(self.kwargs["metadata"])))[1:]
2014-04-21 16:50:24 +02:00
file_d.write(binascii.a2b_hex(b"464c560105000000090000000012"))
file_d.write(metasize)
file_d.write(binascii.a2b_hex(b"00000000000000"))
2014-04-27 13:19:34 +02:00
file_d.write(base64.b64decode(self.kwargs["metadata"]))
2014-04-21 16:50:24 +02:00
file_d.write(binascii.a2b_hex(b"00000000"))
i = 1
start = antal[1]["first"]
2014-04-21 16:50:24 +02:00
total = antal[1]["total"]
2014-04-27 13:22:02 +02:00
eta = ETA(total)
2014-04-21 16:50:24 +02:00
while i <= total:
url = "%s/%sSeg1-Frag%s?%s" % (baseurl, self.url, start, querystring)
2014-04-21 16:50:24 +02:00
if self.options.output != "-":
eta.update(i)
progressbar(total, i, ''.join(["ETA: ", str(eta)]))
2014-12-08 23:07:02 +01:00
error, data = get_http_data(url)
if error:
log.error("Missing segment in playlist")
return
2014-04-21 16:50:24 +02:00
number = decode_f4f(i, data)
file_d.write(data[number:])
i += 1
start += 1
2014-04-21 16:50:24 +02:00
if self.options.output != "-":
file_d.close()
progress_stream.write('\n')
def readbyte(data, pos):
2014-01-07 20:52:03 +01:00
return struct.unpack("B", bytes(_chr(data[pos]), "ascii"))[0]
def read16(data, pos):
endpos = pos + 2
return struct.unpack(">H", data[pos:endpos])[0]
def read24(data, pos):
end = pos + 3
return struct.unpack(">L", "\x00" + data[pos:end])[0]
def read32(data, pos):
end = pos + 4
return struct.unpack(">i", data[pos:end])[0]
def readu32(data, pos):
end = pos + 4
return struct.unpack(">I", data[pos:end])[0]
def read64(data, pos):
end = pos + 8
return struct.unpack(">Q", data[pos:end])[0]
def readstring(data, pos):
length = 0
2014-01-07 20:52:03 +01:00
while bytes(_chr(data[pos + length]), "ascii") != b"\x00":
length += 1
endpos = pos + length
string = data[pos:endpos]
pos += length + 1
return pos, string
def readboxtype(data, pos):
boxsize = read32(data, pos)
tpos = pos + 4
endpos = tpos + 4
boxtype = data[tpos:endpos]
if boxsize > 1:
boxsize -= 8
pos += 8
return pos, boxsize, boxtype
2013-04-21 12:33:35 +02:00
# Note! A lot of variable assignments are commented out. These are
# accessible values that we currently don't use.
def readbox(data, pos):
2014-07-28 16:01:27 +02:00
# version = readbyte(data, pos)
pos += 1
2014-07-28 16:01:27 +02:00
# flags = read24(data, pos)
pos += 3
2014-07-28 16:01:27 +02:00
# bootstrapversion = read32(data, pos)
pos += 4
2014-07-28 16:01:27 +02:00
# byte = readbyte(data, pos)
pos += 1
2014-07-28 16:01:27 +02:00
# profile = (byte & 0xC0) >> 6
# live = (byte & 0x20) >> 5
# update = (byte & 0x10) >> 4
# timescale = read32(data, pos)
pos += 4
2014-07-28 16:01:27 +02:00
# currentmediatime = read64(data, pos)
pos += 8
2014-07-28 16:01:27 +02:00
# smptetimecodeoffset = read64(data, pos)
pos += 8
temp = readstring(data, pos)
2014-07-28 16:01:27 +02:00
# movieidentifier = temp[1]
pos = temp[0]
serverentrycount = readbyte(data, pos)
pos += 1
serverentrytable = []
i = 0
while i < serverentrycount:
temp = readstring(data, pos)
serverentrytable.append(temp[1])
pos = temp[0]
i += 1
qualityentrycount = readbyte(data, pos)
pos += 1
qualityentrytable = []
i = 0
while i < qualityentrycount:
temp = readstring(data, pos)
qualityentrytable.append(temp[1])
pos = temp[0]
i += 1
tmp = readstring(data, pos)
2014-07-28 16:01:27 +02:00
# drm = tmp[1]
pos = tmp[0]
tmp = readstring(data, pos)
2014-07-28 16:01:27 +02:00
# metadata = tmp[1]
pos = tmp[0]
segmentruntable = readbyte(data, pos)
pos += 1
if segmentruntable > 0:
tmp = readboxtype(data, pos)
boxtype = tmp[2]
boxsize = tmp[1]
pos = tmp[0]
2014-01-07 20:52:03 +01:00
if boxtype == b"asrt":
antal = readasrtbox(data, pos)
pos += boxsize
fragRunTableCount = readbyte(data, pos)
pos += 1
i = 0
first = 1
while i < fragRunTableCount:
tmp = readboxtype(data, pos)
boxtype = tmp[2]
boxsize = tmp[1]
pos = tmp[0]
2014-01-07 20:52:03 +01:00
if boxtype == b"afrt":
first = readafrtbox(data, pos)
pos += boxsize
i += 1
antal[1]["first"] = first
return antal
2013-04-21 12:33:35 +02:00
# Note! A lot of variable assignments are commented out. These are
# accessible values that we currently don't use.
def readafrtbox(data, pos):
2014-07-28 16:01:27 +02:00
# version = readbyte(data, pos)
pos += 1
2014-07-28 16:01:27 +02:00
# flags = read24(data, pos)
pos += 3
2014-07-28 16:01:27 +02:00
# timescale = read32(data, pos)
pos += 4
qualityentry = readbyte(data, pos)
pos += 1
i = 0
while i < qualityentry:
temp = readstring(data, pos)
2014-08-12 00:08:51 +02:00
# qualitysegmulti = temp[1]
pos = temp[0]
i += 1
fragrunentrycount = read32(data, pos)
pos += 4
i = 0
first = 1
skip = False
while i < fragrunentrycount:
firstfragment = readu32(data, pos)
if not skip:
first = firstfragment
skip = True
pos += 4
2014-07-28 16:01:27 +02:00
# timestamp = read64(data, pos)
pos += 8
2014-07-28 16:01:27 +02:00
# duration = read32(data, pos)
pos += 4
i += 1
return first
2013-04-21 12:33:35 +02:00
# Note! A lot of variable assignments are commented out. These are
# accessible values that we currently don't use.
def readasrtbox(data, pos):
2014-07-28 16:01:27 +02:00
# version = readbyte(data, pos)
pos += 1
2014-07-28 16:01:27 +02:00
# flags = read24(data, pos)
pos += 3
qualityentrycount = readbyte(data, pos)
pos += 1
qualitysegmentmodifers = []
i = 0
while i < qualityentrycount:
temp = readstring(data, pos)
qualitysegmentmodifers.append(temp[1])
pos = temp[0]
i += 1
seqCount = read32(data, pos)
pos += 4
ret = {}
i = 0
while i < seqCount:
firstseg = read32(data, pos)
pos += 4
fragPerSeg = read32(data, pos)
pos += 4
tmp = i + 1
ret[tmp] = {"first": firstseg, "total": fragPerSeg}
i += 1
return ret
def decode_f4f(fragID, fragData):
2014-01-07 20:52:03 +01:00
start = fragData.find(b"mdat") + 4
if (fragID > 1):
tagLen, = struct.unpack_from(">L", fragData, start)
tagLen &= 0x00ffffff
start += tagLen + 11 + 4
return start