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

20 Replies to “YouTube Uploader”

  1. Great work, way to go! ;)

    Congrats on your scripting!

  2. It can be changed easily to ‘public’. Your right though, generally there is no reason. Private just removes the shared code. The file can still be shared by us in the know in fact. When I first started out this endevour I knew nothing about YouTube and thought it would enable more protection.

    Thanks for adding another thing to my ever-growing to-do list. :)

  3. Hi, it’s possible to add a upload progress bar?

  4. Possible… sure..

    However PHP has some problems with file upload. I was actually planning to do a tutorial on it but ended up running in circles. Perl does it fairly well, but the source code that’s available is fairly rough.

    Remember with this script you’ve already uploaded the file to your server, so I’ve found that file transfer time is quite low even with large video files. I guess those who are unfortunate to have a decent webhost might run into problems, but it should be quite speedy.

  5. I’m new to this, I’ve been Googling around to find a Youtube like uploading script for my site and I ran into this. What is this exactly? Please give me the simplified version of what this does? I know a bit of PHP.

    Thanks
    Tony Cai
    http://tonycai.com

  6. Okay I understand it a little more now.

    So the PHP code above is the backend of what needs to be used with a template to get videos uploaded onto youtube without having to be on youtube. This is GENIUS!

    Does someone have an example of a template they can provide me?

    Thanks a lot!
    Tony Cai

  7. Hi great post I really enjoyed it. However I’m having occasionaly ( works about 50% ) issues when I try to upload. I’m get an error back that says the file is to large, please make it smaller. Unfortunately I wish it was to big that would be an easy solve. It uploads fine uploading the same video file with the browser. Do you have any ideas why this might be happening? The video is a standard 320×240, 30fps. I’m totally baffled on this one and it very sporadic.

  8. Wow, woke up this morning with comments and IM’s all about my little script here. I’ll try my best to help.

    @Tony

    The script is lacking a simple front end. I ended up creating a simple form page to fill in the needed information. Then, because my files were already uploaded to the server, it uses the information and the file to upload. Depending on how you’ll use the script you’ll want to modify the information you put in or the info that is hard coded. When I have time over this weekend I’ll see if I can’t revamp this article to include a simple form front end which people can work off of.

    @Smaxor

    Perhaps the occasional problem is the server you are uploading to? For my script I just picked a number and went with it, but I do vaguely recall having problems with the first number I randomly picked. Sporadic problems are so darn hard to debug. Feel free to send/post your code sample and I’ll be happy to take a look.

  9. Llynix,

    Awesome man! I can’t wait! I will try to contact you via IM again.

    Thanks in advance!
    Tony Cai

  10. nice script, works fine. been adding some parser to the returned webpage to get the youtube link however (i think its what most people want)

  11. does it work under PHP5 ?
    someone told me curl might have problems with cookiejar under PHP5…

    and i’m getting – ‘failed creating formpost data’ error.
    thanks

    b.

  12. Does anyone have just a downloadable solution??
    aka: very simple simple front end / a back end..

  13. Hey this is awesome, especially appreciate Brien’s update. Thanks so much.

  14. How can i know the video ID after uploading?

  15. Hi,

    I have tried this script but it does not upload any file on my youtube account. So I realy don’t know what the problem is but it doesn’t work

  16. it is an outdated script :) need to be modified

  17. Yes,
    It’s not working now.
    Youtube has changes names of almost all hidden html fields.
    I’m working to create a new working script.

    I’ll post it once it’s successful.

    IndiaGuy

Comments are closed.