Forum Controls
Spotlight Features
Replies: 4 - Pages: 1  
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

Interact with YouTube from Java

URL: YouTube Java Developer Guide

At 8:36 AM on Nov 30, 2007, Geertjan wrote:

Yet another stallion in Google's stable is its YouTube Data API . In a nutshell, you can interact with anything from YouTube within your very own Java application! And even better news is that the API is under active development. A blog entry from 2 days ago , in the YouTube Developer API Blog , tells us about several new enhancements to the API.

As an introduction, below is some Java code that will print the titles of all videos on YouTube that have "lizards" in them, together with the authors of these videos.

Before you begin coding, just put this into your browser, and then you'll see the feed that you'll be accessing from code:

http://gdata.youtube.com/feeds/videos?start-index=1&max-results=25&vq=lizards&oi=spell

Right, and now let's get that from Java and, from that feed, get some interesting information:

public static void main(String[] args) {
    try {
        YouTubeService myService = new YouTubeService("mycompany-myapp-1");
        String myFeed = "http://gdata.youtube.com/feeds/videos?start-index=1&max-results=25&vq=lizards&oi=spell";
        printVideoFeed(myService, myFeed);
    } catch (IOException ex) {
        Logger.getLogger(YouTubeTest.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ServiceException ex) {
        Logger.getLogger(YouTubeTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}

private static void printVideoFeed(YouTubeService service, String feedUrl) throws IOException, ServiceException {
    VideoFeed videoFeed = service.getFeed(new URL(feedUrl), VideoFeed.class);
    List<VideoEntry> allVideos = videoFeed.getEntries();
    Iterator<VideoEntry> itAllVideos = allVideos.iterator();
    while (itAllVideos.hasNext()){
        VideoEntry oneVideo  = itAllVideos.next();
        TextConstruct oneVideoTitle = oneVideo.getTitle();
        String oneVideoTitleText = oneVideoInfo.getPlainText();
        //Print titles of all videos:
        System.out.print(oneVideoTitleText);
        List<Person> allAuthors = oneVideo.getAuthors();
        Iterator<Person> itAllAuthors = allAuthors.iterator();
        while (itAllAuthors.hasNext()){
            Person oneAuthor = itAllAuthors.next();
            //Print authors of current title:
            System.out.print(" (by " + oneAuthor.getName() +")\n");
        }
    }
}

And here's the result (don't know how "Budweiser Compilation" got in there):

The JARs needed for this sample are:

  • gdata-youtube-1.0.jar
  • mail.jar
  • gdata-core-1.0.jar
  • gdata-media-1.0.jar
  • gdata-client-1.0.jar

I don't know why mail.jar is needed, but errors are thrown if it isn't included. I had to download JavaMail especially for this purpose, which seems odd. Something else that's unclear is the argument to the YouTubeService class's constructor. The documentation says that the "argument to the constructor takes the form of a short string identifying your application, for logging purposes. This string should take the form 'companyName-applicationName-versionID'". Hmmm, "for logging purposes." Interesting. A bit like those sites where they say: "For logging purposes, please fill in your bank account number and secret password." In other words, seems like a slightly dodgy "phone home" thingummy that I'm not comfortable with. On the upside, in your code you get to use the "VideoEntry.isRacy" boolean, which is kinda fun. :-)

One cool use case of this API is... integration with your IDE. For example, here is a toolbar in NetBeans IDE, with a list showing all the videos on YouTube that have "NetBeans" in the title:

Conceivably, the user would be able to click on an item in the list and then an external browser would open, displaying the video. Or, even better, the video could be played within the IDE. That should be possible to do too, somehow, I imagine, redirecting the YouTube video into a Swing container.

Thanks again Google for your cool APIs.

1 . At 11:52 AM on Nov 30, 2007, Mike P wrote:
  Click to reply to this thread Reply

Re: Interact with YouTube from Java

Maybe someone could work with Google to create a better media framework?

- video algorithm plugin strategy (this is where people insert their video library code to support the various video algorithms, eg. where Adobe inserts their flash player. If Adobe doesn't want to play, perhaps Google or someone else can create a wrapper - the Adobe libraries are already installed by the browser)
- playing directly onto a window region
- and/or feeding decompressed frames to a DataBuffer
- edit facilities that properly handles the damage between key frames after cutting and inserting

All done up in a nice clean logical object oriented frame work. Not with flat C style spaghetti with bits of logic impulsively spread out in unrelated places. (some people just fail to create OO structures and have no clue how to think logically and think ahead - I'm amazed at this inability, often).
2 . At 5:49 AM on Dec 1, 2007, Kimmig wrote:
  Click to reply to this thread Reply

Re: Interact with YouTube from Java

That would have been my rant, too. I would have loved to find an actual play method in that API ;)


But I understand that Sun's engineering ressources are limited. Maybe some day things like JMF will get the attention it deserves.
3 . At 3:08 PM on Dec 1, 2007, Geertjan wrote:
  Click to reply to this thread Reply

Re: Interact with YouTube from Java

I agree with the above comments. By the way, I've started working on a YouTube Swing Browser and will probably be able to get quite far with it:
http://blogs.sun.com/geertjan/entry/youtube_swing_browser
4 . At 4:22 AM on Dec 3, 2007, Raveman wrote:
  Click to reply to this thread Reply

Re: Interact with YouTube from Java

cool idea, looks very cool, i might start using it

P.S. Logger.getLogger(YouTubeTest.class.getName()).log(Level.SEVERE, null, ex); looks really weird, when i see it i know why im still using log4j, why you didnt create static field for logger? that remind me of wierd code writing styles in some tutorials and then people are in shock why some developers really code this way.

thread.rss_message