How do you use youtube-dl to download live streams (that are live)?
Asked Answered
G

6

103

Is it possible to use youtube-dl to download video from a .m3u8 stream file or other livestream formats?

When I copy the video URL into YouTube-dl it spits out:

[https @ 0x7fc351416080] inflate return value: -3, incorrect header check
Last message repeated 15 times

After that it spits out of couple lines of red text that doesn't seem to want to copy in properly, so I took a snippet:

Picture

Does anyone know if this is possible?

Gymnasium answered 5/5, 2016 at 1:25 Comment(2)
Streamlink seems to be handling live streams better at the moment - just adding this in because this question comes up high in search results.Wieldy
After upgrading youtube-dl today, I noticed that infinite live streams (like street webcams) are now passed to ffmpeg (as a playlist) and the download seems to work OK. Unfortunately, passing --external-downloader-args "-t 60" didn't limit it to 1 minute, but pressing Ctrl+C finishes the download gracefully, and the video file is not corrupt.Sassoon
P
134

I'll be using this Live Event from NASA TV as an example:

https://www.youtube.com/watch?v=21X5lGlDOfg

First, list the formats for the video:

youtube-dl --list-formats https://www.youtube.com/watch\?v\=21X5lGlDOfg

[youtube] 21X5lGlDOfg: Downloading webpage
[youtube] 21X5lGlDOfg: Downloading m3u8 information
[youtube] 21X5lGlDOfg: Downloading MPD manifest
[info] Available formats for 21X5lGlDOfg:
format code  extension  resolution note
91           mp4        256x144    HLS  197k , avc1.42c00b, 30.0fps, mp4a.40.5@ 48k
92           mp4        426x240    HLS  338k , avc1.4d4015, 30.0fps, mp4a.40.5@ 48k
93           mp4        640x360    HLS  829k , avc1.4d401e, 30.0fps, mp4a.40.2@128k
94           mp4        854x480    HLS 1380k , avc1.4d401f, 30.0fps, mp4a.40.2@128k
300          mp4        1280x720   3806k , avc1.4d4020, 60.0fps, mp4a.40.2 (best)

Pick the format you wish to download, and fetch the HLS m3u8 URL of the video from the manifest. I'll be using 94 mp4 854x480 HLS 1380k , avc1.4d401f, 30.0fps, mp4a.40.2@128k for this example:

youtube-dl -f 94 -g https://www.youtube.com/watch\?v\=21X5lGlDOfg
https://manifest.googlevideo.com/api/manifest/hls_playlist/expire/1592099895/ei/1y_lXuLOEsnXyQWYs4GABw/ip/81.190.155.248/id/21X5lGlDOfg.3/itag/94/source/yt_live_broadcast/requiressl/yes/ratebypass/yes/live/1/goi/160/sgoap/gir%3Dyes%3Bitag%3D140/sgovp/gir%3Dyes%3Bitag%3D135/hls_chunk_host/r5---sn-h0auphxqp5-f5fs.googlevideo.com/playlist_duration/30/manifest_duration/30/vprv/1/playlist_type/DVR/initcwndbps/8270/mh/N8/mm/44/mn/sn-h0auphxqp5-f5fs/ms/lva/mv/m/mvi/4/pl/16/dover/11/keepalive/yes/beids/9466586/mt/1592078245/disable_polymer/true/sparams/expire,ei,ip,id,itag,source,requiressl,ratebypass,live,goi,sgoap,sgovp,playlist_duration,manifest_duration,vprv,playlist_type/sig/AOq0QJ8wRgIhAM2dGSece2shUTgS73Qa3KseLqnf85ca_9u7Laz7IDfSAiEAj8KHw_9xXVS_PV3ODLlwDD-xfN6rSOcLVNBpxKgkRLI%3D/lsparams/hls_chunk_host,initcwndbps,mh,mm,mn,ms,mv,mvi,pl/lsig/AG3C_xAwRQIhAJCO6kSwn7PivqMW7sZaiYFvrultXl6Qmu9wppjCvImzAiA7vkub9JaanJPGjmB4qhLVpHJOb9fZyhMEeh1EUCd-3Q%3D%3D/playlist/index.m3u8

Note that link could be different and it contains expiration timestamp, in this case 1592099895 (about 6 hours).

Now that you have the HLS playlist, you can open this URL in VLC and save it using "Record", or write a small ffmpeg command:

