quibblify
quibblify

Reputation: 395

Retrieving details from a YouTube video given the URL

For example, given the link https://www.youtube.com/watch?v=Hl-zzrqQoSE which is a video about installing the Java JDK.

Using Java and the YouTube-API, how can I get information such as length, title, and views?

I looked and I found https://developers.google.com/youtube/2.0/developers_guide_protocol_video_entries but it's outdated and I'm not sure exactly how to use it.

Forgive my ignorance as I'm new to Java. Thanks.

Upvotes: 1

Views: 3303

Answers (1)

theduck
theduck

Reputation: 2617

You can use Videos:List and specify the video ID to retrieve video information for a specific video. For example:

YouTube youtube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(),
        new HttpRequestInitializer() {
            public void initialize(HttpRequest request) throws IOException {
            }
        }).setApplicationName("video-test").build();

final String videoId = "Hl-zzrqQoSE";
YouTube.Videos.List videoRequest = youtube.videos().list("snippet,statistics,contentDetails");
videoRequest.setId(videoId);
videoRequest.setKey("{YOUR-API-KEY}");
VideoListResponse listResponse = videoRequest.execute();
List<Video> videoList = listResponse.getItems();

Video targetVideo = videoList.iterator().next();

targetVideo would then hold information related to your video. You can get information such as title, view count and duration from this object:

    targetVideo.getSnippet().getTitle();
    targetVideo.getStatistics().getViewCount();
    targetVideo.getContentDetails().getDuration();

The following imports are required:

import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.Video;
import com.google.api.services.youtube.model.VideoListResponse;

import java.io.IOException;
import java.util.List;

Upvotes: 5

Related Questions