Bart Simons

Bart Simons

Thoughts, stories and ideas.

Bart Simons




Playing and saving at the same time with streamlink

Bart SimonsBart Simons

I have recently got my hands dirty with streamlink, because I needed a CLI tool to create video recordings of YouTube and Twitch live streams, since YouTube has a buffer limit, and Twitch doesn't even support scrolling back in time during a livestream. Streamlink seemed like the perfect fit for my idea, and the results were good: nice and crisp full VODs of my favorite streaming content. But there was only one open question left to make it really perfect: can it play and record at the same time?

Here's how I did it

First of all, you need to be running macOS or Linux. Windows might work if you use Windows 10 with the Windows subsystem for Linux installed, but that's untested so your results may vary.

The solution that exists for now is to use the tee utility to pipe the stdout of streamlink to a transport stream (.ts) file, and pipe it again to your favourite video player.

streamlink -O url stream | tee recording.ts | vlc - 

Help: my recording.ts file is corrupt - what should I do?

That's not an uncommon problem, in fact: those files are prone to breaking if for example the last frame is not fully written yet when you cancel streamlink. You can always use ffmpeg afterwards to fix this automagically:

ffmpeg -err_detect ignore_err -i recording.ts -c copy recording-fixed.ts

This should work as far as I know.

Is there a better way?

Of course, this is not the best solution: a PR is currently open over at GitHub at:

Hopefully the record flag will be introduced so that you don't have to double pipe the stream. Good luck with getting this to work 👍

Bart Simons

Bart Simons