YouTube Uploader


This script no longer works. The correct way to do this now is to use YouTube’s API.

There are plenty of tutorials on how to use YouTube to get content. But after much searching I couldn’t find a way to upload videos to Youtube.

So with the help of a friend to get me started, and some diligent searching to fill in the missing pieces, I came up with this code.

It’s important to note that this is article is geared towards experienced programmers. If you are a typical user trying to upload your best bet is to use YouTube’s webpage. You will need to understand and modify some parts of this code in order to get it working in your environment.

Below is the main function that does the dirty work. We are using curl for our uploading. I’ve tried commenting the best I can.

The Posting Function

<?php

define('DEBUG','FALSE');

function http_post_form($url, $vars) {
  $ch = curl_init();
  $timeout = 0; // set to zero for no timeout
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return to string instead of spewing to output
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  // follow location header, not sure if this is needed.
  // I like chocolate chip
  curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
  curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");

  // Expect: 100-continue doesn't work properly with lightTPD
  // This fix by zorro http://groups.google.com/group/php.general/msg/aaea439233ac709b
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); 

  // Debugging
  if(defined(DEBUG) && DEBUG == TRUE) {
    $mydebug = f-open('debug.txt','a');
    curl_setopt($ch, CURLOPT_STDERR, $mydebug);
    curl_setopt($ch, CURLOPT_VERBOSE, 1); 
  }

  // Set method post
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);

  $file_contents = curl_e xec($ch);

  if(defined(DEBUG) && DEBUG == TRUE) {
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    f write($mydebug,"/nHttp Code: $httpCode/n");
  }

  curl_close($ch);

  if(defined(DEBUG) && DEBUG == TRUE) {
    f close($mydebug);
  }

  return $file_contents;
}

?>

That function essentially does the job of filling out a form and submitting it to the server for us. While there are a few lines of code to get it working with YouTube’s servers this function could certainly be useful for any automated posting means.

Logging In

The first step in our YouTube journey is logging in. $user and $password needs to be replaced with your YouTube login information.

$user = 'user';
$password = 'password';

/* don't touch these, we are just setting up our ugly strings with our information to pass onto our super function above. */
$param = 'current_form=loginForm&next=/my_videos_upload%3F&username='.$user.'&password='.$password.'&action_login=Log+In';
$url = 'http://www.youtube.com/signup?next=/my_videos_upload%3F';

http_post_form($url, $param);

Posting Your Video

$title, $description and the filename of the video to be uploaded change every time. I insert this information using a simple form webpage. The other information stays the same for all of my ditties. You will need to adjust the variables for your situation.
$file is set to the path of the file to be uploaded to YouTube. (The file should already be uploaded to your server.)

/* Change me ! */
$title = stripslashes($_POST['title']);
$description = stripslashes($_POST['description']);
$keywords = 'insert your keywords here';
$file = '../uploads/' . $_POST['file'];
$privacy = 'private';
$category = '10';
$language = 'EN'

/* stop changing */
$post_vars = array();
$post_vars['field_privacy'] = $privacy;
$post_vars['field_myvideo_keywords'] = $keywords;
$post_vars['field_myvideo_title'] = $title;
$post_vars['field_myvideo_descr'] = $description;
$post_vars['field_myvideo_categories'] = $category;
$post_vars['language'] = $language;
$post_vars['field_uploadfile'] = "@$file";
$post_vars['field_command'] = 'myvideo_submit';
$post_vars['submit'] = 'Upload%20Video';

/* See note below about YouTube URL's */
$url = 'http://v##.youtube.com/my_videos_post';

$return = http_post_form($url, $post_vars);

Please note I’m fairly sure YouTube gives a random server number for the url listed above. As far as I know your safe using v1-v64. Please choose a number at random, come up with some nifty random function, or best yet figure out exactly what YouTube is up to and come up with the best solution.

For convenience YouTube’s current settings are copied below.

2
Autos & Vehicles
23
Comedy
24
Entertainment
1
Film & Animation
20
Gadgets & Games
26
Howto & DIY
10
Music
25
News & Politics
22
People & Blogs
15
Pets & Animals
17
Sports
19
Travel & Places
EN
English
ES
Spanish
JP
Japanese
DE
German
CN
Chinese
FR
French
field_privacy
public|private

Moving

I’ve been busy packing the past couple of weeks. We are lucky in that we have the opportunity to pack and move well in advance. So we are taking the time to organize and go through all our crap.

Crap it is. Besides beds and clothes everything now is in boxes or piles. One for yard sale. One for stuff we’ll keep. The stuff we’ll keep we’ve been going through and tossing away.

It’s amazing how much crap we collect. We have a full box of magic markers and a full box of crayons. We found something like 10 pairs of scissors. Tons of little happy meal and bubble gum machine toys.

The whole thing has made me wonder if we as humans aren’t going about it all wrong. Why do we even have closets? We are wasting a few feet of wall to toss up doors that make it harder to get to our stuff. We hide our stuff in little rooms. We put it in storage.

Closets are just silly. If you came over to my house and noticed an attractive shelving system, nicely organized instead of a closet you’d probably be more impressed.

Lots of silly stuff though. Like it’s amazing how much crap we have for decoration. We have so much of it it isn’t very decorative. We have a half dozen little children’s toys that didn’t work as advertised, and a half dozen adult toys that are the same. Apparently we are addicted to stuffed animals. All of us gave up bags of them.

Old clothes are always going to happen. But did we really need six pairs of winter gloves for the kid? Do I need 32 pairs of socks?

Probably 200 pencils with 100 pens, three scientific calculators and a half dozen pencil sharpeners another half dozen headphones.

I’ve collected tons of computer junk too. Two dozen power cables. A few hundred feet of Ethernet and Coaxial cable. Countless boards and chips.

Yard sale, yard sale. We are having a big one. What doesn’t sell that I know will goes to E-bay. The rest to the local freecycle community. Anything left over to the salvation army. As I’m looking around there is more that needs to sell. You see I plan on using the yard sale money to upgrade. Maybe we should sell all our ratty towels.

I need to get new ones if I’m gonna display them out in the open one day.