Bash is a command language seen in many unix-like systems mostly used as a scripting utility for automation, using existing programs to perform tasks. It is this language's interpreter that runs when you type on your terminal emulator in most GNU/Linux systems some examples are touch, mkdir etc. So in simpler words this bash is used as a language which is designed to let you play with various command line programs to make them work together to do some even bigger task than these programs are originally designed for.

We can do a lot with bash. In this instance of writing, an automation for downloading an online video stream is demonstrated.

Finding the resource(video stream) uri

  • Open dev tools in your browser
  • Go to the Network tab
  • First clear the existing request
  • Play the video, as you play some network request may appear on the screen.
  • Analyze the URL

Reference for the network tab
Example of a stream url:-

The key thing here is to look for pattern in urls of all the network request going out as the video proceeds. In this example the last part just before .ts i.e 00020 and with seg it made sense for segment 20 because that's how streaming happens, sending over the files broken down into segments AKA packets.

It is simple enough to see the pattern now that we know the url, the next step is to get this request equivalent in cURL which will be used in the script by right clicking on one of the request.

First we need to download all the segments for that we can incorporate a simple for loop which will keep on increasing the segment number, this will download only the first 5 segments
The %04d just adds padding to the segment number because of the url pattern. These are only the segments of that original video, but we need the video in one single piece.
To recreate this video in one single piece we can use two of the most commonly used programs found in unix and unix-like systems xargs which is used to literally build commands and cat which stands for concatenate meaning to link together.

We can use as simple as

cat /tmp/gfg/* > video.mp4

The cat just combines the content of files together for eg. if we do cat 1.txt 2.txt on a term it will just display the concatenated output with content of both the text files. In the same way we are using cat in this case to combine the video streams but the difference is it will not display the content on the terminal but redirect the output to create a new file named video.mp4 which will be the final video. Read more about output redirections

The full script is available here. You can modify this script to take in argument of the url directly, change the segment number to download all the segments, handle errors to stop running curl when 404s status code arrives. I'll leave all this to you.

At last thank you for reading this, feel free to give your feedback on the gist itself or an Anonymous Feedback .