Trevor Sullivan's Tech Room

Minding the gap between administration and development

Uploading a Video to Youtube via PowerShell

Posted by Trevor Sullivan on 2010/03/09


Hey guys! I know it’s been a long time since I have posted anything useful (that is, assuming anything I post is useful! haha). While sitting at Starbucks today, I suddenly came up with an idea for a new post though, and after doing a quick bit of research, figured I would write about it! The purpose of today’s post is to show an example of using PowerShell to upload a video to Youtube! This could be useful in a variety of circumstances, but I’ll leave finding the use cases up to each of you individually 🙂 A couple of examples I can think of would be:

  • Batch uploading a folder of videos
  • Automating the upload of videos that may be somehow generated into a folder automatically
  • …. ???

In any event, in my search to find a solution, knowing that PowerShell is based on the Microsoft .NET Framework, I searched for “C# upload youtube”. One of the first results that came up was a blog called “Trails in the Sand,” and more specifically, a post entitled “Programmatically Uploading Videos to Youtube.” This blog article goes into detail about uploading videos to Youtube using C#/.NET, but at the bottom of the post, the author was kind enough to include the source code for a library he had written! As a PowerShell scripter, you’ve probably learned to be very thankful for other people who write and share .NET libraries to do “cool stuff!”

After downloading the “Trails in the Sand Youtube Library,” I opened it up in Visual C# 2008 Express Edition (free, fully-functional version of Visual Studio), and converted the project to Visual Studio 2008 format, using the wizard that pops up. The conversion was successful, so I proceeded to create a developer key for Youtube. You can generate your own developer key at the following URL:

http://code.google.com/apis/youtube/dashboard/

After pasting my developer key into the project under the “devCode” string constant, I compiled the library by hitting “F6.” This created my library under the “bin\release” folder of the project. Next, I opened Quest’s PowerGUI (a great, free PowerShell editor), and started a new script. First things first, using the System.Reflection.Assembly .NET class, I loaded the newly created .NET Youtube library into PowerShell:

[Reflection.Assembly]::LoadFile(“c:\YouTube.dll”)

Next, I instantiated the Trails.YouTube object:

$Yt = New-Object Trails.YouTube

Once I had the YouTube object, I defined a string variable to use as an “out” parameter, to capture any resulting error (if you don’t pre-define it, you’ll get an error):

$UploadError = “”

Next, we call the Authorize() method, passing our username and password:

$Yt.Authorize(“username”, “password”)

And then finally, we upload the video using the Upload() method! Parameters are as follows:

  • Title
  • Description
  • Category (library has a spelling error: “catagory”)
  • Video keywords
  • Path to local video file
  • Error variable (pre-defined variable, used as an “out” parameter)

$result = $Yt.Upload(“Test upload”, “test upload”, [Trails.YouTube+Catagory]::Howto, “keyword1”, “C:\Users\trevor.sullivan\Videos\SMS_BootImagePackage.wmv”, [ref] $UploadError)

Optionally, you can print out any resulting error message:

$result

The entire script put together looks like this:

$LibYt = [System.Reflection.Assembly]::LoadFile(“c:\Users\trevor.sullivan\Documents\TrailsintheSand.com-YouTubeLibrary\YouTube\bin\Release\YouTube.dll”)

$Yt = New-Object Trails.YouTube

$UploadError = “”

$Yt.Authorize($(Read-Host -Prompt “Please enter username”), $(Read-Host -Prompt “Please enter password”))

$result = $Yt.Upload(“Test upload”, “test upload”, [Trails.YouTube+Catagory]::Howto, “keyword1”, “C:\Users\trevor.sullivan\Videos\SMS_BootImagePackage.wmv”, [ref] $UploadError)

$result

If we wanted to “variable-ize” (yes, I made that up just now) this script a bit more, we could do something like this:

### Define YouTube library path

$YtDll = “c:\Users\trevor.sullivan\Documents\TrailsintheSand.com-YouTubeLibrary\YouTube\bin\Release\YouTube.dll”

### Get YouTube username and password

$YtUser = $(Read-Host -Prompt “Please enter username”)

$YtPass = $(Read-Host -Prompt “Please enter password”)

######## Set up video variables ########

$vTitle = “Video Title”

$vDesc = “Video Description”

$vCat = [Trails.YouTube+Catagory]::Howto

$vKeyword = “keyword1”

$vPath = “C:\Users\trevor.sullivan\Videos\SMS_BootImagePackage.wmv”

$vErr = “”

### Load YouTube library, login, upload video, and echo error

$LibYt = [System.Reflection.Assembly]::LoadFile($YtDll)

$Yt = New-Object Trails.YouTube

$Yt.Authorize($YtUser, $YtPass)

$result = $Yt.Upload($vTitle, $vDesc, $vCat, $vKeyword, $vPath, [ref] $vErr)

$result

Hope this post helps!

Trevor

Advertisements

5 Responses to “Uploading a Video to Youtube via PowerShell”

  1. Sitaram said

    Thanks for the article trevor. That is why I like powershell :-). More easy…more granular…

  2. Robin said

    Is it possible to get a copy of your youtube.dll file please?

  3. Robin said

    Haha! I just realised the stupidity of my request! The key goes into the dll, not the script… therefore I won’t be needing your dll 😉 Thanks for the article though – gonna crack on with it now 🙂

  4. KamikaZee said

    Just tried to make this work, but every time I wanted to upload a video the method threw an exception.
    The problem is that for larger files (>150MB) the timeout set in the youtube.dll is set too low. I incresed it from 5 to 30 minutes, fixing it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: