use of com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener in project api-samples by youtube.
the class UploadVideo method main.
/**
* Upload the user-selected video to the user's YouTube channel. The code
* looks for the video in the application's project folder and uses OAuth
* 2.0 to authorize the API request.
*
* @param args command line args (not used).
*/
public static void main(String[] args) {
// This OAuth 2.0 access scope allows an application to upload files
// to the authenticated user's YouTube channel, but doesn't allow
// other types of access.
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.upload");
try {
// Authorize the request.
Credential credential = Auth.authorize(scopes, "uploadvideo");
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-uploadvideo-sample").build();
System.out.println("Uploading: " + SAMPLE_VIDEO_FILENAME);
// Add extra information to the video before uploading.
Video videoObjectDefiningMetadata = new Video();
// Set the video to be publicly visible. This is the default
// setting. Other supporting settings are "unlisted" and "private."
VideoStatus status = new VideoStatus();
status.setPrivacyStatus("public");
videoObjectDefiningMetadata.setStatus(status);
// Most of the video's metadata is set on the VideoSnippet object.
VideoSnippet snippet = new VideoSnippet();
// This code uses a Calendar instance to create a unique name and
// description for test purposes so that you can easily upload
// multiple files. You should remove this code from your project
// and use your own standard names instead.
Calendar cal = Calendar.getInstance();
snippet.setTitle("Test Upload via Java on " + cal.getTime());
snippet.setDescription("Video uploaded via YouTube Data API V3 using the Java library " + "on " + cal.getTime());
// Set the keyword tags that you want to associate with the video.
List<String> tags = new ArrayList<String>();
tags.add("test");
tags.add("example");
tags.add("java");
tags.add("YouTube Data API V3");
tags.add("erase me");
snippet.setTags(tags);
// Add the completed snippet object to the video resource.
videoObjectDefiningMetadata.setSnippet(snippet);
InputStreamContent mediaContent = new InputStreamContent(VIDEO_FILE_FORMAT, UploadVideo.class.getResourceAsStream("/sample-video.mp4"));
// Insert the video. The command sends three arguments. The first
// specifies which information the API request is setting and which
// information the API response should return. The second argument
// is the video resource that contains metadata about the new video.
// The third argument is the actual video content.
YouTube.Videos.Insert videoInsert = youtube.videos().insert("snippet,statistics,status", videoObjectDefiningMetadata, mediaContent);
// Set the upload type and add an event listener.
MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();
// Indicate whether direct media upload is enabled. A value of
// "True" indicates that direct media upload is enabled and that
// the entire media content will be uploaded in a single request.
// A value of "False," which is the default, indicates that the
// request will use the resumable media upload protocol, which
// supports the ability to resume an upload operation after a
// network interruption or other transmission failure, saving
// time and bandwidth in the event of network failures.
uploader.setDirectUploadEnabled(false);
MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
public void progressChanged(MediaHttpUploader uploader) throws IOException {
switch(uploader.getUploadState()) {
case INITIATION_STARTED:
System.out.println("Initiation Started");
break;
case INITIATION_COMPLETE:
System.out.println("Initiation Completed");
break;
case MEDIA_IN_PROGRESS:
System.out.println("Upload in progress");
System.out.println("Upload percentage: " + uploader.getProgress());
break;
case MEDIA_COMPLETE:
System.out.println("Upload Completed!");
break;
case NOT_STARTED:
System.out.println("Upload Not Started!");
break;
}
}
};
uploader.setProgressListener(progressListener);
// Call the API and upload the video.
Video returnedVideo = videoInsert.execute();
// Print data about the newly inserted video from the API response.
System.out.println("\n================== Returned Video ==================\n");
System.out.println(" - Id: " + returnedVideo.getId());
System.out.println(" - Title: " + returnedVideo.getSnippet().getTitle());
System.out.println(" - Tags: " + returnedVideo.getSnippet().getTags());
System.out.println(" - Privacy Status: " + returnedVideo.getStatus().getPrivacyStatus());
System.out.println(" - Video Count: " + returnedVideo.getStatistics().getViewCount());
} catch (GoogleJsonResponseException e) {
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
e.printStackTrace();
} catch (Throwable t) {
System.err.println("Throwable: " + t.getMessage());
t.printStackTrace();
}
}
use of com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener in project HearthStats.net-Uploader by HearthStats.
the class UploadVideo method main.
/**
* Upload the user-selected video to the user's YouTube channel. The code
* looks for the video in the application's project folder and uses OAuth
* 2.0 to authorize the API request.
*
* @param args command line args (not used).
*/
public static void main(String[] args) {
// This OAuth 2.0 access scope allows an application to upload files
// to the authenticated user's YouTube channel, but doesn't allow
// other types of access.
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.upload");
try {
// Authorize the request.
Credential credential = Auth.authorize(scopes, "uploadvideo");
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-uploadvideo-sample").build();
System.out.println("Uploading: " + SAMPLE_VIDEO_FILENAME);
// Add extra information to the video before uploading.
Video videoObjectDefiningMetadata = new Video();
// Set the video to be publicly visible. This is the default
// setting. Other supporting settings are "unlisted" and "private."
VideoStatus status = new VideoStatus();
status.setPrivacyStatus("public");
videoObjectDefiningMetadata.setStatus(status);
// Most of the video's metadata is set on the VideoSnippet object.
VideoSnippet snippet = new VideoSnippet();
// This code uses a Calendar instance to create a unique name and
// description for test purposes so that you can easily upload
// multiple files. You should remove this code from your project
// and use your own standard names instead.
Calendar cal = Calendar.getInstance();
snippet.setTitle("Test Upload via Java on " + cal.getTime());
snippet.setDescription("Video uploaded via YouTube Data API V3 using the Java library " + "on " + cal.getTime());
// Set the keyword tags that you want to associate with the video.
List<String> tags = new ArrayList<String>();
tags.add("test");
tags.add("example");
tags.add("java");
tags.add("YouTube Data API V3");
tags.add("erase me");
snippet.setTags(tags);
// Add the completed snippet object to the video resource.
videoObjectDefiningMetadata.setSnippet(snippet);
InputStreamContent mediaContent = new InputStreamContent(VIDEO_FILE_FORMAT, UploadVideo.class.getResourceAsStream("/sample-video.mp4"));
// Insert the video. The command sends three arguments. The first
// specifies which information the API request is setting and which
// information the API response should return. The second argument
// is the video resource that contains metadata about the new video.
// The third argument is the actual video content.
YouTube.Videos.Insert videoInsert = youtube.videos().insert("snippet,statistics,status", videoObjectDefiningMetadata, mediaContent);
// Set the upload type and add an event listener.
MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();
// Indicate whether direct media upload is enabled. A value of
// "True" indicates that direct media upload is enabled and that
// the entire media content will be uploaded in a single request.
// A value of "False," which is the default, indicates that the
// request will use the resumable media upload protocol, which
// supports the ability to resume an upload operation after a
// network interruption or other transmission failure, saving
// time and bandwidth in the event of network failures.
uploader.setDirectUploadEnabled(false);
MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
public void progressChanged(MediaHttpUploader uploader) throws IOException {
switch(uploader.getUploadState()) {
case INITIATION_STARTED:
System.out.println("Initiation Started");
break;
case INITIATION_COMPLETE:
System.out.println("Initiation Completed");
break;
case MEDIA_IN_PROGRESS:
System.out.println("Upload in progress");
System.out.println("Upload percentage: " + uploader.getProgress());
break;
case MEDIA_COMPLETE:
System.out.println("Upload Completed!");
break;
case NOT_STARTED:
System.out.println("Upload Not Started!");
break;
}
}
};
uploader.setProgressListener(progressListener);
// Call the API and upload the video.
Video returnedVideo = videoInsert.execute();
// Print data about the newly inserted video from the API response.
System.out.println("\n================== Returned Video ==================\n");
System.out.println(" - Id: " + returnedVideo.getId());
System.out.println(" - Title: " + returnedVideo.getSnippet().getTitle());
System.out.println(" - Tags: " + returnedVideo.getSnippet().getTags());
System.out.println(" - Privacy Status: " + returnedVideo.getStatus().getPrivacyStatus());
System.out.println(" - Video Count: " + returnedVideo.getStatistics().getViewCount());
} catch (GoogleJsonResponseException e) {
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
e.printStackTrace();
} catch (Throwable t) {
System.err.println("Throwable: " + t.getMessage());
t.printStackTrace();
}
}
use of com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener in project api-samples by youtube.
the class UploadThumbnail method main.
/**
* Prompt the user to specify a video ID and the path for a thumbnail
* image. Then call the API to set the image as the thumbnail for the video.
*
* @param args command line args (not used).
*/
public static void main(String[] args) {
// This OAuth 2.0 access scope allows for full read/write access to the
// authenticated user's account.
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
try {
// Authorize the request.
Credential credential = Auth.authorize(scopes, "uploadthumbnail");
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-uploadthumbnail-sample").build();
// Prompt the user to enter the video ID of the video being updated.
String videoId = getVideoIdFromUser();
System.out.println("You chose " + videoId + " to upload a thumbnail.");
// Prompt the user to specify the location of the thumbnail image.
File imageFile = getImageFromUser();
System.out.println("You chose " + imageFile + " to upload.");
// Create an object that contains the thumbnail image file's
// contents.
InputStreamContent mediaContent = new InputStreamContent(IMAGE_FILE_FORMAT, new BufferedInputStream(new FileInputStream(imageFile)));
mediaContent.setLength(imageFile.length());
// Create an API request that specifies that the mediaContent
// object is the thumbnail of the specified video.
Set thumbnailSet = youtube.thumbnails().set(videoId, mediaContent);
// Set the upload type and add an event listener.
MediaHttpUploader uploader = thumbnailSet.getMediaHttpUploader();
// Indicate whether direct media upload is enabled. A value of
// "True" indicates that direct media upload is enabled and that
// the entire media content will be uploaded in a single request.
// A value of "False," which is the default, indicates that the
// request will use the resumable media upload protocol, which
// supports the ability to resume an upload operation after a
// network interruption or other transmission failure, saving
// time and bandwidth in the event of network failures.
uploader.setDirectUploadEnabled(false);
// Set the upload state for the thumbnail image.
MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
@Override
public void progressChanged(MediaHttpUploader uploader) throws IOException {
switch(uploader.getUploadState()) {
// sent.
case INITIATION_STARTED:
System.out.println("Initiation Started");
break;
// completes.
case INITIATION_COMPLETE:
System.out.println("Initiation Completed");
break;
// uploaded.
case MEDIA_IN_PROGRESS:
System.out.println("Upload in progress");
System.out.println("Upload percentage: " + uploader.getProgress());
break;
// been successfully uploaded.
case MEDIA_COMPLETE:
System.out.println("Upload Completed!");
break;
// not started yet.
case NOT_STARTED:
System.out.println("Upload Not Started!");
break;
}
}
};
uploader.setProgressListener(progressListener);
// Upload the image and set it as the specified video's thumbnail.
ThumbnailSetResponse setResponse = thumbnailSet.execute();
// Print the URL for the updated video's thumbnail image.
System.out.println("\n================== Uploaded Thumbnail ==================\n");
System.out.println(" - Url: " + setResponse.getItems().get(0).getDefault().getUrl());
} catch (GoogleJsonResponseException e) {
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
e.printStackTrace();
}
}
use of com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener in project api-samples by youtube.
the class Captions method uploadCaption.
/**
* Uploads a caption track in draft status that matches the API request parameters.
* (captions.insert)
*
* @param videoId the YouTube video ID of the video for which the API should
* return caption tracks.
* @param captionLanguage language of the caption track.
* @param captionName name of the caption track.
* @param captionFile caption track binary file.
* @throws IOException
*/
private static void uploadCaption(String videoId, String captionLanguage, String captionName, File captionFile) throws IOException {
// Add extra information to the caption before uploading.
Caption captionObjectDefiningMetadata = new Caption();
// Most of the caption's metadata is set on the CaptionSnippet object.
CaptionSnippet snippet = new CaptionSnippet();
// Set the video, language, name and draft status of the caption.
snippet.setVideoId(videoId);
snippet.setLanguage(captionLanguage);
snippet.setName(captionName);
snippet.setIsDraft(true);
// Add the completed snippet object to the caption resource.
captionObjectDefiningMetadata.setSnippet(snippet);
// Create an object that contains the caption file's contents.
InputStreamContent mediaContent = new InputStreamContent(CAPTION_FILE_FORMAT, new BufferedInputStream(new FileInputStream(captionFile)));
mediaContent.setLength(captionFile.length());
// Create an API request that specifies that the mediaContent
// object is the caption of the specified video.
Insert captionInsert = youtube.captions().insert("snippet", captionObjectDefiningMetadata, mediaContent);
// Set the upload type and add an event listener.
MediaHttpUploader uploader = captionInsert.getMediaHttpUploader();
// Indicate whether direct media upload is enabled. A value of
// "True" indicates that direct media upload is enabled and that
// the entire media content will be uploaded in a single request.
// A value of "False," which is the default, indicates that the
// request will use the resumable media upload protocol, which
// supports the ability to resume an upload operation after a
// network interruption or other transmission failure, saving
// time and bandwidth in the event of network failures.
uploader.setDirectUploadEnabled(false);
// Set the upload state for the caption track file.
MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
@Override
public void progressChanged(MediaHttpUploader uploader) throws IOException {
switch(uploader.getUploadState()) {
// sent.
case INITIATION_STARTED:
System.out.println("Initiation Started");
break;
// completes.
case INITIATION_COMPLETE:
System.out.println("Initiation Completed");
break;
// uploaded.
case MEDIA_IN_PROGRESS:
System.out.println("Upload in progress");
System.out.println("Upload percentage: " + uploader.getProgress());
break;
// been successfully uploaded.
case MEDIA_COMPLETE:
System.out.println("Upload Completed!");
break;
// not started yet.
case NOT_STARTED:
System.out.println("Upload Not Started!");
break;
}
}
};
uploader.setProgressListener(progressListener);
// Upload the caption track.
Caption uploadedCaption = captionInsert.execute();
// Print the metadata of the uploaded caption track.
System.out.println("\n================== Uploaded Caption Track ==================\n");
snippet = uploadedCaption.getSnippet();
System.out.println(" - ID: " + uploadedCaption.getId());
System.out.println(" - Name: " + snippet.getName());
System.out.println(" - Language: " + snippet.getLanguage());
System.out.println(" - Status: " + snippet.getStatus());
System.out.println("\n-------------------------------------------------------------\n");
}
use of com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener in project api-samples by youtube.
the class Captions method updateCaption.
/**
* Updates a caption track's draft status to publish it.
* Updates the track with a new binary file as well if it is present. (captions.update)
*
* @param captionId The id parameter specifies the caption ID for the resource
* that is being updated. In a caption resource, the id property specifies the
* caption track's ID.
* @param captionFile caption track binary file.
* @throws IOException
*/
private static void updateCaption(String captionId, File captionFile) throws IOException {
// Modify caption's isDraft property to unpublish a caption track.
CaptionSnippet updateCaptionSnippet = new CaptionSnippet();
updateCaptionSnippet.setIsDraft(false);
Caption updateCaption = new Caption();
updateCaption.setId(captionId);
updateCaption.setSnippet(updateCaptionSnippet);
Caption captionUpdateResponse;
if (captionFile == null) {
// Call the YouTube Data API's captions.update method to update an existing caption track.
captionUpdateResponse = youtube.captions().update("snippet", updateCaption).execute();
} else {
// Create an object that contains the caption file's contents.
InputStreamContent mediaContent = new InputStreamContent(CAPTION_FILE_FORMAT, new BufferedInputStream(new FileInputStream(captionFile)));
mediaContent.setLength(captionFile.length());
// Create an API request that specifies that the mediaContent
// object is the caption of the specified video.
Update captionUpdate = youtube.captions().update("snippet", updateCaption, mediaContent);
// Set the upload type and add an event listener.
MediaHttpUploader uploader = captionUpdate.getMediaHttpUploader();
// Indicate whether direct media upload is enabled. A value of
// "True" indicates that direct media upload is enabled and that
// the entire media content will be uploaded in a single request.
// A value of "False," which is the default, indicates that the
// request will use the resumable media upload protocol, which
// supports the ability to resume an upload operation after a
// network interruption or other transmission failure, saving
// time and bandwidth in the event of network failures.
uploader.setDirectUploadEnabled(false);
// Set the upload state for the caption track file.
MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
@Override
public void progressChanged(MediaHttpUploader uploader) throws IOException {
switch(uploader.getUploadState()) {
// sent.
case INITIATION_STARTED:
System.out.println("Initiation Started");
break;
// completes.
case INITIATION_COMPLETE:
System.out.println("Initiation Completed");
break;
// uploaded.
case MEDIA_IN_PROGRESS:
System.out.println("Upload in progress");
System.out.println("Upload percentage: " + uploader.getProgress());
break;
// been successfully uploaded.
case MEDIA_COMPLETE:
System.out.println("Upload Completed!");
break;
// not started yet.
case NOT_STARTED:
System.out.println("Upload Not Started!");
break;
}
}
};
uploader.setProgressListener(progressListener);
// Upload the caption track.
captionUpdateResponse = captionUpdate.execute();
System.out.println("\n================== Uploaded New Caption Track ==================\n");
}
// Print information from the API response.
System.out.println("\n================== Updated Caption Track ==================\n");
CaptionSnippet snippet = captionUpdateResponse.getSnippet();
System.out.println(" - ID: " + captionUpdateResponse.getId());
System.out.println(" - Name: " + snippet.getName());
System.out.println(" - Language: " + snippet.getLanguage());
System.out.println(" - Draft Status: " + snippet.getIsDraft());
System.out.println("\n-------------------------------------------------------------\n");
}
Aggregations