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

Website Design Skeleton

You don’t want to re-invent the wheel every new website you begin coding on. Instead maximize your productivity with a template or skeleton if you will of a basic site design.

Your implementation may vary. I’ve tried to keep mine terse for the utmost configuration.

We’ll start with a basic header:

head.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>Change Me Later</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <link href="/style.css" rel="stylesheet" type="text/css">
</head>
<body>

I’ve choose HTML 4.01 strict for various reasons. You will need to remember to fill in your title.

As you may have noticed, I’ve included a style sheet in this header, so let’s go over that.

style.css

* {margin:0;padding:0;}

Not much to see here, we reset our margin and padding to 0 to eliminate cross browser quirks and general madness while designing. Some people prefer more in their default CSS.

Let’s continue on to the main page.

index.php

require('head.php');
<!-- Do stuff here -->
require('foot.php');

Not much to see nor explain. Now onwards to our footer.

foot.php

</body>
</html>

And there you have your skeleton. It’s up to you to add the muscle.

A few ideas:

  • Include your name, version and other information in comments in the header and stylesheet.
  • Include your statistics code in the footer.
  • Include any javascript you might use.
  • Instead write a php library to generate your header/footer bassed on doctype/chartype.
  • Set default text styles, colors etc to your liking in your CSS