如何在youtube上下载直播视频的网址

时间:2018-01-02 03:10:56

标签: javascript java c# android youtube

我正在编写一个用于从youtube下载视频的工具,包括c#中的直播视频。我正在做的是:从youtube网址获取html源代码" https://www.youtube.com/watch?v=bnpPLjEudoU&# 34;然后我会找到一个代码的javascript片段,其中包含JSON播放视频的配置,其中包含我可以下载的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 个答案:

答案 0 :(得分:2)

*最后我自己找到了一个解决方案,在youtube上播放了普通视频:  +我使用以下命令从youtube网址获取HTMLpage源代码:

    string pageSource = WebClient.DownloadString(youtubeURL)   

+来自HTML源代码我找到了想要使用Regularexpression下载的视频的配置,并将我发现的内容转换为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对象将具有如下格式:

{

" attrs":{

"id": "movie_player"

},

" params":{

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

},

" args":{

"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"

},

" url":"",

" 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时,我会找到所有可以使用以下内容下载视频的网址:

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

*使用直播视频我会像普通视频一样完成相同的过程,但在最后一步,我不得不获取可下载的网址列表,我必须通过以下方式获取.m3u8网址:

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

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