use of com.google.api.client.http.InputStreamContent 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.http.InputStreamContent in project api-samples by youtube.
the class InvideoProgramming method main.
/**
* This code sample demonstrates different ways that the API can be used to
* promote your channel content. It includes code for the following tasks:
* <ol>
* <li>Feature a video.</li>
* <li>Feature a link to a social media channel.</li>
* <li>Set a watermark for videos on your channel.</li>
* </ol>
*
* @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, "invideoprogramming");
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-invideoprogramming-sample").build();
// Construct a request to retrieve the current user's channel ID.
// In the API response, only include channel information needed for
// this use case. The channel's uploads playlist identifies the
// channel's most recently uploaded video.
// See https://developers.google.com/youtube/v3/docs/channels/list
ChannelListResponse channelListResponse = youtube.channels().list("id,contentDetails").setMine(true).setFields("items(contentDetails/relatedPlaylists/uploads,id)").execute();
// The user's default channel is the first item in the list. If the
// user does not have a channel, this code should throw a
// GoogleJsonResponseException explaining the issue.
Channel myChannel = channelListResponse.getItems().get(0);
String channelId = myChannel.getId();
// The promotion appears 15000ms (15 seconds) before the video ends.
InvideoTiming invideoTiming = new InvideoTiming();
invideoTiming.setOffsetMs(BigInteger.valueOf(15000l));
invideoTiming.setType("offsetFromEnd");
// This is one type of promotion and promotes a video.
PromotedItemId promotedItemId = new PromotedItemId();
promotedItemId.setType("video");
promotedItemId.setVideoId(FEATURED_VIDEO_ID);
// Set a custom message providing additional information about the
// promoted video or your channel.
PromotedItem promotedItem = new PromotedItem();
promotedItem.setCustomMessage("Check out this video about WebM!");
promotedItem.setId(promotedItemId);
// Construct an object representing the invideo promotion data, and
// add it to the channel.
InvideoPromotion invideoPromotion = new InvideoPromotion();
invideoPromotion.setDefaultTiming(invideoTiming);
invideoPromotion.setItems(Lists.newArrayList(promotedItem));
Channel channel = new Channel();
channel.setId(channelId);
channel.setInvideoPromotion(invideoPromotion);
Channel updateChannelResponse = youtube.channels().update("invideoPromotion", channel).execute();
// Print data from the API response.
System.out.println("\n================== Updated Channel Information ==================\n");
System.out.println("\t- Channel ID: " + updateChannelResponse.getId());
InvideoPromotion promotions = updateChannelResponse.getInvideoPromotion();
// We only care about the first item
promotedItem = promotions.getItems().get(0);
System.out.println("\t- Invideo promotion video ID: " + promotedItem.getId().getVideoId());
System.out.println("\t- Promotion message: " + promotedItem.getCustomMessage());
// In-video programming can also be used to feature links to
// associated websites, merchant sites, or social networking sites.
// The code below overrides the promotional video set above by
// featuring a link to the YouTube Developers Twitter feed.
PromotedItemId promotedTwitterFeed = new PromotedItemId();
promotedTwitterFeed.setType("website");
promotedTwitterFeed.setWebsiteUrl("https://twitter.com/youtubedev");
promotedItem = new PromotedItem();
promotedItem.setCustomMessage("Follow us on Twitter!");
promotedItem.setId(promotedTwitterFeed);
invideoPromotion.setItems(Lists.newArrayList(promotedItem));
channel.setInvideoPromotion(invideoPromotion);
// Call the API to set the in-video promotion data.
updateChannelResponse = youtube.channels().update("invideoPromotion", channel).execute();
// Print data from the API response.
System.out.println("\n================== Updated Channel Information ==================\n");
System.out.println("\t- Channel ID: " + updateChannelResponse.getId());
promotions = updateChannelResponse.getInvideoPromotion();
promotedItem = promotions.getItems().get(0);
System.out.println("\t- Invideo promotion URL: " + promotedItem.getId().getWebsiteUrl());
System.out.println("\t- Promotion message: " + promotedItem.getCustomMessage());
// This example sets a custom watermark for the channel. The image
// used is the watermark.jpg file in the "resources/" directory.
InputStreamContent mediaContent = new InputStreamContent("image/jpeg", InvideoProgramming.class.getResourceAsStream("/watermark.jpg"));
// Indicate that the watermark should display during the last 15
// seconds of the video.
InvideoTiming watermarkTiming = new InvideoTiming();
watermarkTiming.setType("offsetFromEnd");
watermarkTiming.setDurationMs(BigInteger.valueOf(15000l));
watermarkTiming.setOffsetMs(BigInteger.valueOf(15000l));
InvideoBranding invideoBranding = new InvideoBranding();
invideoBranding.setTiming(watermarkTiming);
youtube.watermarks().set(channelId, invideoBranding, mediaContent).execute();
} 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.http.InputStreamContent in project google-cloud-java by GoogleCloudPlatform.
the class HttpStorageRpc method create.
@Override
public StorageObject create(StorageObject storageObject, final InputStream content, Map<Option, ?> options) {
try {
Storage.Objects.Insert insert = storage.objects().insert(storageObject.getBucket(), storageObject, new InputStreamContent(storageObject.getContentType(), content));
insert.getMediaHttpUploader().setDirectUploadEnabled(true);
setEncryptionHeaders(insert.getRequestHeaders(), ENCRYPTION_KEY_PREFIX, options);
return insert.setProjection(DEFAULT_PROJECTION).setPredefinedAcl(Option.PREDEFINED_ACL.getString(options)).setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)).setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)).setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)).setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)).execute();
} catch (IOException ex) {
throw translate(ex);
}
}
Aggregations