如何在YouTube中找到上传视频的网址(通过Youtube API)

时间:2011-06-16 12:14:07

标签: youtube-api

我使用以下代码通过youtube API将视频上传到Youtube。我的问题是在上传视频后,我需要将视频的位置提供给用户。我怎么找到这个? 如果有人能帮助我解决这个问题,我将非常感激。

MediaFileSource ms = new MediaFileSource(videoFile, mimeType);
        String videoTitle = title;
        VideoEntry newEntry = new VideoEntry();
        YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
        mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Tech"));
        mg.setTitle(new MediaTitle());
        mg.getTitle().setPlainTextContent(videoTitle);
        mg.setKeywords(new MediaKeywords());
        mg.getKeywords().addKeyword("yt:crop=16:9");
        mg.setDescription(new MediaDescription());
        mg.getDescription().setHtmlContent(attributionDocument);
        mg.setPrivate(true);
        mg.setVideoId("Vid1");


    ResumableGDataFileUploader uploader = null;
    try {
        uploader = new ResumableGDataFileUploader.Builder(
            service, new URL(RESUMABLE_UPLOAD_URL), ms, newEntry)
            .title(videoTitle)
            .build();

        uploader.start();
          while (!uploader.isDone()) {
            try {
                Thread.sleep(PROGRESS_UPDATE_INTERVAL);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          }

          switch(uploader.getUploadState()) {
            case COMPLETE:
              System.out.println("Uploaded successfully");

              break;
            case CLIENT_ERROR:
                System.out.println("Upload Failed");
              break;
            default:
                System.out.println("Unexpected upload status");
              break;
          }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ServiceException e) {
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:2)

我自己设法解决了这个问题。我使用直接上传,而不是使用可恢复文件上传。我的代码:

    String id = "";
    File videoFile = new File(videoLocation);
    if (!videoFile.exists()) {
        System.out.println("Sorry, that video doesn't exist.");

    }
    String videoTitle = title;
    VideoEntry newEntry = new VideoEntry();
    YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
    mg.setTitle(new MediaTitle());
    mg.getTitle().setPlainTextContent(videoTitle);
    mg.setKeywords(new MediaKeywords());
    mg.getKeywords().addKeyword("yt:crop=16:9");
    mg.setDescription(new MediaDescription());
    mg.getDescription().setHtmlContent(attributionDocument);
    mg.setPrivate(true);
    mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Tech"));


    MediaFileSource ms = new MediaFileSource(videoFile, "video/quicktime");
    newEntry.setMediaSource(ms);

    String uploadUrl =
      "http://uploads.gdata.youtube.com/feeds/api/users/default/uploads";

    VideoEntry createdEntry = service.insert(new URL(uploadUrl), newEntry);
    id =createdEntry.getId();
    return id;

    }

我希望这会挽救别人的一天。