Asp.net blogs

Wednesday 3 July 2013

Upload Video on Youtube using Asp.Net (c#) and play the url within your website.




First you need to download the following dlls from Internet via Google Search Engine :

1. Google.GData.Client.DLL
2. Google.GData.Extensions.dll
3. Google.GData.YouTube.dll
4. Newtonsoft.Json.dll


 Following c# code is usable to upload video on You Tube and play it in on your website :

C# Code :

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Google.YouTube;
using Google.GData.YouTube;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Extensions.MediaRss;
using System.IO;
using Google.GData.Extensions.Location;
using System.Threading;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    string developerKey = "AI39si7ETkiMUFrMQtfkuYcOoaxs1H8QMso8R0YTzApg-alwA9XebuWKUCm4e_tbUHWvU9Of5Iwr6gXPQOwFwTa5D7gn3oecRg";
    string username = "abc@gmail.com"; // Gmail username
    string password = "123456"; // Gmail Password
    public string youtubeurl;
    
protected void Button3_Click(object sender, EventArgs e)
    {
        string FileName = FileUpload1.FileName;
        string fillPath = FileUpload1.PostedFile.FileName;
        var newp = Server.MapPath("download/" + FileName);
        FileUpload1.SaveAs(Server.MapPath("download/" + FileName));
        YouTubeRequestSettings settings = new YouTubeRequestSettings("applicationName", developerKey, username, password);

        settings.Timeout = int.MaxValue;
        YouTubeRequest request = new YouTubeRequest(settings);
        Video newVideo = new Video();
        newVideo.Title = "Test";
        newVideo.Tags.Add(new MediaCategory("Animals", YouTubeNameTable.CategorySchema));
        newVideo.Description = "Testing Testing Testing";
        newVideo.YouTubeEntry.Private = false;
        newVideo.YouTubeEntry.MediaSource = new MediaFileSource(newp, "video/mpeg");
        Video A = request.Upload(newVideo);
        youtubeurl = "http://www.youtube.com/v/" + A.VideoId + "&hl=en_US&fs=1&rel=0";
        System.IO.File.Delete(Request.PhysicalApplicationPath + "download/" + FileName);


    }
}

Output Shown as below :




No comments:

Post a Comment