Automating HLS-Stream-Creator (Python)

My HLS Stream Creator script takes input videos and outputs adaptive HLS streams. However, it was written as an experiment and developed over time, so has always been focused more on manual runs.

This snippet gives an example of wrapping automation around the script, and (a slightly different version) is in active use on one of my muxers, fetching muxing jobs from an upstream API (hence the slightly odd input to triggerMux).

It may need a little adaptation for use-cases that differ to mine, for example, the output directory is written into the same path as the input video and all input videos exist under the same base directory (basically, a NFS mount point), but should serve to show the basic concept

Read more…

Use a variable to fetch an object attribute (Python)

Sometimes you want to use a variable to contain the name of an attribute that you might later want to fetch from an object. For example, if you're doing hashing operations you might want to have the algorithm name in a variable, and use that in order to trigger behaviour (rather than having a massive if block, duplicating calls).

Read more…

Supporting conditional HTTP requests in PHP (PHP)

RFC 7232 implements support for conditional requests in HTTP. These are commonly used by downstream caches (including those built into browsers) in order to revalidate content - i.e. check whether the copy in cache is stale or whether it should still be considered valid.

The primary mechanisms for this are If-Modified-Since (i.e. has it chanced since date x) and If-None-Match (i.e. does it's Etag match any of those). If the metadata validates, then a HTTP 304 (Not Modified) will be returned, otherwise the requested content will be returned using whatever status code is appropriate for the request (usually a 200, but it could equally be a 206 if the request specifies a range)

Because PHP is used in dynamic pages, conditional requests won't simply work out of the box. To support conditional requests, you need to generate some metadata for whatever asset is being requested and include that in your initial response to the client. You can then validate that if it's later included in a conditional request

The benefit of doing this, though, is that you can save quite a few CPU cycles where a revalidation is possible - particularly if there are any components of your page/asset which are expensive to generate

Read more…

SystemCTL Cheat Sheet (BASH)

Whether or not you like SystemD it is a reality of the world we currently live in, and anyone managing a modern operating system is likely to have a need to interact with it.

Utilities such as service,ckconfig and update.rc-d have largely been replaced with systemctl and it's syntax isn't always as simple and intuitive - especially when you're used to the other ways of doing things

Read more…

Reducing latency of SSH tunnelled connections (BASH)

The ability to tunnel connections over an SSH connection is incredibly useful, essentially creating a poor man's VPN, whether to a specific port (ssh -L 1443:google.com:443 ssh-server.domain) or by standing up a SOCKS proxy (ssh -D 8080 ssh-server.domain).

The most common complaint when doing this, though, is that the latency sucks. This is because of the TCP over TCP Problem. You're essentially running two layers of TCP congestion control, so any loss hurts - a lot.

This can be addressed by installing and using SShuttle (docs here).

It works (on Linux and Mac OS X) by intercepting and termination TCP connections locally, and transmitting payload and packet metadata over a SSH connection as data. The remote end re-assembles the packets and transmits on your behalf. The remote end only needs to have Python installed (as sshuttle remotely runs a python snippet to handle receipt and re-assembly of packet information).

The result is, there's no TCP connection within the tunnel, so you're still only contending with a single layer of congestion control

Read more…