Sametinget WebTV exploration
For a master's student
This video page has the ID 244, which is passed to the API:
URL = "https://sametinget.kommunetv.no/api/streams?streamType=1&id=244"
import requests
request = requests.get(URL)
assert request.status_code == 200
I already know that this is JSON
import json
streams = json.loads(request.text)
streams.keys()
'stream' has some useful metadata that I'm not looking at now:
streams['stream'].keys()
streams['stream']['title']
streams['stream']['description']
...but 'playlist' is where the video files are ultimately retrieved from:
streams['playlist'][0].keys()
streams['playlist'][0]['started']
streams['playlist'][0]['playlist'][0].keys()
streams['playlist'][0]['playlist'][0]['file']
playlist.m3u8 is basically a default filename, something like index.html on a webserver.
smilreq = requests.get(streams['playlist'][0]['playlist'][0]['file'])
print(smilreq.text)
It's possible to have multiple playlists within a playlist, so this function aims at that:
def get_m3u(text):
res = []
for line in text.split("\n"):
if line.startswith("#"):
continue
if "m3u8" in line:
res.append(line)
return res
get_m3u(smilreq.text)
m3u_list = get_m3u(smilreq.text)
That it contains just a filename (instead of a full URL) implies that it's relative to the URL of the playlist:
def get_base_url(url):
last_slash = url.rfind("/")
return url[:last_slash + 1]
get_base_url(streams['playlist'][0]['playlist'][0]['file'])
base = get_base_url(streams['playlist'][0]['playlist'][0]['file'])
urls = [base + x for x in m3u_list]
m3u_req = requests.get(urls[0])
m3u_req.status_code
print(m3u_req.text)
I won't include this output: essentially, this is a sequence of .ts files, which contain an individual chunk of the stream. They need to be concatenated to create a full video (ffmpeg can do this).