I wanted to create a video site but lacked the server resources to run it, I am using a shared server, it has ffmpeg but when converting a video it takes lot’s of system resources. I successfully made the fmpeg video site, But didn’t decide to use it. I searched for youtube APIs and i came to know we could simply use the youtube api to upload videos from our site to youtube. So i decided to use it for my video site. Below is step by step tutorial on using youtube api to upload videos from your site to youtube, I used the clientLogin method of youtube api to upload videos.
- First get the developer key for using youtube api.
Go to http://code.google.com/apis/youtube/dashboard/ and regsiter a developer key
You will be given client id and developer key. Note it down, it will be used in your own application later. - Get the php client libraries, Go to http://framework.zend.com/download/gdata
- extract it to your server. I assume you put the Zend direcctory in your server document root.
- Create a form in your site as below which i assume will be pointed to upload.php file in your srver.
<form action="upload.php" method="post" enctype="multipart/form-data"> <select name="videoCategory"> <option value="Autos">Autos &amp; Vehicles</option> <option value="Music">Music</option> <option value="Animals">Pets &amp; Animals</option> <option value="Sports">Sports</option> <option value="Travel">Travel &amp; Events</option> <option value="Games">Gadgets &amp; Games</option> <option value="Comedy">Comedy</option> <option value="People">People &amp; Blogs</option> <option value="News">News &amp; Politics</option> <option value="Entertainment">Entertainment</option> <option value="Education">Education</option> <option value="Howto">Howto &amp; Style</option> <option value="Nonprofit">Nonprofit &amp; Activism</option> <option value="Tech">Science &amp; Technology</option> </select> <label for="title">Title</label> <input id="title" name="title" type="text"> <label for="description">Description</label> <textarea id="description" name="description" rows="5"></textarea> <label for="keywords">Kewords (use comma , to separate)</label> <input id="keywords" name="keywords" value="" type="text"> <br class="clr" /> <input value="Next -> choose video" name="add" type="submit" /> </form>
Create upload.php
<?php
if(isset($_POST['add'],$_POST['videoCategory'],$_POST['title'],$_POST['description']))
{
//add the video meta data to session
$_SESSION['vidmeta']['category'] = $_POST['videoCategory'];
$_SESSION['vidmeta']['title'] = $_POST['title'];
$_SESSION['vidmeta']['description'] = $_POST['description'];
$_SESSION['vidmeta']['keywords'] = $_POST['keywords'];
//application id change this to our applicationId
$applicationId = 'onnepalvideosite';
//client id change this to our own client id
$clientId = "ytapi-xxxxxx-xxxx-xxxxx";
//developer key change this to your own key
$developerKey = "xxxxxxx-xxxxxxx-xxxxxxxxx-xxxxxxx";
//include zend loader
require_once('Zend/Loader.php'); // the Zend dir must be in your include_path
//include youtube class
Zend_Loader::loadClass('Zend_Gdata_YouTube');
//include the client login class
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
//Client login
$authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin';
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
$username = 'your username here',
$password = 'our password here',
$service = 'youtube',
$client = null,
$source = 'onnepal', // a short string identifying your application
$loginToken = null,
$loginCaptcha = null,
$authenticationURL);
$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);
// create a new VideoEntry object
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
$myVideoEntry->setVideoTitle($_POST['title']);
$myVideoEntry->setVideoDescription($_POST['description']);
// The category must be a valid YouTube category!
$myVideoEntry->setVideoCategory($_POST['videoCategory']);
// Set keywords. Please note that this must be a comma-separated string
// and that individual keywords cannot contain whitespace
$myVideoEntry->SetVideoTags($_POST['keywords']);
$tokenHandlerUrl = 'http://gdata.youtube.com/action/GetUploadToken';
$tokenArray = $yt->getFormUploadToken($myVideoEntry, $tokenHandlerUrl);
$tokenValue = $tokenArray['token'];
$postUrl = $tokenArray['url'];
$nextUrl = 'http://www.yoursite.com/yt.php';
print <<< END
<br /><form action="${postUrl}?nexturl=${nextUrl}"
method="post" enctype="multipart/form-data">
<input name="file" type="file"/>
<input name="token" type="hidden" value="${tokenValue}"/>
<input value="Upload Video File" type="submit" />
</form>
END;
}
?>
This code takes the category, title, description and keywords from the form and generates a form to upload video to youtube. You can then upload video to youtube using the form.
Next make yt.php file which is the $nexturl file, $nexturl is the location to which youtube will redirect after the upload is complete. YouTube appends id and status parameters to the URL as shown in the following example
http://www.yoursite.com/yt.php?status=200&id=JPF-DXF7hzc
Afterou get the youtube video id in your yt.php file you can add it to your own database to show videos in your site. You can do anything you wish with the variables you get through yt.php.I hope it will be help ful to all wishing to use youtube apis to upload video.
This can also be implemented to phpizabi sites.