Category : C# | Author : Chtiwi Malek | First posted : 10/23/2012 | Updated : 12/13/2012
Tags : youtube, .net, c#, upload, video, videos, api, google, delete, guide, how to
Upload a Video to YouTube with C# .Net

Upload a Video to YouTube with C# .Net

My last client project was an audio/video c# application where users upload their educational videos, audios, notes…  The website basically stores the uploaded videos on Youtube and the users watch the embedded video via an enhanced custom player. That’s cool, no need for video storage servers and huge bandwidth.

In this entry I’m sharing the process and functions I wrote to get a video file uploaded to (or Deleted from) Youtube.

First, to interact with Youtube we will use the Api provided by google for the .Net environment.

If you haven’t already downloaded the “Google_Data_Api” here’s the Link :
http://code.google.com/p/google-gdata/downloads/list

Three Dlls need to be referenced in our project (Google.GData.Client.dll, Google.GData.Extensions.dll, Google.GData.YouTube.dll). Click on add references and search for them, or just copy the files to the Bin directory.

The next step is Getting a Youtube Api Developper Key, to obtain a developer key visit http://code.google.com/apis/youtube/dashboard/

Now we can start coding: add these classes declaration:
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.GData.Extensions.MediaRss;
using Google.YouTube;
using Google.GData.Client;
and here’s the function you need to upload a video to Youtube:
public static string UploadVideo(string FilePath,string Title, string Description)
{
   YouTubeRequestSettings settings;
   YouTubeRequest request;
   string devkey = "YOUR DEVELOPPER KEY HERE";
   string username = "Your Youtube Username";
   string password = "Your Youtube Password";
   settings = new YouTubeRequestSettings("Your Application Name", devkey, username, password) {Timeout = -1};
   request = new YouTubeRequest(settings);
	        
   Video newVideo = new Video();
	
   newVideo.Title = Title;
   newVideo.Description = Description;
   newVideo.Private = true;
   newVideo.YouTubeEntry.Private = false;
	
   //newVideo.Tags.Add(new MediaCategory("Autos", YouTubeNameTable.CategorySchema));
	       
   //newVideo.Tags.Add(new MediaCategory("mydevtag, anotherdevtag", YouTubeNameTable.DeveloperTagSchema));
	
   //newVideo.YouTubeEntry.setYouTubeExtension("location", "Paris, FR");
   // You can also specify just a descriptive string ==>
   // newVideo.YouTubeEntry.Location = new GeoRssWhere(71, -111);
   // newVideo.YouTubeEntry.setYouTubeExtension("location", "Paris, France.");
	
   newVideo.YouTubeEntry.MediaSource = new MediaFileSource(FilePath, "video/mp4");
   Video createdVideo = request.Upload(newVideo);
	
   return createdVideo.VideoId;
}
The previous function will return the Video ID, you should store that string because you’ll need it to access, play, delete... the video later.

The next function will delete a video, you’ll need to provide the Video ID :
public static bool DeleteVideo(string VideoId)
{
   try
   {
      YouTubeRequestSettings settings;
      YouTubeRequest request;
      string devkey = "YOUR DEVELOPPER KEY HERE";
      string username = "Your Youtube Username";
      string password = "Your Youtube Password";
      settings = new YouTubeRequestSettings("Your Application Name", devkey, username, password) { Timeout = -1 };
      request = new YouTubeRequest(settings);
	
      Uri videoEntryUrl = new Uri(String.Format("http://gdata.youtube.com/feeds/api/users/{0}/uploads/{1}", username, VideoId));
      Video video = request.Retrieve<Video>(videoEntryUrl);
      request.Delete(video);
	
      return true;
   }
   catch (Exception ex)
   {
      return false;
   }
}
You can extend this code to include other actions (Updating a video, retrieving standard video feeds, video comments, responses... ), for more check https://developers.google.com/youtube/2.0/developers_guide_protocol

if you like this tutorial, please Share it.
Have fun and don't hesitate to comment or ask questions.
About the author :
Malek Chtiwi is the man behind Codicode.com
34 years old full stack developer.
Loves technology; but also likes design, photography and composing music.
Comments & Opinions :
Fantastic
Fantastic
- by Wdwsd on 2/15/2013
share
you can share the source code of application, I need it for work
- by Huynhthang on 3/27/2013
send me the sample code
plz send me the sample code
- by govinda on 8/1/2013
Not working! :(
Not working code! :(
- by Sedat on 9/9/2013
Not Working
Not Working
- by uzair on 9/19/2013
Uploading Video to youtube Using APi(V3)
Hello Malek Chtiwi
Followed ur code it worked fine and able to do upload and delete but i need a sample code that uploads video using youtube api(v3)

Thanks in advance
Aravind.
- by Aravind on 11/30/2013
i m geting error Execution of request failed: https://uploads.gdata.youtube.com/feeds/api/users/default/uploads can u send me code 
- by vimlesh on 7/22/2015
How to get Response When Uploading?
How do i get response when uploading video and if the video uplading is stuck than it will be automatically upload again...
- by Saurabh on 7/22/2014
Uploading Video to YouTube Using API(V3)
When I go and click on the developer key link, all it does is ask me to sign in, and then goes to a 404 Not Found? Got an updated link?
- by Micah on 11/19/2014
Delete Video Uploaded on Youtube
Hi..,
I have Uploaded video on youtube and trying to delete the video using your the upper code but it is showing that "No longer available".
Please help me to solve this error.
Thanks.
- by Naushad Ansari on 6/16/2015
i m geting error Execution of request failed: https://uploads.gdata.youtube.com/feeds/api/users/default/uploads can u send me code 
- by vimlesh on 7/22/2015
plz send me the uploading video code
 
- by kotishwar on 12/13/2016
Not Working
Hello Malek Chtiwi,
I Followed your code, it's working but when i going to upload any video it's getting error :

Invalid Crediantial. Please help
 
- by Jitendra Behera on 7/23/2015
I'm having the same issue.
- by Jake on 8/7/2015
Same issue for me.
Invalid credentials
- by Kundan Kumar on 8/25/2016
Отличный код
Ебал сейчас Хованского, фу, блядь, заебал он ныть, постоянно ноет - выеби меня, выеби меня,... говноед ёбаный... Ну и ебал я его ебал, а потом думаю, ебать, а дайка я зайду на мой любимый сайт и вот я здесь.
Код кстати шняга полная, устарел он в бездонную пиздятину.
Автор тоже говноед, людей кормит говном. Сука, увижу еще раз, что выкладываешь говно, выебу хуем вонючую жопу Хованского, а потом обосранный хуй засуну тебе в рот. Ты понял автор? Мразота ебаная.
Сучью вонючее, я вас блядь научу жить. Будете сраться при видеменя, уёбы драные.
- by Ебарь Хованского on 2/12/2016
this code now expired
:(
- by Dinesh Nisshanka on 6/28/2016
Binod8541
Happy
- by Binod kuamr on 10/11/2016
Not Working
Hi, i'm getting invalid credentials exeption
- by kotishwar on 12/6/2016
Leave a Comment:
Name :
Email : * will not be shown
Title :
Comment :


» Member access