-->

How do you use youtube-dl to download live streams

2020-02-07 14:14发布

问题:

Just wondering if that's possible or not- 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

Does anyone know if this is possible?

回答1:

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

https://www.youtube.com/watch?v=6aXR-SL5L2o

First, list the formats for the video:

$  ~ youtube-dl --list-formats https://www.youtube.com/watch\?v\=6aXR-SL5L2o
[youtube] 6aXR-SL5L2o: Downloading webpage
[youtube] 6aXR-SL5L2o: Downloading video info webpage
[youtube] 6aXR-SL5L2o: Extracting video information
[youtube] 6aXR-SL5L2o: Downloading formats manifest
[youtube] 6aXR-SL5L2o: Downloading MPD manifest
[info] Available formats for 6aXR-SL5L2o:
format code  extension  resolution note
91           mp4        144p       HLS , h264, aac  @ 48k
92           mp4        240p       HLS , h264, aac  @ 48k
93           mp4        360p       HLS , h264, aac  @128k
94           mp4        480p       HLS , h264, aac  @128k
95           mp4        720p       HLS , h264, aac  @256k (best)

Pick the format you wish to download, and fetch the HLS m3u8 URL of the video from the manifest. I'll be using 95 mp4 720p HLS , h264, aac @256k for this example:

➜  ~ youtube-dl -f 95 -g https://www.youtube.com/watch\?v\=6aXR-SL5L2o
https://manifest.googlevideo.com/api/manifest/hls_playlist/id/6aXR-SL5L2o.1/itag/95/source/yt_live_broadcast/requiressl/yes/ratebypass/yes/live/1/cmbypass/yes/goi/160/sgoap/itag%3D140/sgovp/itag%3D136/hls_chunk_host/r16---sn-ab5l6ne7.googlevideo.com/gcr/us/playlist_type/DVR/mm/32/mn/sn-ab5l6ne7/ms/lv/mv/u/pl/17/dover/3/fexp/9416126,9416891,9419451,9422596,9423554,9427790,9428193,9428398,9428974,9429011,9431012,9431164,9432683,9433096,9433946,9434343,9434833,9434847,9435186,9435741/upn/UNF1TiBtavY/sver/3/mt/1462544320/ip/64.125.177.124/ipbits/0/expire/1462565936/sparams/ip,ipbits,expire,id,itag,source,requiressl,ratebypass,live,cmbypass,goi,sgoap,sgovp,hls_chunk_host,gcr,playlist_type,mm,mn,ms,mv,pl/signature/67EC57CB964D9D944DE2E1AB40F0E496DA2C73B4.8FBEF1974DF9DB1E614B9C96BA0FF8E52076B04F/key/dg_yt0/playlist/index.m3u8

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/id/6aXR-SL5L2o.1/itag/95/source/yt_live_broadcast/requiressl/yes/ratebypass/yes/live/1/cmbypass/yes/goi/160/sgoap/itag%3D140/sgovp/itag%3D136/hls_chunk_host/r16---sn-ab5l6ne7.googlevideo.com/gcr/us/playlist_type/DVR/mm/32/mn/sn-ab5l6ne7/ms/lv/mv/u/pl/17/dover/3/fexp/9416126,9416891,9419451,9422596,9423554,9427790,9428193,9428398,9428974,9429011,9431012,9431164,9432683,9433096,9433946,9434343,9434833,9434847,9435186,9435741/upn/UNF1TiBtavY/sver/3/mt/1462544320/ip/64.125.177.124/ipbits/0/expire/1462565936/sparams/ip,ipbits,expire,id,itag,source,requiressl,ratebypass,live,cmbypass,goi,sgoap,sgovp,hls_chunk_host,gcr,playlist_type,mm,mn,ms,mv,pl/signature/67EC57CB964D9D944DE2E1AB40F0E496DA2C73B4.8FBEF1974DF9DB1E614B9C96BA0FF8E52076B04F/key/dg_yt0/playlist/index.m3u8 \
-c copy output.ts


回答2:

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.



回答3:

You can also type this :

URL=https://www.youtube.com/watch?v=6aXR-SL5L2o
youtube-dl -f 95 --hls-prefer-native $URL

For some other websites with m3u streaming content where this does not work, you can try something like this :

$ youtube-dl -F https://www.arte.tv/fr/videos/078132-001-A/cosmos-une-odyssee-a-travers-l-univers/ | grep m3u
HLS_XQ_2     m3u8       1280x720   VA-STA, Allemand 2200k 
HLS_XQ_1     m3u8       1280x720   VF-STF, Français 2200k 
$ youtube-dl -gf HLS_XQ_1 --get-filename https://www.arte.tv/fr/videos/078132-001-A/cosmos-une-odyssee-a-travers-l-univers/
https://arteptweb-vh.akamaihd.net/i/am/ptweb/078000/078100/078132-001-A_0_VF-STF_AMM-PTWEB_XQ.1AhsDgVG20.smil/master.m3u8
Cosmos_une_odyssee_a_travers_l_univers__HLS_XQ_1__078132-001-A.m3u8
$ youtube-dl -F https://arteptweb-vh.akamaihd.net/i/am/ptweb/078000/078100/078132-001-A_0_VF-STF_AMM-PTWEB_XQ.1AhsDgVG20.smil/master.m3u8
[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 https://arteptweb-vh.akamaihd.net/i/am/ptweb/078000/078100/078132-001-A_0_VF-STF_AMM-PTWEB_XQ.1AhsDgVG20.smil/master.m3u8 -o Cosmos_une_odyssee_a_travers_l_univers__HLS_XQ_1__078132-001-A.mp4
[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
....

BTW : I have added --hls-prefer-native in my ~/.config/youtube-dl.conf so I don't have to type it every time :)