【发布时间】:2016-09-21 21:03:42
【问题描述】:
在应用程序中,有一段代码类似于official youtube sample,它使用谷歌的库(com.google.api)上传视频文件。上传顺利,在处理结束时com.google.api.services.youtube.model.Video 的id 打印在日志文件中。所以我确信上传本身可以完美运行。
上传小视频文件(小于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