【问题标题】:C# Youtibe Video TitleC# Youtube 视频标题
【发布时间】:2016-03-27 12:05:35
【问题描述】:

我一直在环顾四周,发现的所有答案要么不是 C#,要么使用 V2.0 API,要么使用的方法不会为禁用嵌入的视频返回有效结果。

这是我目前正在使用的,来自我不久前找到的回复:

        public string GetYouTubeTitle(string url)
    {
        string id = GetArgs(url, "v", '?');
        WebClient client = new WebClient();
        return "YouTube Video: \"" + GetArgs(client.DownloadString("http://youtube.com/get_video_info?video_id=" + id), "title", '&') + "\""; 
    }

    public string GetArgs(string args, string key, char query)
    {
        int iqs = args.IndexOf(query);
        string querystring = null;

        if (iqs != -1)
        {
            querystring = (iqs < args.Length - 1) ? args.Substring(iqs + 1) : string.Empty;
            NameValueCollection nvcArgs = HttpUtility.ParseQueryString(querystring);
            return nvcArgs[key];
        }
        return string.Empty;
    }

第一次运行完美,但我测试的第二个视频返回错误:“&errorcode=150&reason=This+video+contains+content+from+Studio71_1_1.+It+is+restricted+from+playback+on +某些+网站”

我的目标只是在给定 URL 的情况下获取 youtube 视频的标题。但我所看到的一切要么含糊不清,要么仅对 2.0 API 有效,甚至不再适用。

编辑:在某些时候,我可能想在响应中添加更多内容,例如视频的长度。因此,如果可能的话,我宁愿让 API 正常工作,也不愿只使用 get_video_info 之类的东西。

编辑: 为了完整起见并帮助以后可能会查看此内容的任何人,这就是我现在所拥有的,可以按我想要的方式工作:

            string titleurl = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id=" + GetArgs(url, "v", '?') + "&key=" + apikey;
            string timeurl = "https://www.googleapis.com/youtube/v3/videos?id=" + GetArgs(url, "v", '?') + "&part=contentDetails&key="  + apikey;

            HttpWebRequest titlerequest = (HttpWebRequest)WebRequest.Create(titleurl);
            HttpWebResponse titleresponse = (HttpWebResponse)titlerequest.GetResponse();
            Stream titlestream = titleresponse.GetResponseStream();
            StreamReader titlereader = new StreamReader(titlestream);
            string titlejson = titlereader.ReadToEnd();
            Newtonsoft.Json.Linq.JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(titlejson);

            string title = (string)jObject["items"][0]["snippet"]["title"];

            HttpWebRequest timerequest = (HttpWebRequest)WebRequest.Create(timeurl);
            HttpWebResponse timeresponse = (HttpWebResponse)timerequest.GetResponse();
            Stream timestream = timeresponse.GetResponseStream();
            StreamReader timereader = new StreamReader(timestream);
            string timejson = timereader.ReadToEnd();
            Newtonsoft.Json.Linq.JObject jObjectTime = Newtonsoft.Json.Linq.JObject.Parse(timejson);

            string time = ParseTime((string)jObjectTime["items"][0]["contentDetails"]["duration"]);

            if (title.Length > 0 && time.Length > 0 && time != "0")
                return "YouTube Video: \"" + title + "\" (" + time + ")";
            else return error; //error is a preset generic error string

这可能不是最干净的代码,但它正在做我想做的事。所以我真的不介意。

【问题讨论】:

    标签: c# api video youtube title


    【解决方案1】:

    您可以使用 YouTube Data API v3 获取视频标题:

    1. 您需要从Here 获取您的 API 密钥:

    1. 按照documentation 中的说明向此 URL 发出 GET 请求:

    https://www.googleapis.com/youtube/v3/videos?part=snippet&id=v2AC41dglnM,kTHNpusq654&key=YourAPIKeyHere

    1. 使用Json.NET 之类的内容解析您收到的回复:

    【讨论】:

      【解决方案2】:

      试试这个:

      首先,您需要在 Visual Studio 包管理器上安装 VideoLibrary:

      **Install-Package VideoLibrary** 
      
        using VideoLibrary;
      

      让我们编写一个函数来获取 YouTube 视频标题:

            public string getTitle (string url){
      
              YouTube ytb = YouTube.Default; //starting point for YouTube actions
              var vid = ytb.GetVideo(url); // gets a Video object with info about the video
             
              string ttl = vid.Title;//get video Title
               return ttl;
      
           }
      

      【讨论】:

      • 请添加解释,您的代码如何以及为何回答问题
      猜你喜欢
      • 2017-05-27
      • 1970-01-01
      • 2013-03-23
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      • 2012-05-22
      • 2015-10-23
      • 2010-11-16
      相关资源
      最近更新 更多