【问题标题】:Upload video to youtube in java用java将视频上传到youtube
【发布时间】:2012-11-01 17:08:48
【问题描述】:

我的本​​地磁盘中有一个视频,我想通过我的应用程序将其上传到 youtube。你能给我遵循同样的指导方针吗?请给我执行该操作的步骤或代码或链接。 提前致谢。

在提出解决方案后,我尝试了: gdata.youtube.com/feeds/api/users/users/uploads(用户作为用户名)但我收到 IOException.i.e 与服务通信时出现问题。

  java.net.SocketException: Connection reset
 java.net.SocketInputStream.read(SocketInputStream.java:189)
        at java.net.SocketInputStream.read(SocketInputStream.java:121)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
        at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
        at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:641)
        at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:589)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1319)
        at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
        at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:490)
        at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:470)
        at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:534)
        at com.google.gdata.client.media.MediaService.insert(MediaService.java:353)
        at ytupload.YouTubeWriteClient.uploadVideo(YouTubeWriteClient.java:508)
        at ytupload.YouTubeWriteClient.main(YouTubeWriteClient.java:828)

请帮忙...

【问题讨论】:

    标签: java jsp servlets youtube-api


    【解决方案1】:

    您是否查看了谷歌开发者指南上的Youtube API,查看了Video Upload 部分?你会发现这样的:

    VideoEntry newEntry = new VideoEntry();
    
    YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
    mg.setTitle(new MediaTitle());
    mg.getTitle().setPlainTextContent("My Test Movie");
    mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Autos"));
    mg.setKeywords(new MediaKeywords());
    mg.getKeywords().addKeyword("cars");
    mg.getKeywords().addKeyword("funny");
    mg.setDescription(new MediaDescription());
    mg.getDescription().setPlainTextContent("My description");
    mg.setPrivate(false);
    mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "mydevtag"));
    mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "anotherdevtag"));
    
    newEntry.setGeoCoordinates(new GeoRssWhere(37.0,-122.0));
    // alternatively, one could specify just a descriptive string
    // newEntry.setLocation("Mountain View, CA");
    
    MediaFileSource ms = new MediaFileSource(new File("file.mov"), "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);
    

    您可能会在上传视频之前成功验证:

    YouTubeService service = new YouTubeService(clientID, developer_key);
    

    希望对你有帮助

    【讨论】:

    【解决方案2】:

    你必须创建一个新的VideoEntry 对象

    以下代码上传视频:

    VideoEntry newEntry = new VideoEntry();
    
    YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
    mg.setTitle(new MediaTitle());
    mg.getTitle().setPlainTextContent("Title goes here");
    mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Autos"));
    mg.setKeywords(new MediaKeywords());
    mg.getKeywords().addKeyword("keyword-here");
    mg.setDescription(new MediaDescription());
    mg.getDescription().setPlainTextContent("My description");
    mg.setPrivate(false);
    mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "mydevtag"));
    mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "anotherdevtag"));
    
    newEntry.setGeoCoordinates(new GeoRssWhere(37.0,-122.0));
    // alternatively, one could specify just a descriptive string
    // newEntry.setLocation("Mountain View, CA");
    
    MediaFileSource ms = new MediaFileSource(new File("file.mov"), "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);
    

    https://developers.google.com/youtube/2.0/developers_guide_java#Uploading_Videos

    【讨论】:

      猜你喜欢
      • 2016-01-27
      • 1970-01-01
      • 2012-03-07
      • 2012-04-07
      • 2013-09-19
      • 2015-01-10
      • 2013-12-12
      • 2014-04-29
      • 1970-01-01
      相关资源
      最近更新 更多