【问题标题】:How to upload a video to YouTube using YouTube API and MediaStreamSource如何使用 YouTube API 和 MediaStreamSource 将视频上传到 YouTube
【发布时间】:2013-12-04 23:25:15
【问题描述】:

我正在构建一个中继服务,它将视频从外部服务器传递到 YouTube。目前我的代码正在按预期工作,但我需要避免提前在本地保存文件以使用 MediaFileSource 上传到 YouTube。有没有办法传递 InputStream 而不是文件并使用 MediaStreamSource 来允许管道?

https://developers.google.com/gdata/javadoc/com/google/gdata/data/media/MediaStreamSource

这样我就可以像这样直接传输文件

 YouTubeService youTubeService = new YouTubeService("My-Service", developerKey);
 youTubeService.setUserCredentials(user, password);

 VideoEntry newEntry = new VideoEntry();

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

 MediaStreamSource ms = new MediaFileSource(new URL("http://www.somewebsite.com/video.mp4").openStream(), "video/mp4");
 newEntry.setMediaStream(ms);

 String uploadUrl = "http://uploads.gdata.youtube.com/feeds/api/users/default/uploads";
 VideoEntry createdEntry = youTubeService.insert(new URL(uploadUrl), newEntry);
 return createdEntry.getHtmlLink().getHref();

【问题讨论】:

    标签: java youtube-api


    【解决方案1】:

    我遇到了一个错误,不得不将我的代码编辑为:

    String title = "Test Video";
    youTubeService.getRequestFactory().setHeader("Slug", title);
    

    我想我应该提一下,因为我在你的 sn-p 中没有看到这一点。但是上传需要非常长的时间。上传有什么进展吗?

    【讨论】:

    • 不支持 MediaStreamSource
    • 我最终没有使用 YouTube API
    猜你喜欢
    • 2011-05-13
    • 2012-12-23
    • 2011-06-13
    • 2014-04-10
    • 2017-02-27
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多