Make youtube-dl wait until a live stream starts

Viewed 8897

Using youtube-dl, you can easily download an ongoing live streaming.

$ youtube-dl --hls-use-mpegts <URL>

If the target live stream hasn't been started yet, however, the command exits right away after printing messages like

[youtube] I1gi2ABCDEf: Downloading webpage
ERROR: This live event will begin in a few moments.

Is it possible to make youtube-dl wait until the live stream starts and then record it?

My current workaround is this:

#pseudo code
while (true) {
    start = time()
    execute youtube-dl
    end = time()
    if (end - start > 10seconds) { #if recording succeeded
        break
    }
    sleep(some seconds)
}

or this:

#pseudo code
while (true) {
    if (check_if_live_is_active_using_curl_or_youtube_api()) {
        break
    }
    sleep(some seconds)
}
execute youtube-dl

Combining the both is working perfectly now (actualy the second one should be enough but I use also the first one as a fallback) but it would be nice if there was a more elegant way.

2 Answers

Maybe this will do

until youtube-dl --hls-use-mpegts <URL>; do continue; done

or another alternative

while [[ true ]]; do youtube-dl --hls-use-mpegts && break || continue ; done

But there is no in-built function if you were looking for that.
Best approach would be the one you already did by using a script or the command line workarounds.

If you happen to be on a linux distro with systemd, you can use systemd-run to schedule a transient timer unit. For example, if the stream starts at 3:30:

systemd-run --working-directory=/path/to/save/file --on-calendar=15:30 youtube-dl --hls-use-mpegts <URL>

You could add the options --property Restart=on-failure and --property RestartSec=30 to retry if something goes wrong.

Related