如何在 youtube 上获取可下载的直播视频 URL

Posted

技术标签:

【中文标题】如何在 youtube 上获取可下载的直播视频 URL【英文标题】:How to get download able url of live stream videos on youtube 【发布时间】:2018-06-11 19:37:06 【问题描述】:

我正在编写一个工具,用于从 youtube 下载视频,包括 c# 中的直播视频。我正在做的是:从 youtube url 获取 html 源,例如“https://www.youtube.com/watch?v=bnpPLjEudoU”,然后我会找到一段 javascript 代码片段包含以 JSON 格式播放视频的配置,其中包含实际视频 url 列表,我可以下载 1080p-720p-480p-360p 等,它与普通视频和一些直播视频完美配合,但与 Fox 配合使用News Live chanel("https://www.youtube.com/watch?v=bnpPLjEudoU") 我找不到 080p-720p-480p-360p 除了 .m3u8 网址之外的任何网址

("https://manifest.googlevideo.com/api/manifest/hls_variant/keepalive/yes/id/bnpPLjEudoU.1~23716582/go/1/gcr/vn/sparams/ei,gcr,go,hfr,id,ip,ipbits,itag,maudio,playlist_type,ratebypass,requiressl,source,tx,txs,expire/requiressl/yes/itag/0/playlist_type/DVR/signature/3696DA0D44C88FCB370F27A780985017099507B2.9D3CB7209C75E0656424848394DAB3C65161365D/txs/9486106,9486107,9486108,9486113,9486114/maudio/1/expire/1514883856/ipbits/0/ratebypass/yes/dover/8/source/yt_live_broadcast/ip/118.107.71.247/key/yt6/ei/sPZKWu7RG4qx4AKKwJmACg/tx/9486113/hfr/1/file/index.m3u8"),

我已经用 VLC 媒体播放器尝试了那个 .m3u8 网址,但屏幕上只有一个图像显示。所以有人请告诉我我从 youtube 获取视频的解决方案好吗?有没有更好的办法?我正在用 c# 编写工具,但我可以切换到任何其他编程语言。

【问题讨论】:

【参考方案1】:

*最后我自己找到了一个解决方案,在 youtube 上有正常的视频: +我使用以下方式从 youtube url 获取 HTMLpage 源代码:

    string pageSource = WebClient.DownloadString(youtubeURL)   

+从 HTML 源代码中,我找到了我想使用正则表达式下载的视频的配置,并将找到的内容转换为 JSON:

    var dataRegex = new Regex(@"ytplayer\.config\s*=\s*(\.+?\);", RegexOptions.Multiline);
        string extractedJson = dataRegex.Match(pageSource).Result("$1");
        Object[] objs = new Object[]  url, JObject.Parse(extractedJson) ;
我得到的

+Json 对象的格式如下:

“属性”:

"id": "movie_player"

,

“参数”:

"allowscriptaccess": "always",
"allowfullscreen": "true",
"bgcolor": "#000000"

,

“参数”:

"c": "WEB",
"vmap": "",
"iv_load_policy": "1",
"innertube_api_version": "v1",
"xhr_apiary_host": "youtubei.youtube.com",
"length_seconds": "3826",
"relative_loudness": "-8.05900001526",
"pltype": "contentugc",
"title": "",
"host_language": "vi",
"enablecsi": "1",
"vm": "CAEQARgE",
"eventid": "gN9VWsD2HYu1qQHYh6DYCQ",
"plid": "AAViaNGgboLtCsQo",
"watermark": "",
"enablejsapi": "1",
"no_get_video_log": "1",
"loaderUrl": "https://www.youtube.com/watch?v=oGeAFd9zjDI",
"vss_host": "s.youtube.com",
"oid": "h6xIstk06YQWVvfO-FkUjg",
"cver": "1.20180108",
"ldpj": "-35",
"timestamp": "1515577216",
"thumbnail_url": "https://i.ytimg.com/vi/oGeAFd9zjDI/default.jpg",
"view_count": "941366",
"idpj": "-9",
"fexp":"",
"loudness": "-29.0590000153",
"allow_ratings": "1",
"pyv_ad_channel": "",
"apiary_host": "",
"tmi": "1",
"fmt_list": "22/1280x720,43/640x360,18/640x360,36/320x180,17/176x144",
"video_id": "oGeAFd9zjDI",
"subtitles_xlb": "https://s.ytimg.com/yts/xlbbin/subtitles-strings-vi_VN-vfl-WQ1GC.xlb",
"videostats_playback_base_url": " ",
"player_response": "",
"ptk": "youtube_single",
"ucid": "UCIt0gjo6BZk37cLWm04LmCQ",
"apiary_host_firstparty": "",
"t": "1",
"swf_player_response": "1",
"ssl": "1",
"avg_rating": "4.11271858215",
"allow_embed": "1",
"is_listed": "1",
"cr": "VN",
"itct": "CAMQu2kiEwjAtvmMjc3YAhWLWioKHdgDCJso-B0=",
 "atc":"",
 "fflags":"",
"cl": "181278566",
"iv_invideo_url": "",
"player_error_log_fraction": "1.0",
"of": "Ds_CbA89Q3Fyo-EOejl1bA",
"token": "1",
"external_play_video": "1",
"show_pyv_in_related": false,
"account_playback_token": "",
"innertube_api_key": "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8",
"csi_page_type": "watch,watch7_html5",
"adaptive_fmts": "",
"gapi_hint_params": "",
"author": "Mr Dolphin",
"ppv_remarketing_url": "",
"innertube_context_client_version": "1.20180108",
"hl": "vi_VN",
"url_encoded_fmt_stream_map": "list of actual urls for 1080p,720p,360p and so on, that will be able to download after decode",
"iv3_module": "1",
"keywords": " ",
"ismb": "8340000",
"watch_xlb": "https://s.ytimg.com/yts/xlbbin/watch-strings-vi_VN-vflzKZ5ht.xlb"

,

“网址”:“”,

"min_version": "8.0.0",

“sts”:17536,

“资产”:

"js": "/yts/jsbin/player-vfluepRD8/vi_VN/base.js",
"css": "/yts/cssbin/player-vfl_z2Ycb/www-player.css"

, “html5”:真

+现在,当我拥有 JsonObject 时,我会找到所有可以下载视频的 url:

     string urlsToDownload = json["args"]["url_encoded_fmt_stream_map"].ToString();

*对于直播视频,我经历了与普通视频相同的过程,但在最后一步,我必须通过以下方式获取 .m3u8 网址,而不是获取可下载网址的列表:

   string downloadUrl = WebUtility.UrlDecode(json["args"]["hlsvp"].ToString());

+当我有 .m3u8 网址时,我使用“FFMPEG”下载直播视频

【讨论】:

从“hlsvp”和youtube-dl得到的url不同。 “hlsvp”方法很简单,但它只是返回最低质量的流吗?

以上是关于如何在 youtube 上获取可下载的直播视频 URL的主要内容,如果未能解决你的问题,请参考以下文章

从 JavaScript 创建一个可下载的附件?

如何嵌入新的 Youtube 直播视频永久 URL?

在变量中获取 YouTube 视频 URL [关闭]

如何在我的 Android 应用中显示 YouTube 浏览? [关闭]

万能视频下载软件 Downie v3.0.7 Multilingual MacOSX

在 Heroku 中构建可下载的站点地图 zip 文件