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 -*-
|
2013-03-01 23:39:42 +01:00
|
|
|
from __future__ import absolute_import
|
2013-02-12 19:39:52 +01:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import re
|
2015-10-04 14:33:54 +02:00
|
|
|
import copy
|
2018-01-07 20:52:19 +01:00
|
|
|
import time
|
2018-01-15 22:16:07 +01:00
|
|
|
from datetime import datetime, timedelta
|
2013-02-12 19:39:52 +01:00
|
|
|
|
2014-08-20 20:27:45 +02:00
|
|
|
from svtplay_dl.output import progressbar, progress_stream, ETA, output
|
2013-03-17 19:55:19 +01:00
|
|
|
from svtplay_dl.log import log
|
2015-10-04 17:41:11 +02:00
|
|
|
from svtplay_dl.error import UIException, ServiceError
|
2014-04-21 16:50:24 +02:00
|
|
|
from svtplay_dl.fetcher import VideoRetriever
|
2017-02-18 23:51:26 +01:00
|
|
|
from svtplay_dl.utils import HTTP
|
2014-04-21 16:50:24 +02:00
|
|
|
|
2014-02-09 15:40:02 +01:00
|
|
|
|
|
|
|
class HLSException(UIException):
|
|
|
|
def __init__(self, url, message):
|
|
|
|
self.url = url
|
|
|
|
super(HLSException, self).__init__(message)
|
|
|
|
|
|
|
|
|
|
|
|
class LiveHLSException(HLSException):
|
|
|
|
def __init__(self, url):
|
|
|
|
super(LiveHLSException, self).__init__(
|
|
|
|
url, "This is a live HLS stream, and they are not supported.")
|
|
|
|
|
2013-02-12 19:39:52 +01:00
|
|
|
|
2014-02-08 16:08:39 +01:00
|
|
|
def _get_full_url(url, srcurl):
|
|
|
|
if url[:4] == 'http':
|
|
|
|
return url
|
2016-09-04 20:43:37 +02:00
|
|
|
if url[0] == '/':
|
|
|
|
baseurl = re.search(r'^(http[s]{0,1}://[^/]+)/', srcurl)
|
2017-10-09 22:35:33 +02:00
|
|
|
return "{0}{1}".format(baseurl.group(1), url)
|
2014-02-08 16:08:39 +01:00
|
|
|
|
|
|
|
# remove everything after last / in the path of the URL
|
|
|
|
baseurl = re.sub(r'^([^\?]+)/[^/]*(\?.*)?$', r'\1', srcurl)
|
2017-10-09 22:35:33 +02:00
|
|
|
returl = "{0}/{1}".format(baseurl, url)
|
2014-02-08 16:08:39 +01:00
|
|
|
|
|
|
|
return returl
|
|
|
|
|
2015-09-15 20:10:32 +02:00
|
|
|
|
2018-01-04 00:46:28 +01:00
|
|
|
def hlsparse(options, res, url, **kwargs):
|
2014-04-21 21:55:39 +02:00
|
|
|
streams = {}
|
2014-04-21 21:42:49 +02:00
|
|
|
|
2016-10-16 19:35:38 +02:00
|
|
|
if not res:
|
|
|
|
return None
|
|
|
|
|
2016-09-09 22:56:05 +02:00
|
|
|
if res.status_code > 400:
|
|
|
|
streams[0] = ServiceError("Can't read HLS playlist. {0}".format(res.status_code))
|
2015-10-04 17:41:11 +02:00
|
|
|
return streams
|
2017-10-21 21:38:03 +02:00
|
|
|
m3u8 = M3U8(res.text)
|
2017-02-18 23:51:26 +01:00
|
|
|
http = HTTP(options)
|
2018-01-04 00:46:28 +01:00
|
|
|
keycookie = kwargs.pop("keycookie", None)
|
2017-10-21 21:38:03 +02:00
|
|
|
|
2017-10-27 00:08:53 +02:00
|
|
|
media = {}
|
2018-01-14 22:23:58 +01:00
|
|
|
if m3u8.master_playlist:
|
|
|
|
for i in m3u8.master_playlist:
|
|
|
|
audio_url = None
|
|
|
|
if i["TAG"] == "EXT-X-MEDIA":
|
|
|
|
if "DEFAULT" in i and (i["DEFAULT"].upper() == "YES"):
|
|
|
|
if i["TYPE"] and ("URI" in i):
|
|
|
|
if i["GROUP-ID"] not in media:
|
|
|
|
media[i["GROUP-ID"]] = []
|
|
|
|
media[i["GROUP-ID"]].append(i["URI"])
|
|
|
|
continue
|
|
|
|
elif i["TAG"] == "EXT-X-STREAM-INF":
|
|
|
|
bit_rate = float(i["BANDWIDTH"]) / 1000
|
|
|
|
|
|
|
|
if "AUDIO" in i and (i["AUDIO"] in media):
|
|
|
|
audio_url = media[i["AUDIO"]][0]
|
|
|
|
|
|
|
|
urls = _get_full_url(i["URI"], url)
|
|
|
|
else:
|
|
|
|
continue # Needs to be changed to utilise other tags.
|
|
|
|
res2 = http.get(urls, cookies=res.cookies)
|
|
|
|
if res2.status_code < 400:
|
|
|
|
|
|
|
|
streams[int(bit_rate)] = HLS(copy.copy(options), urls, bit_rate, cookies=res.cookies, keycookie=keycookie, audio=audio_url)
|
|
|
|
|
|
|
|
elif m3u8.media_segment:
|
|
|
|
streams[0] = HLS(copy.copy(options), url, 0, cookies=res.cookies, keycookie=keycookie)
|
|
|
|
|
|
|
|
else:
|
|
|
|
streams[0] = ServiceError("Can't find HLS playlist in m3u8 file.")
|
|
|
|
|
2014-04-21 21:55:39 +02:00
|
|
|
return streams
|
2014-04-21 21:42:49 +02:00
|
|
|
|
2015-09-15 20:10:32 +02:00
|
|
|
|
2014-04-21 21:55:39 +02:00
|
|
|
class HLS(VideoRetriever):
|
2014-05-01 17:13:46 +02:00
|
|
|
def name(self):
|
|
|
|
return "hls"
|
|
|
|
|
2014-04-21 16:50:24 +02:00
|
|
|
def download(self):
|
2014-04-21 21:42:49 +02:00
|
|
|
|
2018-01-06 00:08:29 +01:00
|
|
|
if self.audio:
|
2018-01-07 20:52:19 +01:00
|
|
|
if self.options.live:
|
|
|
|
raise LiveHLSException(self.url)
|
|
|
|
|
2018-01-06 00:08:29 +01:00
|
|
|
cookies = self.kwargs["cookies"]
|
2017-10-27 00:08:53 +02:00
|
|
|
audio_data_m3u = self.http.request("get", self.audio, cookies=cookies).text
|
|
|
|
audio_m3u8 = M3U8(audio_data_m3u)
|
2018-01-06 00:08:29 +01:00
|
|
|
total_size = audio_m3u8.media_segment[-1]["EXT-X-BYTERANGE"]["n"] + audio_m3u8.media_segment[-1]["EXT-X-BYTERANGE"]["o"]
|
|
|
|
self._download_url(audio_m3u8.media_segment[0]["URI"], audio=True, total_size=total_size)
|
2017-10-27 00:08:53 +02:00
|
|
|
|
2018-01-06 00:08:29 +01:00
|
|
|
data_m3u = self.http.request("get", self.url, cookies=cookies).text
|
|
|
|
m3u8 = M3U8(data_m3u)
|
|
|
|
total_size = m3u8.media_segment[-1]["EXT-X-BYTERANGE"]["n"] + m3u8.media_segment[-1]["EXT-X-BYTERANGE"]["o"]
|
|
|
|
self._download_url(m3u8.media_segment[0]["URI"], total_size=total_size)
|
2017-10-27 00:08:53 +02:00
|
|
|
else:
|
2018-01-07 20:52:19 +01:00
|
|
|
self._download()
|
2017-10-27 00:08:53 +02:00
|
|
|
|
2018-01-07 20:52:19 +01:00
|
|
|
def _download(self):
|
2017-10-27 00:08:53 +02:00
|
|
|
cookies = self.kwargs["cookies"]
|
2018-01-07 20:52:19 +01:00
|
|
|
start_time = time.time()
|
|
|
|
m3u8 = M3U8(self.http.request("get", self.url, cookies=cookies).text)
|
2014-04-21 16:50:24 +02:00
|
|
|
key = None
|
|
|
|
|
2017-10-21 21:38:03 +02:00
|
|
|
if m3u8.encrypted:
|
2014-04-21 16:50:24 +02:00
|
|
|
try:
|
|
|
|
from Crypto.Cipher import AES
|
|
|
|
except ImportError:
|
|
|
|
log.error("You need to install pycrypto to download encrypted HLS streams")
|
|
|
|
sys.exit(2)
|
|
|
|
|
2014-12-30 21:18:01 +01:00
|
|
|
file_d = output(self.options, "ts")
|
2017-11-24 23:11:48 +01:00
|
|
|
if file_d is None:
|
2014-08-20 20:27:45 +02:00
|
|
|
return
|
2014-04-21 16:50:24 +02:00
|
|
|
|
2017-10-22 22:47:15 +02:00
|
|
|
decryptor = None
|
2018-01-07 20:52:19 +01:00
|
|
|
size_media = len(m3u8.media_segment)
|
|
|
|
eta = ETA(size_media)
|
2018-01-20 12:03:19 +01:00
|
|
|
total_duration = 0
|
2018-01-07 20:52:19 +01:00
|
|
|
duration = 0
|
2018-01-21 11:23:14 +01:00
|
|
|
max_duration = 0
|
2018-01-06 00:08:29 +01:00
|
|
|
for index, i in enumerate(m3u8.media_segment):
|
2018-01-07 20:52:19 +01:00
|
|
|
if "duration" in i["EXTINF"]:
|
2018-01-20 12:03:19 +01:00
|
|
|
duration = i["EXTINF"]["duration"]
|
2018-01-21 11:23:14 +01:00
|
|
|
max_duration = max(max_duration, duration)
|
2018-01-20 12:03:19 +01:00
|
|
|
total_duration += duration
|
2018-01-07 20:52:19 +01:00
|
|
|
item = _get_full_url(i["URI"], self.url)
|
2014-04-21 16:50:24 +02:00
|
|
|
|
2017-11-24 23:11:48 +01:00
|
|
|
if not self.options.silent:
|
2018-01-07 20:52:19 +01:00
|
|
|
if self.options.live:
|
2018-01-20 12:03:19 +01:00
|
|
|
progressbar(size_media, index + 1, ''.join(['DU: ', str(timedelta(seconds=int(total_duration)))]))
|
2018-01-07 20:52:19 +01:00
|
|
|
else:
|
|
|
|
eta.increment()
|
|
|
|
progressbar(size_media, index + 1, ''.join(['ETA: ', str(eta)]))
|
2014-04-21 16:50:24 +02:00
|
|
|
|
2015-10-04 14:36:06 +02:00
|
|
|
data = self.http.request("get", item, cookies=cookies)
|
2015-08-30 00:06:20 +02:00
|
|
|
if data.status_code == 404:
|
|
|
|
break
|
|
|
|
data = data.content
|
2017-10-21 21:38:03 +02:00
|
|
|
if m3u8.encrypted:
|
|
|
|
if self.keycookie:
|
|
|
|
keycookies = self.keycookie
|
|
|
|
else:
|
|
|
|
keycookies = cookies
|
|
|
|
|
2017-10-22 22:47:15 +02:00
|
|
|
# Update key/decryptor
|
2018-01-06 00:08:29 +01:00
|
|
|
if "EXT-X-KEY" in i:
|
|
|
|
keyurl = _get_full_url(i["EXT-X-KEY"]["URI"], self.url)
|
2017-10-22 22:47:15 +02:00
|
|
|
key = self.http.request("get", keyurl, cookies=keycookies).content
|
|
|
|
decryptor = AES.new(key, AES.MODE_CBC, os.urandom(16))
|
2018-01-06 00:08:29 +01:00
|
|
|
|
2017-10-22 22:47:15 +02:00
|
|
|
if decryptor:
|
|
|
|
data = decryptor.decrypt(data)
|
|
|
|
else:
|
|
|
|
raise ValueError("No decryptor found for encrypted hls steam.")
|
|
|
|
|
2014-04-21 16:50:24 +02:00
|
|
|
file_d.write(data)
|
2013-02-12 19:39:52 +01:00
|
|
|
|
2018-01-20 12:03:19 +01:00
|
|
|
if (self.options.capture_time > 0) and total_duration >= (self.options.capture_time * 60):
|
2018-01-07 21:49:50 +01:00
|
|
|
break
|
|
|
|
|
2018-01-07 20:52:19 +01:00
|
|
|
if (size_media == (index + 1)) and self.options.live:
|
2018-01-21 11:23:14 +01:00
|
|
|
sleep_int = (start_time + max_duration * 2) - time.time()
|
|
|
|
if sleep_int > 0:
|
|
|
|
time.sleep(sleep_int)
|
2018-01-07 20:52:19 +01:00
|
|
|
|
2018-01-21 11:23:14 +01:00
|
|
|
size_media_old = size_media
|
|
|
|
while size_media_old == size_media:
|
|
|
|
start_time = time.time()
|
2018-01-15 22:16:07 +01:00
|
|
|
|
2018-01-21 11:23:14 +01:00
|
|
|
if self.options.hls_time_stamp:
|
2018-01-15 22:16:07 +01:00
|
|
|
|
2018-01-21 11:23:14 +01:00
|
|
|
end_time_stamp = (datetime.utcnow() - timedelta(seconds=max_duration * 2)).replace(microsecond=0)
|
|
|
|
start_time_stamp = end_time_stamp - timedelta(minutes=1)
|
2018-01-15 22:16:07 +01:00
|
|
|
|
2018-01-21 11:23:14 +01:00
|
|
|
base_url = self.url.split(".m3u8")[0]
|
|
|
|
self.url = "{0}.m3u8?in={1}&out={2}?".format(base_url, start_time_stamp.isoformat(), end_time_stamp.isoformat())
|
2018-01-15 22:16:07 +01:00
|
|
|
|
2018-01-21 11:23:14 +01:00
|
|
|
new_m3u8 = M3U8(self.http.request("get", self.url, cookies=cookies).text)
|
|
|
|
for n_m3u in new_m3u8.media_segment:
|
|
|
|
if n_m3u not in m3u8.media_segment:
|
|
|
|
m3u8.media_segment.append(n_m3u)
|
2018-01-07 20:52:19 +01:00
|
|
|
|
2018-01-21 11:23:14 +01:00
|
|
|
size_media = len(m3u8.media_segment)
|
|
|
|
|
|
|
|
if size_media_old == size_media:
|
|
|
|
time.sleep(max_duration)
|
2018-01-07 20:52:19 +01:00
|
|
|
|
2017-11-24 23:11:48 +01:00
|
|
|
file_d.close()
|
|
|
|
if not self.options.silent:
|
|
|
|
progress_stream.write('\n')
|
|
|
|
self.finished = True
|
2013-02-12 19:39:52 +01:00
|
|
|
|
2017-10-19 00:35:11 +02:00
|
|
|
class M3U8():
|
2017-10-21 21:38:03 +02:00
|
|
|
# Created for hls version <=7
|
|
|
|
# https://tools.ietf.org/html/rfc8216
|
2017-10-19 00:35:11 +02:00
|
|
|
|
2017-10-21 21:38:03 +02:00
|
|
|
MEDIA_SEGMENT_TAGS = ("EXTINF", "EXT-X-BYTERANGE", "EXT-X-DISCONTINUITY",
|
|
|
|
"EXT-X-KEY", "EXT-X-MAP", "EXT-X-PROGRAM-DATE-TIME", "EXT-X-DATERANGE")
|
|
|
|
MEDIA_PLAYLIST_TAGS = ("EXT-X-TARGETDURATION", "EXT-X-MEDIA-SEQUENCE", "EXT-X-DISCONTINUITY-SEQUENCE",
|
|
|
|
"EXT-X-ENDLIST", "EXT-X-PLAYLIST-TYPE", "EXT-X-I-FRAMES-ONLY")
|
|
|
|
MASTER_PLAYLIST_TAGS = ("EXT-X-MEDIA", "EXT-X-STREAM-INF", "EXT-X-I-FRAME-STREAM-INF",
|
|
|
|
"EXT-X-SESSION-DATA", "EXT-X-SESSION-KEY")
|
|
|
|
MEDIA_OR_MASTER_PLAYLIST_TAGS = ("EXT-X-INDEPENDENT-SEGMENTS", "EXT-X-START")
|
|
|
|
|
|
|
|
TAG_TYPES = {"MEDIA_SEGMENT": 0, "MEDIA_PLAYLIST": 1, "MASTER_PLAYLIST": 2}
|
|
|
|
|
|
|
|
def __init__(self, data):
|
2017-10-19 00:35:11 +02:00
|
|
|
|
|
|
|
self.version = None
|
2017-10-21 21:38:03 +02:00
|
|
|
|
|
|
|
self.media_segment = []
|
|
|
|
self.media_playlist = {}
|
|
|
|
self.master_playlist = []
|
|
|
|
|
|
|
|
self.encrypted = False
|
|
|
|
|
|
|
|
self.parse_m3u(data)
|
2017-10-19 00:35:11 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
2017-10-21 21:38:03 +02:00
|
|
|
return "Version: {0}\nMedia Segment: {1}\nMedia Playlist: {2}\nMaster Playlist: {3}\nEncrypted: {4}"\
|
|
|
|
.format(self.version, self.media_segment, self.media_playlist, self.master_playlist, self.encrypted)
|
2017-10-19 00:35:11 +02:00
|
|
|
|
|
|
|
def parse_m3u(self, data):
|
2017-10-21 21:38:03 +02:00
|
|
|
if not data.startswith("#EXTM3U"):
|
2017-10-19 00:35:11 +02:00
|
|
|
raise ValueError("Does not appear to be an 'EXTM3U' file.")
|
|
|
|
|
|
|
|
data = data.replace("\r\n", "\n")
|
|
|
|
lines = data.split("\n")[1:]
|
2017-10-21 21:38:03 +02:00
|
|
|
|
|
|
|
last_tag_type = None
|
|
|
|
tag_type = None
|
|
|
|
|
|
|
|
media_segment_info = {}
|
2017-10-19 00:35:11 +02:00
|
|
|
|
|
|
|
for index, l in enumerate(lines):
|
2017-10-21 21:38:03 +02:00
|
|
|
if not l:
|
|
|
|
continue
|
|
|
|
elif l.startswith("#EXT"):
|
|
|
|
|
|
|
|
info = {}
|
|
|
|
tag, attr = _get_tag_attribute(l)
|
|
|
|
if tag == "EXT-X-VERSION":
|
|
|
|
self.version = int(attr)
|
|
|
|
|
|
|
|
# 4.3.2. Media Segment Tags
|
|
|
|
elif tag in M3U8.MEDIA_SEGMENT_TAGS:
|
|
|
|
|
|
|
|
tag_type = M3U8.TAG_TYPES["MEDIA_SEGMENT"]
|
|
|
|
# 4.3.2.1. EXTINF
|
|
|
|
if tag == "EXTINF":
|
2018-01-05 21:47:42 +01:00
|
|
|
if "," in attr:
|
|
|
|
dur, title = attr.split(",", 1)
|
|
|
|
else:
|
|
|
|
dur = attr
|
|
|
|
title = None
|
2017-10-21 21:38:03 +02:00
|
|
|
info["duration"] = float(dur)
|
|
|
|
info["title"] = title
|
|
|
|
|
|
|
|
# 4.3.2.2. EXT-X-BYTERANGE
|
|
|
|
elif tag == "EXT-X-BYTERANGE":
|
|
|
|
if "@" in attr:
|
|
|
|
n, o = attr.split("@", 1)
|
|
|
|
info["n"], info["o"] = (int(n), int(o))
|
|
|
|
else:
|
|
|
|
info["n"] = int(attr)
|
2018-01-06 00:08:29 +01:00
|
|
|
info["o"] = 0
|
2017-10-21 21:38:03 +02:00
|
|
|
|
|
|
|
# 4.3.2.3. EXT-X-DISCONTINUITY
|
|
|
|
elif tag == "EXT-X-DISCONTINUITY":
|
|
|
|
pass
|
|
|
|
|
|
|
|
# 4.3.2.4. EXT-X-KEY
|
|
|
|
elif tag == "EXT-X-KEY":
|
|
|
|
self.encrypted = True
|
|
|
|
info = _get_tuple_attribute(attr)
|
|
|
|
|
|
|
|
# 4.3.2.5. EXT-X-MAP
|
|
|
|
elif tag == "EXT-X-MAP":
|
|
|
|
info = _get_tuple_attribute(attr)
|
|
|
|
|
|
|
|
# 4.3.2.6. EXT-X-PROGRAM-DATE-TIME"
|
|
|
|
elif tag == "EXT-X-PROGRAM-DATE-TIME":
|
|
|
|
info = attr
|
|
|
|
|
|
|
|
# 4.3.2.7. EXT-X-DATERANGE
|
|
|
|
elif tag == "EXT-X-DATERANGE":
|
|
|
|
info = _get_tuple_attribute(attr)
|
|
|
|
|
|
|
|
media_segment_info[tag] = info
|
|
|
|
|
|
|
|
# 4.3.3. Media Playlist Tags
|
|
|
|
elif tag in M3U8.MEDIA_PLAYLIST_TAGS:
|
|
|
|
|
|
|
|
tag_type = M3U8.TAG_TYPES["MEDIA_PLAYLIST"]
|
|
|
|
# 4.3.3.1. EXT-X-TARGETDURATION
|
|
|
|
if tag == "EXT-X-TARGETDURATION":
|
|
|
|
info = int(attr)
|
|
|
|
|
|
|
|
# 4.3.3.2. EXT-X-MEDIA-SEQUENCE
|
|
|
|
elif tag == "EXT-X-MEDIA-SEQUENCE":
|
|
|
|
info = int(attr)
|
|
|
|
|
|
|
|
# 4.3.3.3. EXT-X-DISCONTINUITY-SEQUENCE
|
|
|
|
elif tag == "EXT-X-DISCONTINUITY-SEQUENCE":
|
|
|
|
info = int(attr)
|
|
|
|
|
|
|
|
# 4.3.3.4. EXT-X-ENDLIST
|
|
|
|
elif tag == "EXT-X-ENDLIST":
|
|
|
|
break
|
|
|
|
|
|
|
|
# 4.3.3.5. EXT-X-PLAYLIST-TYPE
|
|
|
|
elif tag == "EXT-X-PLAYLIST-TYPE":
|
|
|
|
info = attr
|
|
|
|
|
|
|
|
# 4.3.3.6. EXT-X-I-FRAMES-ONLY
|
|
|
|
elif tag == "EXT-X-I-FRAMES-ONLY":
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.media_playlist[tag] = info
|
|
|
|
|
|
|
|
# 4.3.4. Master Playlist Tags
|
|
|
|
elif tag in M3U8.MASTER_PLAYLIST_TAGS:
|
|
|
|
|
|
|
|
tag_type = M3U8.TAG_TYPES["MASTER_PLAYLIST"]
|
|
|
|
# 4.3.4.1. EXT-X-MEDIA
|
|
|
|
if tag == "EXT-X-MEDIA":
|
|
|
|
info = _get_tuple_attribute(attr)
|
|
|
|
|
|
|
|
# 4.3.4.2. EXT-X-STREAM-INF
|
|
|
|
elif tag == "EXT-X-STREAM-INF":
|
|
|
|
info = _get_tuple_attribute(attr)
|
|
|
|
if "BANDWIDTH" not in info:
|
|
|
|
raise ValueError("Can't find 'BANDWIDTH' in 'EXT-X-STREAM-INF'")
|
2018-01-05 22:43:53 +01:00
|
|
|
info["URI"] = lines[index+1]
|
2017-10-21 21:38:03 +02:00
|
|
|
|
|
|
|
# 4.3.4.3. EXT-X-I-FRAME-STREAM-INF
|
|
|
|
elif tag == "EXT-X-I-FRAME-STREAM-INF":
|
|
|
|
info = _get_tuple_attribute(attr)
|
|
|
|
|
|
|
|
# 4.3.4.4. EXT-X-SESSION-DATA
|
|
|
|
elif tag == "EXT-X-SESSION-DATA":
|
|
|
|
info = _get_tuple_attribute(attr)
|
|
|
|
|
|
|
|
# 4.3.4.5. EXT-X-SESSION-KEY
|
|
|
|
elif tag == "EXT-X-SESSION-KEY":
|
|
|
|
self.encrypted = True
|
|
|
|
info = _get_tuple_attribute(attr)
|
|
|
|
info["TAG"] = tag
|
|
|
|
|
2018-01-05 22:43:53 +01:00
|
|
|
self.master_playlist.append(info)
|
2017-10-21 21:38:03 +02:00
|
|
|
|
|
|
|
# 4.3.5. Media or Master Playlist Tags
|
|
|
|
elif tag in M3U8.MEDIA_OR_MASTER_PLAYLIST_TAGS:
|
|
|
|
|
|
|
|
tag_type = M3U8.TAG_TYPES["MEDIA_PLAYLIST"]
|
|
|
|
# 4.3.5.1. EXT-X-INDEPENDENT-SEGMENTS
|
|
|
|
if tag == "EXT-X-INDEPENDENT-SEGMENTS":
|
|
|
|
pass
|
|
|
|
|
|
|
|
# 4.3.5.2. EXT-X-START
|
|
|
|
elif tag == "EXT-X-START":
|
|
|
|
info = _get_tuple_attribute(attr)
|
|
|
|
|
|
|
|
self.media_playlist[tag] = info
|
|
|
|
|
|
|
|
# Unused tags
|
|
|
|
else:
|
|
|
|
pass
|
|
|
|
# This is a comment
|
|
|
|
elif l.startswith("#"):
|
|
|
|
pass
|
|
|
|
# This must be a url/uri
|
|
|
|
else:
|
|
|
|
tag_type = None
|
|
|
|
|
|
|
|
if last_tag_type is M3U8.TAG_TYPES["MEDIA_SEGMENT"]:
|
2018-01-06 00:08:29 +01:00
|
|
|
media_segment_info["URI"] = l
|
|
|
|
self.media_segment.append(media_segment_info)
|
2017-10-21 21:38:03 +02:00
|
|
|
media_segment_info = {}
|
|
|
|
|
|
|
|
last_tag_type = tag_type
|
|
|
|
|
|
|
|
if self.media_segment and self.master_playlist:
|
|
|
|
raise ValueError("This 'M3U8' file contains data for both 'Media Segment' and 'Master Playlist'. This is not allowed.")
|
|
|
|
|
|
|
|
|
|
|
|
def _get_tag_attribute(line):
|
|
|
|
line = line[1:]
|
|
|
|
try:
|
|
|
|
search_line = re.search("^([A-Z\-]*):(.*)", line)
|
|
|
|
return search_line.group(1), search_line.group(2)
|
|
|
|
except:
|
|
|
|
return line, None
|
|
|
|
|
|
|
|
|
|
|
|
def _get_tuple_attribute(attribute):
|
|
|
|
attr_tuple = {}
|
|
|
|
for art_l in re.split(''',(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', attribute):
|
|
|
|
if art_l:
|
2017-10-27 00:08:53 +02:00
|
|
|
name, value = art_l.split("=", 1)
|
2018-01-09 13:59:12 +01:00
|
|
|
name = name.strip()
|
2017-10-27 00:08:53 +02:00
|
|
|
# Checks for attribute name
|
|
|
|
if not re.match("^[A-Z0-9\-]*$", name):
|
|
|
|
raise ValueError("Not a valid attribute name.")
|
2017-10-21 21:38:03 +02:00
|
|
|
|
2017-10-27 00:08:53 +02:00
|
|
|
# Remove extra quotes of string
|
|
|
|
if value.startswith('"') and value.endswith('"'):
|
|
|
|
value = value[1:-1]
|
|
|
|
attr_tuple[name] = value
|
2017-10-21 21:38:03 +02:00
|
|
|
|
|
|
|
return attr_tuple
|