开发者

How to find the url of the uploaded video in YouTube (Through Youtube API)

I am using the following code to upload a video to Youtube through youtube API. My problem is after uploading the video, I need to give the location of the video to the user. How do I find this ? I'll be really grateful if someone can help me to solve this.

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();
    }


I managed to solve this by myself. Instead of using resumable file upload, I used direct upload. My code:

    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;

    }

I hope this will save someone else's day.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