1

我正在实施一项使用他们的 API (V2) 将视频上传到 YouTube 的服务,但是一旦上传了视频,我需要找到响应,以便我可以获得目标 URL。有人知道如何实现这一目标吗?

这是调用上传函数的调用:

YouTubeService service = new YouTubeService("My_Service", developerKey);

 try {
      service.setUserCredentials(user, password);
 } catch (AuthenticationException e) {
      System.out.println("Invalid login credentials.");
      return;
 }

 youtubeURL = YouTubeUploadClient.uploadVideo(service);

这是我所说的上传功能代码:

public static String uploadVideo(YouTubeService service)
        throws IOException, ServiceException, InterruptedException, JSONException {

    String youtubeURL = null;
    File videoFile = new File("c://video.mp4");

    if (!videoFile.exists()) {
        System.out.println("Sorry, that video doesn't exist.");
        return null;
    }

    MediaFileSource ms = new MediaFileSource(videoFile, "video/mp4");
    String videoTitle = "Title";

    VideoEntry newEntry = new VideoEntry();
    YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
    mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Games"));
    mg.setTitle(new MediaTitle());
    mg.getTitle().setPlainTextContent("Title");
    mg.setKeywords(new MediaKeywords());
    mg.getKeywords().addKeyword("Test");
    mg.setDescription(new MediaDescription());
    mg.getDescription().setPlainTextContent("Description");

    FileUploadProgressListener listener = new FileUploadProgressListener();
    ResumableGDataFileUploader uploader = new ResumableGDataFileUploader.Builder(
            service, new URL("http://uploads.gdata.youtube.com/resumable/feeds/api/users/default/uploads"), ms, newEntry)
            .title(videoTitle)
            .trackProgress(listener, 1000)
            .chunkSize(10000000)
            .build();

    uploader.start();
    while (!uploader.isDone()) { Thread.sleep(25); }

    switch(uploader.getUploadState()) {
        case COMPLETE: System.out.println("Uploaded successfully"); return null;
        case CLIENT_ERROR: System.out.println("Upload Failed"); return null;
        default: System.out.println("Unexpected upload status"); return null;
    }
}

private static class FileUploadProgressListener implements ProgressListener {
    public synchronized void progressChanged(ResumableHttpFileUploader uploader)
    {
        switch(uploader.getUploadState()) {
            case COMPLETE: System.out.println("Upload Completed"); break;
            case CLIENT_ERROR: System.out.println("Upload Failed"); break;
            case IN_PROGRESS: System.out.println(String.format("Upload in progress: %3.0f", uploader.getProgress() * 100) + "%"); break;
            case NOT_STARTED: System.out.println("Upload Not Started"); break;
        }
    }
}
4

1 回答 1

0

在 YouTube API 论坛(https://groups.google.com/forum/embed/?place=forum/youtube-api-gdata&showsearch=true&showpopout=true&parenturl=https%3A%2F%2Fdevelopers.google.com% 2Fyoutube%2Fforum%2Fdiscussion#!searchin/youtube-api-gdata/resumable$20response/youtube-api-gdata/ox_c40kvZWQ/ADy-DWo16HkJ):

        ResumableGDataFileUploader uploader = new ResumableGDataFileUploader.Builder(
                service, new URL("http://uploads.gdata.youtube.com/resumable/feeds/api/users/default/uploads"), ms, newEntry)
                .title(videoTitle)
                .trackProgress(listener, 1000)
                .chunkSize(10000000)
                .build();

        Future<ResumableHttpFileUploader.ResponseMessage> futureResponse = uploader.start();

        while (!uploader.isDone()) { Thread.sleep(1000); }

        switch(uploader.getUploadState()) {
            case COMPLETE:
                ResumableHttpFileUploader.ResponseMessage responseMessage = futureResponse.get();
                VideoEntry videoEntry = new VideoEntry();
                videoEntry.parseAtom(new ExtensionProfile(), responseMessage.getInputStream());
                youtubeURL = videoEntry.getHtmlLink().getHref();
                logger.info("Uploaded successfully");
                return youtubeURL;
            case CLIENT_ERROR: logger.error("Upload Failed"); return null;
            default: logger.warn("Unexpected upload status"); return null;
        }
于 2013-11-18T20:07:24.590 回答