Search in sources :

Example 1 with Insert

use of com.google.api.services.youtube.YouTube.Captions.Insert 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");
}
Also used : MediaHttpUploader(com.google.api.client.googleapis.media.MediaHttpUploader) BufferedInputStream(java.io.BufferedInputStream) InputStreamContent(com.google.api.client.http.InputStreamContent) CaptionSnippet(com.google.api.services.youtube.model.CaptionSnippet) MediaHttpUploaderProgressListener(com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener) Insert(com.google.api.services.youtube.YouTube.Captions.Insert) Caption(com.google.api.services.youtube.model.Caption) FileInputStream(java.io.FileInputStream)

Aggregations

MediaHttpUploader (com.google.api.client.googleapis.media.MediaHttpUploader)1 MediaHttpUploaderProgressListener (com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener)1 InputStreamContent (com.google.api.client.http.InputStreamContent)1 Insert (com.google.api.services.youtube.YouTube.Captions.Insert)1 Caption (com.google.api.services.youtube.model.Caption)1 CaptionSnippet (com.google.api.services.youtube.model.CaptionSnippet)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1