【问题标题】:Youtube Upload API returns videoId but video is not available on youtubeYoutube Upload API 返回 videoId 但视频在 youtube 上不可用
【发布时间】:2016-09-21 21:03:42
【问题描述】:

在应用程序中,有一段代码类似于official youtube sample,它使用谷歌的库(com.google.api)上传视频文件。上传顺利,在处理结束时com.google.api.services.youtube.model.Videoid 打印在日志文件中。所以我确信上传本身可以完美运行。

上传小视频文件(小于15mb)可以。可以在 youtube 上看到生成的视频,但对于大型 100+ mb 视频文件,情况并非如此。上传了大文件,我可以在日志文件中看到 youtube id,但 youtube 本身没有视频。

这是代码摘录

private static final String VIDEO_FILE_FORMAT = "video/*";
private static YouTube youtube;
private static final List<String> SCOPES = Lists.newArrayList(
        YouTubeScopes.YOUTUBE,
        YouTubeScopes.YOUTUBE_UPLOAD
);

private static int uploadToYoutube(BlablaConfiguration configuration, com.blabla.youtube.model.Video videoObject, String localFilename) {
    LOG.debug("UPLOAD_YOUTUBE");
    try {
        JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
        HttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(HTTP_TRANSPORT)
                .setJsonFactory(JSON_FACTORY)
                .setServiceAccountId(configuration.getClientEmail())
                .setServiceAccountPrivateKeyFromP12File(new File(configuration.getP12filename()))
                .setServiceAccountScopes(SCOPES)
                .setServiceAccountUser("my@account.com")
                .build();



        youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName(configuration.getAppName())
                .build();

        LOG.info("Uploading...");

        Video videoObjectDefiningMetadata = new Video();
        VideoStatus status = new VideoStatus();
        status.setPrivacyStatus("public");
        videoObjectDefiningMetadata.setStatus(status);

        VideoSnippet snippet = new VideoSnippet();

        StringBuffer title = new StringBuffer();
        try {
            String yearStr = Integer.toString(videoObject.getYear());
            title.append(yearStr);
        } catch (Exception e) {
            //
        }

        String make = videoObject.getMake();
        if (make != null) {
            title.append(" " + make);
        }

        String model = videoObject.getModel();
        if (model != null) {
            title.append(" " + model);
        }

        String style = videoObject.getStyle();
        if (style != null) {
            title.append(" " + style);
        }

        snippet.setTitle(title.toString());
        snippet.setDescription("Video uploaded via Blabla.com");

        List<String> tags = new ArrayList<String>();
        tags.add("http://www.blabla.com");
        snippet.setTags(tags);

        videoObjectDefiningMetadata.setSnippet(snippet);

        InputStreamContent mediaContent = new InputStreamContent(VIDEO_FILE_FORMAT,
                new FileInputStream(localFilename)
        );

        YouTube.Videos.Insert videoInsert = youtube.videos()
                .insert("snippet,statistics,status", videoObjectDefiningMetadata, mediaContent);

        MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();

        uploader.setDirectUploadEnabled(false);
        MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
            public void progressChanged(MediaHttpUploader uploader) throws IOException {
                switch (uploader.getUploadState()) {
                    case INITIATION_STARTED:
                        LOG.debug("Initiation Started");
                        break;
                    case INITIATION_COMPLETE:
                        LOG.debug("Initiation Completed");
                        break;
                    case MEDIA_IN_PROGRESS:
                        LOG.debug("Upload in progress");
                        LOG.debug("Upload percentage: " + uploader.getProgress());
                        break;
                    case MEDIA_COMPLETE:
                        LOG.debug("Upload Completed!");
                        break;
                    case NOT_STARTED:
                        LOG.warn("Upload Not Started!");
                        break;
                }
            }
        };
        uploader.setProgressListener(progressListener);
        Video returnedVideo = videoInsert.execute();
        String videoID = returnedVideo.getId();
        LOG.debug("\n================== Returned Video ==================\n");
        LOG.debug("  - Id: " + videoID);
        videoObject.setYoutubeId(videoID);
        return 0;
    } catch (GoogleJsonResponseException e) {
        LOG.error("GoogleJSONException");
        e.printStackTrace();
        return 1;
    } catch (IOException e) {
        LOG.error("IOException: " + e.getMessage());
        e.printStackTrace();
        return 2;
    } catch (Throwable t) {
        LOG.error("Throwable: " + t.getMessage());
        t.printStackTrace();
        return 3;
    }
}

这可能是什么问题? 对此有什么想法吗?

【问题讨论】:

  • 你能把你试过的代码贴出来
  • @Alok 我已经更新了包含代码的问题

标签: java api youtube-api


【解决方案1】:

一旦视频上传,Youtube 会进行大量的后期处理,而这个时间与视频时间成正比——视频越长,处理时间就越长。

这可以解释为什么当一个 videoId 被分配给您的视频时,它还没有显示出来。

给它一些时间再试一次。

https://www.quora.com/Why-does-YouTube-take-so-long-to-process-videos-after-theyre-uploaded

【讨论】:

  • 感谢您的回答。视频是昨天上传的,假设它应该进行后期处理,我认为 10 分钟的视频应该不会花费太多时间。
  • 如果这个问题仍然存在,我能想到的唯一其他解释是该视频可能被标记为待审核——谷歌使用大量视频处理算法来查看内容是否侵犯版权,令人反感内容等。最好是与 Youtube 支持联系,可能吗?
猜你喜欢
  • 1970-01-01
  • 2022-12-19
  • 2017-09-01
  • 1970-01-01
  • 2016-06-05
  • 1970-01-01
  • 2013-10-19
  • 2020-01-12
  • 2013-09-11
相关资源
最近更新 更多