KekwVideos Downloader
An experiment in reverse-engineering streaming services, aimed at better understanding how videos are delivered to users. I built it to download music and video content for editing, a green screen or an effect, that was free of copyright restrictions but no longer available for download.

Contents
KekwVideos Downloader is simply an experiment in reverse-engineering streaming services, aimed at better understanding how videos are delivered to users, for research and academic purpose.
Why the program exists#
I needed an application to download YouTube videos. The interesting part was the system that ships the video to the user from the web: the page does not hand over one finished file first. It hands over a set of addresses, and the player follows them.
The practical reason was video editing. I needed music and video content to cut into an edit. The video to download is the green screen or the effect, and other material of the same kind. YouTube is full of free green screen videos that are free to use for a montage. The MLG pack is one example. The content I needed was free of copyright restrictions, but it was no longer available for download.
pytube#
The tool I chose for YouTube was pytube. It is free, it comes from the community, and it can download from a YouTube link. The call itself is direct. What I wanted on top of it was a window, so I did not have to drive the download from a script each time.
The m3u8 method#
While I was searching streaming services I found a second method, built on m3u8. A file named playlist.m3u8 is provided. It contains the information to reach the stream outputs for video quality. I used one website as the testing ground for that method. The site is not named here.
I tested URLs taken from the HTTP requests and edited those URLs. That was enough to see how the system worked. A video is provided in smaller packages. Each package is a short clip, in the clear or encrypted in some form. The browser could already show the final picture, so the pieces required for the download were already in that exchange.
The application#
pytube was wrapped in an application to make the YouTube download easier to use. The clip below is that application.
The same clip is on YouTube.
You paste a link into Video's URL. The program scans the URL and fills the Quality list with what that link offers, either the video qualities or the audio options. You select the quality you want. If you want only the audio, you check mp3, and the download is an mp3 file.


The m3u8 playlist#
Standard use#
An m3u8 file is a playlist. The usual delivery has two of them.
The first is the master playlist. A common name for it is playlist.m3u8. It does not contain the picture. It lists one address for each quality the service is willing to send. A player reads that list and chooses one quality.
The chosen address is a second playlist, the media playlist. That file lists the video as short clips, in order. The player requests the clips one after another and plays them as a single video. A clip can be in the clear, or it can be encrypted. The browser can show the result because it receives the same clips.
Master playlist -> Media playlist
Media playlist -> Clips, in orderThe requests follow the same order. The player asks for the master playlist, then for one media playlist, then for each clip.
Player -> Server: GET master playlist
Server -> Player: quality list
Player -> Server: GET media playlist
Server -> Player: clip list
Player -> Server: GET clip
Server -> Player: clip bytesThis downloader#
The downloader takes the player's place in that exchange and keeps the bytes. The address it starts from has the shape https://.../01/playlist.m3u8. When that address is not yet the media playlist, the URL from the request is edited and requested again, until the reply is a playlist. A playlist reply uses the content type application/vnd.apple.mpegurl.
m3u8_get_quality reads the master playlist and keeps a quality only when its address contains one of 1080, 720, 480, 360, 240, or 144. The first match wins. The stored path drops the first character of the URI, and the label is the height plus p, such as 1080p.
def m3u8_get_quality(url):
response = requests.get(url)
playlist = m3u8.loads(response.text)
found = []
for item in playlist.data["playlists"]:
uri = item["uri"]
for height in ("1080", "720", "480", "360", "240", "144"):
if height in uri:
found.append(vid.video(uri[1:], height + "p"))
break
return found
When the format is m3u8, download_video calls m3u8_get_quality, then requests the media playlist for one quality from that list. The directory of that address is the base for the clips. Each clip is written into one .ts file. The name comes from the UI. Spaces in the name become underscores, and an empty name becomes video. If the download thread is marked killed, the loop returns. Each clip is requested with a timeout of 3 seconds, the bytes are appended, and the progress bar advances. streamable then writes the .mp4. The .ts file is removed, and the .mp4 is moved to the output path.
def download_video(self, url):
if format != "m3u8":
return
media = requests.get(url)
playlist = m3u8.loads(media.text)
base = url.rsplit("/", 1)[0]
name = self.ui.filename_entry.get().replace(" ", "_")
if name == "":
name = "video"
segments = playlist.data["segments"]
step = 100 / len(segments)
with open(name + ".ts", "wb") as handle:
for index, segment in enumerate(segments, start=1):
if self.ui.dwn_thread.killed:
return
piece = requests.get(base + "/" + segment["uri"], timeout=3)
piece.raise_for_status()
handle.write(piece.content)
self.ui.set_progress_bar(index * step)
self.streamable(name + ".ts", name + ".mp4")
Clips -> name.ts
name.ts -> streamable
streamable -> mp4 in the output path