Close

Download live youtube streams from an arbitrary time

A project log for Silly software wishlist

Motivation to do some software projects by writing them down.

lion-mclionheadlion mclionhead 01/17/2023 at 22:281 Comment

Using

yt-dlp --live-from-start <URL>

yt-dlp can download a live stream from now or from the beginning but not from an arbitrary point.  The easiest solution is inside https://github.com/yt-dlp/yt-dlp/blob/master/yt_dlp/extractor/youtube.py

there's a MAX_DURATION variable set to 120 hours or 432000 seconds.  This file is installed in

/usr/lib/python3.10/site-packages/yt_dlp/extractor/youtube.py

You can change MAX_DURATION to any desired number of seconds & --live-from-start will rewind to that time.  It's not accurate to the second.  You can start playing it in mplayer to check the starting time.  Get mplayer to start from later in the stream with mplayer -ss <seconds>

mplayer -ss 999999 makes it start playing live data from the end.

You can also ctrl-c to stop downloading.  It multiplexes the data into a .mkv file.  Helas, if the live stream is still growing, yt-dlp will never stop downloading.

The trick is making MAX_DURATION more conveniently accessed & possibly adding a stop time.

Only 1 stream can be downloaded at a time per python installation.  The fact that all python programs have to be installed in the python system directory is a pain in the ass.

Seeking in the large .mkv files is a buster.  ffmpeg can remux the streams & create an index.

ffmpeg -i "Big Bear Bald Eagle Live Nest - Cam 1 [B4-L2nfGcuE].f140.mkv" -i "Big Bear Bald Eagle Live Nest - Cam 1 [B4-L2nfGcuE].f248.mkv" -c:v copy -c:a copy bald.mp4

ffmpeg -i "Big Bear Bald Eagle Live Nest - Cam 1 [B4-L2nfGcuE].mkv" -c:v copy -c:a copy bald.mp4

The trick is ffmpeg already multiplexes the streams after ctrl-c but it uses mkv by default.  There is a way to make it output a .mp4 with index after ctrl-c.

yt-dlp --live-from-start --remux-video mp4 <URL>


Helas, this generates a .mkv file 1st, then converts the .mkv to a .mp4.  It's just as slow as running the ffmpeg command yourself.  The .mp4 file it generates has corrupted frame durations while the original .mkv has a correct frame rate. 

       count 2 duration 672
       count 1 duration 656
       count 3 duration 672
       count 1 duration 656
       count 2 duration 672
       count 1 duration 656
       count 3 duration 672
       count 1 duration 656
       count 2 duration 672
       count 1 duration 656
       count 2 duration 672
       count 1 duration 656
       count 3 duration 672




Discussions

lentilwallop wrote 11/06/2023 at 03:19 point

this was a super good tip.  is there any way to force quit the download after the download catches up to current live?

  Are you sure? yes | no