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
|
2018-03-04 19:29:32 +01:00
|
|
|
import binascii
|
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
|
2018-02-10 00:16:04 +01:00
|
|
|
from svtplay_dl.utils.urllib import urljoin
|
2018-02-22 03:28:44 +01:00
|
|
|
from svtplay_dl.subtitle import subtitle
|
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
|
2018-02-10 00:38:52 +01:00
|
|
|
baseurl = re.sub(r'^([^\?]+)/[^/]*(\?.*)?$', r'\1/', srcurl)
|
2018-02-10 00:16:04 +01:00
|
|
|
returl = urljoin(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)
|
2018-01-28 20:23:24 +01:00
|
|
|
|
2018-01-04 00:46:28 +01:00
|
|
|
keycookie = kwargs.pop("keycookie", None)
|
2018-02-12 00:08:09 +01:00
|
|
|
authorization = kwargs.pop("authorization", None)
|
2018-02-22 03:28:44 +01:00
|
|
|
httpobject = kwargs.pop("httpobject", None)
|
2017-10-21 21:38:03 +02:00
|
|
|
|
2017-10-27 00:08:53 +02:00
|
|
|
media = {}
|
2018-02-12 21:20:48 +01:00
|
|
|
segments = None
|
2018-02-04 22:59:38 +01:00
|
|
|
|
2018-01-14 22:23:58 +01:00
|
|
|
if m3u8.master_playlist:
|
|
|
|
for i in m3u8.master_playlist:
|
|
|
|
audio_url = None
|
2018-02-22 03:28:44 +01:00
|
|
|
subtitle_url = None
|
2018-01-14 22:23:58 +01:00
|
|
|
if i["TAG"] == "EXT-X-MEDIA":
|
2018-02-04 22:59:38 +01:00
|
|
|
if "AUTOSELECT" in i and (i["AUTOSELECT"].upper() == "YES"):
|
2018-02-12 21:20:48 +01:00
|
|
|
if i["TYPE"]:
|
|
|
|
if "URI" in i:
|
|
|
|
if segments is None:
|
|
|
|
segments = True
|
|
|
|
if i["GROUP-ID"] not in media:
|
|
|
|
media[i["GROUP-ID"]] = []
|
|
|
|
media[i["GROUP-ID"]].append(i["URI"])
|
|
|
|
else:
|
|
|
|
segments = False
|
2018-01-14 22:23:58 +01:00
|
|
|
continue
|
|
|
|
elif i["TAG"] == "EXT-X-STREAM-INF":
|
|
|
|
bit_rate = float(i["BANDWIDTH"]) / 1000
|
|
|
|
|
|
|
|
if "AUDIO" in i and (i["AUDIO"] in media):
|
2018-02-04 22:59:38 +01:00
|
|
|
audio_url = _get_full_url(media[i["AUDIO"]][0], url)
|
2018-02-22 03:28:44 +01:00
|
|
|
if "SUBTITLES" in i and (i["SUBTITLES"] in media):
|
|
|
|
subtitle_url = _get_full_url(media[i["SUBTITLES"]][0], url)
|
2018-01-14 22:23:58 +01:00
|
|
|
urls = _get_full_url(i["URI"], url)
|
|
|
|
else:
|
2018-01-30 20:11:37 +01:00
|
|
|
continue # Needs to be changed to utilise other tags.
|
2018-02-12 21:20:48 +01:00
|
|
|
options.segments = bool(segments)
|
2018-02-22 03:28:44 +01:00
|
|
|
if subtitle_url and httpobject:
|
|
|
|
m3u8s = M3U8(httpobject.request("get", subtitle_url, cookies=res.cookies).text)
|
|
|
|
streams[1] = subtitle(copy.copy(options), "wrst", _get_full_url(m3u8s.media_segment[0]["URI"], url))
|
2018-02-12 00:08:09 +01:00
|
|
|
streams[int(bit_rate)] = HLS(copy.copy(options), urls, bit_rate, cookies=res.cookies, keycookie=keycookie, authorization=authorization, audio=audio_url)
|
2018-01-14 22:23:58 +01:00
|
|
|
|
|
|
|
elif m3u8.media_segment:
|
2018-03-04 19:29:32 +01:00
|
|
|
options.segments = False
|
2018-02-12 00:08:09 +01:00
|
|
|
streams[0] = HLS(copy.copy(options), url, 0, cookies=res.cookies, keycookie=keycookie, authorization=authorization)
|
2018-01-14 22:23:58 +01:00
|
|
|
|
|
|
|
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-02-04 22:59:38 +01:00
|
|
|
if self.options.segments:
|
|
|
|
if self.audio:
|
|
|
|
self._download(self.audio, file_name=(copy.copy(self.options), "m4a"))
|
|
|
|
self._download(self.url, file_name=(self.options, "mp4"))
|
2017-10-27 00:08:53 +02:00
|
|
|
|
|
|
|
else:
|
2018-02-04 22:59:38 +01:00
|
|
|
self._download(self.url, file_name=(self.options, "ts"))
|
2017-10-27 00:08:53 +02:00
|
|
|
|
2018-02-04 22:59:38 +01:00
|
|
|
def _download(self, url, file_name):
|
2017-10-27 00:08:53 +02:00
|
|
|
cookies = self.kwargs["cookies"]
|
2018-01-07 20:52:19 +01:00
|
|
|
start_time = time.time()
|
2018-02-04 22:59:38 +01:00
|
|
|
m3u8 = M3U8(self.http.request("get", 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
|
2018-03-04 19:29:32 +01:00
|
|
|
from Crypto import Random
|
2014-04-21 16:50:24 +02:00
|
|
|
except ImportError:
|
|
|
|
log.error("You need to install pycrypto to download encrypted HLS streams")
|
|
|
|
sys.exit(2)
|
|
|
|
|
2018-02-04 22:59:38 +01:00
|
|
|
file_d = output(file_name[0], file_name[1])
|
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-02-04 22:59:38 +01:00
|
|
|
item = _get_full_url(i["URI"], 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:
|
2018-02-12 00:08:09 +01:00
|
|
|
headers = {}
|
2017-10-21 21:38:03 +02:00
|
|
|
if self.keycookie:
|
|
|
|
keycookies = self.keycookie
|
|
|
|
else:
|
|
|
|
keycookies = cookies
|
2018-02-12 00:08:09 +01:00
|
|
|
if self.authorization:
|
|
|
|
headers["authorization"] = self.authorization
|
2017-10-21 21:38:03 +02:00
|
|
|
|
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:
|
2018-02-04 22:59:38 +01:00
|
|
|
keyurl = _get_full_url(i["EXT-X-KEY"]["URI"], url)
|
2018-02-12 00:08:09 +01:00
|
|
|
key = self.http.request("get", keyurl, cookies=keycookies, headers=headers).content
|
2018-03-04 19:29:32 +01:00
|
|
|
vi = binascii.unhexlify(i["EXT-X-KEY"]["IV"][2:].zfill(32)) if "IV" in i["EXT-X-KEY"] else Random.new().read(AES.block_size)
|
|
|
|
decryptor = AES.new(key, AES.MODE_CBC, vi)
|
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-02-04 22:59:38 +01:00
|
|
|
base_url = url.split(".m3u8")[0]
|
|
|
|
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-02-04 22:59:38 +01:00
|
|
|
new_m3u8 = M3U8(self.http.request("get", url, cookies=cookies).text)
|
2018-01-21 11:23:14 +01:00
|
|
|
for n_m3u in new_m3u8.media_segment:
|
2018-02-10 14:37:59 +01:00
|
|
|
if not any(d["URI"] == n_m3u["URI"] for d in m3u8.media_segment):
|
2018-01-21 11:23:14 +01:00
|
|
|
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
|
|
|
|
2018-01-30 20:11:37 +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
|
2018-02-12 19:41:17 +01:00
|
|
|
self.independent_segments = False
|
2017-10-21 21:38:03 +02:00
|
|
|
|
|
|
|
self.parse_m3u(data)
|
2017-10-19 00:35:11 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
2018-02-12 21:20:48 +01:00
|
|
|
return "Version: {0}\nMedia Segment: {1}\nMedia Playlist: {2}\nMaster Playlist: {3}\nEncrypted: {4}\tIndependent_segments: {5}"\
|
|
|
|
.format(self.version, self.media_segment, self.media_playlist, self.master_playlist, self.encrypted, self.independent_segments)
|
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-30 20:11:37 +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":
|
2018-02-12 19:41:17 +01:00
|
|
|
self.independent_segments = True
|
2017-10-21 21:38:03 +02:00
|
|
|
|
|
|
|
# 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)
|
2018-01-30 20:11:37 +01:00
|
|
|
except Exception:
|
2017-10-21 21:38:03 +02:00
|
|
|
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
|