您如何使用youtube-dl下载实时流(实时流)?

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

只是想知道这是否可行-当我将视频网址复制到YouTube-dl时,它会弹出

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

此后,它从似乎不想正确复制的几行红色文本中吐出来,所以我摘录了一段代码

Picture

有人知道这是否可能吗?


我将以NASA TV的现场直播为例:

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

首先,列出视频的格式:

1
2
3
4
5
6
7
8
9
10
11
$  ~ 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)

选择您要下载的格式,然后从清单中获取视频的HLS m3u8 URL。在此示例中,我将使用94 mp4 854x480 HLS 1380k , avc1.4d401f, 30.0fps, mp4a.40.2@128k

1
2
a??  ~ 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

请注意,链接可能有所不同,并且包含到期时间戳,在这种情况下为1592099895(约6小时)。

现在您有了HLS播放列表,您可以在VLC中打开此URL并使用"记录"将其保存,或者编写一个小的ffmpeg命令:

1
2
3
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


不需要传递任何内容给ffmpeg,您只需获取所需的格式即可,在本例中为" 95"格式。

所以一旦知道它是95,您只需键入:

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

也就是说:

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

它将开始在工作目录上生成部分下载的文件<somename>.<probably mp4>.part,放开它,然后按<Ctrl-C>停止捕获。

该文件仍将命名为<something>.part,将其重命名为<whatever>.mp4,然后在其中...

ffmpeg代码:

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

也为我工作,但是声音和视频不同步,仅使用youtube-dl似乎会产生更好的结果,尽管它也使用ffmpeg

此方法的缺点是您无法在下载时观看视频,还可以打开另一个FF或Chrome,但是mplayer似乎无法处理视频输出,直到youtube-dl / ffmpeg运行。


之前,可以使用流链接下载该文件,但YouTube更改了DASH的HLS倒带功能。
因此,下面的操作方式(Prashant Adlinge评论)不再适用于YouTube:

1
streamlink --hls-live-restart STREAMURL best

更多信息在这里


某些具有m3u流式传输的网站无法在单个youtube-dl步骤中下载,您可以尝试执行以下操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ 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
....

我已经编写了一个小脚本来下载YouTube直播视频,您也可以将其用作单个命令。
脚本,可以简单地通过

调用

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

例如

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

脚本如下所示,

1
2
3
4
5
6
7
8
9
#!/bin/bash

# ytdl_lv.sh
# Author Prashant
#

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

最好的是流质量,也可以是144p(最差),240p,360p,480p,720p(最好)