- What: The YouTube APIs
- Where: YouTube APIs and Tools
- Who: Pratyus Patnaik & Matt Martin
The YouTube API allows developers to leverage the infrastructure developed by YouTube for storing, playing, and otherwise manipulating video playback over the web. Applications do not have to bother about efficient search mechanisms, video storage or video formats. Using YouTube’s infrastructure, application developers can easily fetch or upload videos and create dynamic playlists. The API is available in the Java, .NET, PHP and Python languages which allows developers to get started quickly in the language of their choice. In general, the API vastly simplifies the task of enriching applications with video content. The suite has four components:
- Widgets: Simple embeddable page elements to give sites YouTube functionality.
- Custom Player: Allows easy configuring of the custom player to show playlists, videos, etc.
- Player APIs: To control over YouTube video playback on the site.
- Data API: To perform video searches, upload videos, create playlists, and more.
We begin by discussing the simplest components of the YouTube API. The video bar and video search widgets are extremely simple to use, but offer limited customizability. Next we discuss the player API, which offers a simple but powerful way to control video playback within a browser. Then we discuss the data management features offered by the Data API. We go deeper to understand operations which are more relevant from the perspective of a third party application, namely retrieval and manipulation of videos. Finally, we conclude our report by discussing about application using YouTube API and critiquing the API.
Within these simple widgets, there are only a handful of basic options which can be controlled by the developer. In the video bar API, the developer can specify the orientation of the bar, the size of the video thumbnails, and the location of the associated player. The developer can choose between displaying 4 videos and displaying 8 videos, but there are no other options. Although the thumbnails are displayed in a table, there is no way to change the layout from 1 column to 2 columns, for example. Both of these options would seem relatively straightforward to implement within the API, but neither exists.
Additionally, the video bar API offers limited interaction with more advanced YouTube APIs. For example, a slightly more advanced developer may want to combine the video bar with the player API in such a way that videos clicked in the video bar are loaded into a customized YouTube player. Currently, there does not appear to be any simple way to integrate these two APIs. The lack of integration between these two components may be a result of their different development paths. In particular, the video bar and video search widgets seem to have grown out of Google’s AJAX search APIs rather than YouTube-specific APIs. Regardless, developers seeking a more customized functionality cannot rely on reusing either widget within a more complicated application.
Player API Edit
Integration with the Data API leverages the ability to dynamically load videos within the Player API. Thus, queries could be passed to the Data API and the results could be used to generate a dynamic playlist, for example. To generate a dynamic playlist, the developer could pass the first video ID to the loadVideoByID() method and wait for the state change indicating the video has been loaded before calling the playVideo() method. Finally, the developer could repeat the process once the state changes in the player to indicate that playback of the first video has stopped.
Data API Edit
The YouTube Data API allows users to retrieve and manipulate YouTube videos on their site. It allows applications to perform most of the functions executed on the YouTube website. The API enables applications to search for YouTube videos and retrieve standard video feeds, comments and video responses and ratings. In addition, the API lets applications upload videos to YouTube and delete or update existing videos. Applications can also retrieve and manipulate playlists, subscriptions and user profiles.
Usage of Data API Edit
Here we will dive deeper into the Data API understanding its usage. To get started one needs to sign up for a developer account. That will give the developer id required to use the API. All the videos uploaded to YouTube using the application can be categorized with the developer id, giving application developers a view of the portion of YouTube uploaded by users using their application. ClientID and associated developer key can be generated from http://code.google.com/apis/base/signup.html. All of the YouTube API calls get handled by YouTubeService object, instantiated with clientID and developer_key (for both authenticated and un-authenticated way), as shown below:
YouTubeService service = new YouTubeService(clientID, developer_key);
With this call, the user is enabling applications to access the Google server providing services to the API (http://gdata.youtube.com) on his behalf.
All public feeds (search, read-only) do not require any authentication. On the other hand, operations like retrieval of private feeds as well as write, upload, update and delete operations need user authentication. There are two ways to authenticate requests:
- AuthSub proxy authentication: It authenticates a user through Google login page. After the user logs in, the application is allowed to use the API to access a user’s account. Google will then redirect the user back to a URL specified in the authentication request. Sessions tokens are maintained for multiple authentications. For Web applications AuthSub authentication is used.
- ClientLogin username/password authentication: It is used in installed applications that store the user's email address (username) and password (passed to YouTube using HTTPS). The following code demonstrates how to use ClientLogin to authenticate a user.
YouTubeService service = new YouTubeService(clientID, developer_key); service.setUserCredentials("GmailID@gmail.com", "password");
Video Retrieval Edit
Data API consists of many video feeds which contains a number of VideoEntry objects. Each video entry corresponds to exactly one URL for a feed of information about a particular video. The information includes information like author, video Id, description, keywords, location, rating, view count, duration, etc. The URL has the following format:
The following code retrieves a VideoEntry object, which corresponds to a particular YouTube video, and then prints the metadata for the video:
String videoEntryUrl = "http://gdata.youtube.com/feeds/api/videos/videoID"; VideoEntry videoEntry = service.getEntry(new URL(videoEntryUrl), VideoEntry.class); printVideoEntry(videoEntry, TRUE);
Also, the API provides feeds selected based on a variety of criteria like region, category. The URL for a region and category feed has the following format:
- localeID are region ids like US for USA and JP for Japan.
- feedID are most_viewed, top_rated, recently_featured, etc.
- CATEGORY_NAME are Comedy, News etc.
The API allows for searching videos based on particular search term. Search can be performed on author name, keywords, formats and full text search on title, metadata, tags. To execute a search request, a YouTubeQuery object is created that specifies search criteria. The following code demonstrates how to perform a search query for videos:
YouTubeQuery query = new YouTubeQuery(new URL("http://gdata.youtube.com/feeds/api/videos")); query.setFullTextQuery("Keyword"); VideoFeed videoFeed = service.query(query, VideoFeed.class);
Search results can be ordered by RELEVANCE, VIEW_COUNT, UPDATED,or RATING. The information returned is enough to display a video on the site using the Player API. The following code demonstrates how to play a particular video:
<object width="425" height="350"> <param name="movie" value="http://www.youtube.com/v/videoID&autoplay=1″> </param> <embed src=”http://www.youtube.com/v/videoID&autoplay=1″ type=”application/x-shockwave-flash” width=”425″ height=”350″></embed> </object>
Video Manipulation Edit
Videos can be uploaded to YouTube using the API from an authenticated user. For all the uploaded videos fields Title, Description, Category, Keywords need to be provided. Videos can be uploaded in two ways using the API:
- Direct upload: Allows user to add videos that are in application’s video library to YouTube. This method is useful when an application stores videos uploaded through the site and also adds those videos to YouTube. Here user video is first sent to application’s server, which will subsequently send an API request to upload the video to YouTube.
- Browser-based upload: Allows applications to accept video uploads from its users without having to handle, host or proxy the actual video files. This is useful when the application does not store the uploaded video files.
Youtube Data API supports updating and deleting of videos by calling the appropriate methods in VideoEntry. It supports methods to fetch and insert video rating and comments. API also gives a programmatic way to manage user subscription, playlists and profile using the objects SubscriptionEntry, PlaylistEntry and UserProfileEntry respectively.
Specific Examples Edit
One could use this suite of APIs to create a website for dynamically uploading and sharing data within a customized layout. The API also allows developers to build interesting mashup applications using YouTube videos. The YouTube API is currently the 10th most popular API on ProgrammableWeb with 46 mashups. Some examples are:
- YellingZebra, which combines Last.fm and YouTube to create video playlists with videos of one artist or several related artists.
- Youtute is a simple application using YouTube API of videos targeting special interest (tutorials).
- RateMyDance is a Hot-or-Not application for videos using YouTube.
- Kosmix is a search engine that mashups information from various sources including YouTube.
- The YouTube related widgets and APIs offer ways of incorporating YouTube into websites for developers of all skill levels. It would be nice to have more preconfigured components (maybe a selection of different video bar designs) and an easier way of incorporating very simple components into slightly more complicated designs. For example, a developer may have enough skills to use the Player API, but may have difficulty integrating it with the video bar widget without more information or support. While the developer only loses very basic functionality, he still must redo from scratch something which has already been done.
- The YouTube Data API tries to ensure high performance even under heavy API server loads by retrieving search results from a specially optimized search index. However, there are still delays (30 minutes to two hours) between the time that a video is uploaded or updated and the time the new information is included in video feeds.
- The ability to authenticate users in the YouTube API should result in viral use of the API. Applications immediately gain access to a large user base and can serve users better by retrieving user-specific information. However, this advantage might also become a weakness when someone without a YouTube login tries to use the application. The user may choose not to use an application rather than sign up for a YouTube account.
- Ability for application developers to search videos uploaded to YouTube using their application (by the means of developer_ID) also is an useful ways for developers to understand their user base.