ffmpeg -i \
https://manifest.googlevideo.com/api/manifest/hls_playlist/expire/1592099895/ei/1y_lXuLOEsnXyQWYs4GABw/ip/81.190.155.248/id/21X5lGlDOfg.3/itag/94/source/yt_live_broadcast/requiressl/yes/ratebypass/yes/live/1/goi/160/sgoap/gir%3Dyes%3Bitag%3D140/sgovp/gir%3Dyes%3Bitag%3D135/hls_chunk_host/r5---sn-h0auphxqp5-f5fs.googlevideo.com/playlist_duration/30/manifest_duration/30/vprv/1/playlist_type/DVR/initcwndbps/8270/mh/N8/mm/44/mn/sn-h0auphxqp5-f5fs/ms/lva/mv/m/mvi/4/pl/16/dover/11/keepalive/yes/beids/9466586/mt/1592078245/disable_polymer/true/sparams/expire,ei,ip,id,itag,source,requiressl,ratebypass,live,goi,sgoap,sgovp,playlist_duration,manifest_duration,vprv,playlist_type/sig/AOq0QJ8wRgIhAM2dGSece2shUTgS73Qa3KseLqnf85ca_9u7Laz7IDfSAiEAj8KHw_9xXVS_PV3ODLlwDD-xfN6rSOcLVNBpxKgkRLI%3D/lsparams/hls_chunk_host,initcwndbps,mh,mm,mn,ms,mv,mvi,pl/lsig/AG3C_xAwRQIhAJCO6kSwn7PivqMW7sZaiYFvrultXl6Qmu9wppjCvImzAiA7vkub9JaanJPGjmB4qhLVpHJOb9fZyhMEeh1EUCd-3Q%3D%3D/playlist/index.m3u8 \
-c copy output.ts
Penutian answered 6/5, 2016 at 14:24 Comment(11)
If you have choosen the format you like, instead of copy and paste you can use '$()' (bash command substitution): ffmpeg -i $(youtube-dl -f 22 -g https://www.youtube.com/watch\?v\=6aXR-SL5L2o) -c copy omg1.tsConstanceconstancia
Maybe you'll want to use NASA TV's livestream as an example here, because that has been live nonstop for the last 19 months, so the link will stay the same for quite a while.Eolic
What is youtube_live_test? ThankVanpelt
@Vanpelt that was just the directory I was in. Edited the answer for clarity.Penutian
sadly doesn't work for 3QV streams like: konzertzuhaus.at/streaming-konzert/…Characteristically
I get ERROR: VUhQ6zEky0o: YouTube said: Invalid parameters. My command was youtube-dl --list-formats https://www.youtube.com/watch?v=VUhQ6zEky0o.Gendarmerie
Here is my solution: https://mcmap.net/q/209720/-how-do-you-use-youtube-dl-to-download-live-streams-that-are-live.Gendarmerie
^^ Any ideas how to fix this error otherwise? ERROR: VUhQ6zEky0o: YouTube said: Invalid parameters. I'm getting downvotes on my answer despite it working while this most-upvoted answer doesn't. Sample command where I see this error: youtube-dl --list-formats https://www.youtube.com/watch?v=VUhQ6zEky0o.Gendarmerie
Is there a way of recording the livestream from the beginning? On the youtube web page, it is possible to queue the video to play from the start, but youtube-dl seems to start recording from the current moment.Derry
After executing command like ./youtube-dl -f 22 -g https://www.youtube.com/watch\?v\=___VIDEO_ID___, I've copy/pasted output opening URL in browser (Brave), and finally picked "download" option.Halfhour
To fix ERROR: Unable to extract uploader id in youtube-dl version 2021.12.17 refere to https://mcmap.net/q/211878/-error-unable-to-extract-uploader-id-youtube-discord-pyExtravaganza
D
42

There is no need to pass anything to ffmpeg you can just grab the desired format, in this example, it was the "95" format.

So once you know that it is the 95, you just type:

youtube-dl -f 95  https://www.youtube.com/watch\?v\=6aXR-SL5L2o

that is to say:

youtube-dl -f <format number> <url>

It will begin generating on the working directory a <somename>.<probably mp4>.part which is the partially downloaded file, let it go and just press <Ctrl-C> to stop the capture.

The file will still be named <something>.part, rename it to <whatever>.mp4 and there it is...

The ffmpeg code:

ffmpeg -i $(youtube-dl -f <format number> -g <url>) -copy <file_name>.ts

also worked for me, but sound and video got out of sync, using just youtube-dl seemed to yield a better result although it too uses ffmpeg.

The downside of this approach is that you cannot watch the video while downloading, well you can open yet another FF or Chrome, but it seems that mplayer cannot process the video output till youtube-dl/ffmpeg are running.

Disinter answered 22/4, 2017 at 17:5 Comment(3)
Midway during the live stream, copied the .part file being downloaded and changed the format to .mp4. However, Windows Media Player says file format is not recognized. Any workarounds? Or, it is best to run the command when the live stream is complete?Arie
@Arie I believe it depends on the fact that some metadata are missing in the MP4 file because of the interruption. Have you tried to open it using VLC instead? If you really want to use Windows Media Player, you can repair the file using some tool (e.g. ffmpeg) and it should work with WMP as well.Suber
That way you cannot download vp9 live streams. Alas.Abruzzi
F
9

Some websites with m3u streaming cannot be downloaded in a single youtube-dl step, you can try something like this :

$ URL=https://www.arte.tv/fr/videos/078132-001-A/cosmos-une-odyssee-a-travers-l-univers/
$ youtube-dl -F $URL | grep m3u
HLS_XQ_2     m3u8       1280x720   VA-STA, Allemand 2200k 
HLS_XQ_1     m3u8       1280x720   VF-STF, Français 2200k 
$ CHOSEN_FORMAT=HLS_XQ_1
$ youtube-dl -F "$(youtube-dl -gf $CHOSEN_FORMAT)"
[generic] master: Requesting header
[generic] master: Downloading webpage
[generic] master: Downloading m3u8 information
[info] Available formats for master:
format code  extension  resolution note
61           mp4        audio only   61k , mp4a.40.2
419          mp4        384x216     419k , avc1.66.30, mp4a.40.2
923          mp4        640x360     923k , avc1.77.30, mp4a.40.2
1737         mp4        720x406    1737k , avc1.77.30, mp4a.40.2
2521         mp4        1280x720   2521k , avc1.77.30, mp4a.40.2 (best)
$ youtube-dl --hls-prefer-native -f 1737 "$(youtube-dl -gf $CHOSEN_FORMAT $URL)" -o "$(youtube-dl -f $CHOSEN_FORMAT --get-filename $URL)"
[generic] master: Requesting header
[generic] master: Downloading webpage
[generic] master: Downloading m3u8 information
[hlsnative] Downloading m3u8 manifest
[hlsnative] Total fragments: 257
[download] Destination: Cosmos_une_odyssee_a_travers_l_univers__HLS_XQ_1__078132-001-A.mp4
[download]   0.9% of ~731.27MiB at 624.95KiB/s ETA 13:13
....
Funke answered 29/8, 2019 at 20:39 Comment(1)
The command youtube-dl --hls-prefer-native your_m3u8_file_url.m3u8 seemed to work best for me. It downloads the video straight to mp4 format.Babism
S
3

I have Written a small script to download the live youtube video, you may use as single command as well. script it can be invoked simply as,

~/ytdl_lv.sh <URL> <output file name>

e.g.

~/ytdl_lv.sh https://www.youtube.com/watch?v=nX0sg1Gp-1 myfile.mp4

script is as simple as below,

#!/bin/bash 

# ytdl_lv.sh
# Author Prashant
# 

URL=$1 
OUTNAME=$2
streamlink --hls-live-restart -o ${OUTNAME} ${URL} best

here the best is the stream quality, it also can be 144p (worst), 240p, 360p, 480p, 720p (best)

Surgeon answered 14/5, 2020 at 17:54 Comment(2)
Your script is just a wrapper. But it call streamlink and this doesn't work: streamlink: command not found. I suppose last line should call youtuble-dl or yt-dlpHecklau
of course, it is wrapper around streamlink. you need to install streamlink with apt install streamlinkSurgeon
G
2

This answer has been completely rewritten on 19 Dec. 2022, after receiving the 6 downvotes.

How to download a live video? Follow the most-upvoted answer here, but use yt-dlp now wherever they use youtube-dl.

I recommend you use yt-dlp instead of youtube-dl. It's a fork off of youtube-dl and is much better-maintained and works much better. In the cases where youtube-dl gives me errors, yt-dlp works just fine. Also, in cases where youtube-dl downloads at 42 KiB/sec (which includes pretty much every time I use it--including 19 Dec. 2022 on Ubuntu 22.04), yt-dlp downloads at 86 MiB/sec, which is ~2100x faster, again, as tested on Ubuntu 22.04 seconds ago.

Tested on Ubuntu 22.04 on 19 Dec. 2022.

# Install yt-dlp
sudo apt update
sudo apt install yt-dlp

# Use it to download a video

# generally a smaller size; good quality
yt-dlp -f best https://www.youtube.com/watch?v=VUhQ6zEky0o

# bigger size; best quality
yt-dlp https://www.youtube.com/watch?v=VUhQ6zEky0o

See also my answer here where I explain this command a bit more: How to select video quality from youtube-dl?.

What youtube-dl errors did I see?

Tested on Ubuntu 18.04 and/or 20.04 on 28 Mar. 2021.

I tried following the the most-upvoted answer here but I'm getting the ERROR: VUhQ6zEky0o: YouTube said: Invalid parameters. error with youtube-dl and it's not working for me.

Sample live stream link: https://www.youtube.com/watch?v=VUhQ6zEky0o. My attempt, and the failure message:

$ youtube-dl --list-formats https://www.youtube.com/watch?v=VUhQ6zEky0o
[youtube] VUhQ6zEky0o: Downloading webpage
[youtube] VUhQ6zEky0o: Downloading video info webpage
ERROR: VUhQ6zEky0o: YouTube said: Invalid parameters.

Again, here's the error from above:

ERROR: VUhQ6zEky0o: YouTube said: Invalid parameters.

I tried youtube-dl with multiple live stream links while they were live. It didn't work for any of them. I got the error message above instead.

Solution

Use yt-dlp instead.

Last resort: use OBS studio to do a live screen capture

As a last resort, if you can't get youtube-dl nor yt-dlp to work, just do a live screen capture via OBS studio instead. Here are my detailed instructions on how to do that: Super User: How do you use OBS studio to perform screen capture (including to save live videos or make how-to tutorials)?.

This approach also works great, but is clearly not "downloading" the video stream in the same way.

Gendarmerie answered 28/3, 2021 at 4:31 Comment(7)
Here, you are not downloading the stream, but what is being displayed on your screen.Pomatum
@JoelGMathew, agreed. My answer doesn't answer the exact question directly. Rather, it answers the question the asker didn't know to ask, and the question which one might ask when the accepted answer doesn't work. it helps all those for whom the accepted answer to this question doesn't work--myself included, and it provides a universal alternative to do something similar to what the asker is asking, thereby solving the same problem but in a different way.Gendarmerie
@GabrielStaples While providing an alternative solution to a problem is most likely welcome, saying you're answering "the question the asker didn't know to ask" is incredibly questionable, to say the least. You're completely ignoring the question itself, which is specifically asking how to use youtube-dl to download it. Your solution leads to lossy recording and having to display the video in a monitor for the whole recording time, let alone installing another piece of software. May be a solution for some, but doesn't answer the question and isn't that good a solution at all.Cotterell
I'm getting a lot of downvotes here, but I put this answer here because I'm getting the ERROR: VUhQ6zEky0o: YouTube said: Invalid parameters. error with youtube-dl and it's not working for me. Anyone else getting this error message? My approach obviously is not downloading the stream, but is rather recording what is displayed on my screen, but that's the point. This is a workaround for those who are having problems with youtube-dl.Gendarmerie
This is like saying "Since your Honda Acura won't start today, I'm offering a workaround of how to build your own vehicle out of 3d printed parts". Thanks?Contra
@KaibutsuX, does the main answer always work for you? Do you ever see the Invalid parameters erorr?Gendarmerie
@JoelGMathew and others, points taken. I just rewrote this entire answer and migrated the OBS studio part of it to Super User here.Gendarmerie
V
2

As of 2023, youtube-dl seems to work fine, but note that sometimes the live stream separates the audio and video into distinct streams, then if you try to supply -f <preferred stream code> you will only get either the video or the audio.

The solution then is to simply not pass any argument, just youtube-dl https://address-to-the-stream.m3u8 and it will automatically download the best quality video stream and merge it transparently with the best audio stream via ffmpeg (tested on Windows 10).

Vasileior answered 29/1, 2023 at 0:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.