Youtube上传API返回videoId,但YouTube上没有视频

时间:2016-05-24 14:55:21

标签: java api youtube-api

在应用中,有一段类似于official youtube sample的代码,使用谷歌的lib(com.google.api)上传视频文件。上传顺利,在处理结束时,id com.google.api.services.youtube.model.Video打印在日志文件中。因此,我确信上传本身效果非常好。

上传小视频文件(少于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;
    }
}

这可能是个问题? 有什么想法?

1 个答案:

答案 0 :(得分:0)

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

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

给它一些时间再试一次。

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