Wednesday 4 January 2017

How to get the Youtube VideoId from Url and generate image thumbnail in C#

To get the Youtube VideoId from an Url enerate image thumbnail in C#  you can use the following snippet.

Sample Input


  • http://youtu.be/AAAAAAAAA01
  • http://www.youtube.com/embed/watch?feature=player_embedded&v=AAAAAAAAA02
  • http://www.youtube.com/embed/watch?v=AAAAAAAAA03
  • http://www.youtube.com/embed/v=AAAAAAAAA04
  • http://www.youtube.com/watch?v=AAAAAAAAA06
  • www.youtube.com/watch?v=AAAAAAAAA07
  • www.youtu.be/AAAAAAAAA08
  • youtu.be/AAAAAAAAA09
  • http://www.youtube.com/watch?feature=player_embedded&v=AAAAAAAAA05
  • http://www.youtube.com/watch?v=i-AAAAAAA14&feature=related
  • http://www.youtube.com/attribution_link?u=/watch?v=AAAAAAAAA15&feature=share&a=9QlmP1yvjcllp0h3l0NwuA
  • http://www.youtube.com/attribution_link?a=fF1CWYwxCQ4&u=/watch?v=AAAAAAAAA16&feature=em-uploademail
  • http://www.youtube.com/attribution_link?a=fF1CWYwxCQ4&feature=em-uploademail&u=/watch?v=AAAAAAAAA17
  • http://www.youtube.com/v/A-AAAAAAA18?fs=1&rel=0
  • youtube.com/watch?v=AAAAAAAAA10
  • http://www.youtube.com/watch/AAAAAAAAA11
  • http://www.youtube.com/v/AAAAAAAAA12
  • http://www.youtube.com/v/AAAAAAAAA13


  •   /// <summary>
      ///  Method to reslove the youtube urls into image URL
      /// </summary>
      /// <param name="youtubeUrl"></param>
      /// <returns>imageURL</returns>
      public string GetYouTubeThumbnail(string youtubeUrl)
        {
          string imageURL = string.Empty;
          string youTubeThumb = string.Empty;
          if (youtubeUrl == "")
            return "";
          if (youtubeUrl.IndexOf("/embed/") > 0)
          {
            string strVideoCode = youtubeUrl.Substring(youtubeUrl.IndexOf("/embed/") + 7);
            int ind = strVideoCode.IndexOf("?");
            youTubeThumb = strVideoCode.Substring(0, ind == -1 ? strVideoCode.Length : ind);
          }
          else if (youtubeUrl.IndexOf("v=") > 0)
          {
            string strVideoCode = youtubeUrl.Substring(youtubeUrl.IndexOf("v=") + 2);
            int ind = strVideoCode.IndexOf("?");
            youTubeThumb = strVideoCode.Substring(0, ind == -1 ? strVideoCode.Length : ind);
          }
          
          else if (youtubeUrl.IndexOf("/v/") > 0)
          {
            string strVideoCode = youtubeUrl.Substring(youtubeUrl.IndexOf("/v/") + 3);
            int ind = strVideoCode.IndexOf("?");
            youTubeThumb = strVideoCode.Substring(0, ind == -1 ? strVideoCode.Length : ind);
          }
          else if (youtubeUrl.IndexOf("/") > 6)
          {
            youTubeThumb = youtubeUrl.Split('/')[1];
          }
          
          if (youTubeThumb != string.Empty && youTubeThumb != "" && youTubeThumb != null)
          {
            imageURL = "http://img.youtube.com/vi/" + youTubeThumb + "/mqdefault.jpg";
          }
          return imageURL;
        }    


    Monday 28 March 2016

    About MVC In ASP.NET :
    MVC stands for model-view-controller.  MVC is a pattern for developing applications that are well architected, testable  and easy to maintain. MVC-based applications contain:
    • Models: Classes that represent the data of the application  and that use validation logic to enforce business rules for that data.
    • Views: Template files that your application uses to dynamically  generate HTML responses.
    • Controllers: Classes that handle incoming browser requests,  retrieve model data, and then specify view templates that return a response  to the browser.