Search in sources :

Example 1 with Download

use of com.google.api.services.youtube.YouTube.Captions.Download in project api-samples by youtube.

the class Captions method downloadCaption.

/**
     * Downloads a caption track for a YouTube video. (captions.download)
     *
     * @param captionId The id parameter specifies the caption ID for the resource
     * that is being downloaded. In a caption resource, the id property specifies the
     * caption track's ID.
     * @throws IOException
     */
private static void downloadCaption(String captionId) throws IOException {
    // Create an API request to the YouTube Data API's captions.download
    // method to download an existing caption track.
    Download captionDownload = youtube.captions().download(captionId).setTfmt(SRT);
    // Set the download type and add an event listener.
    MediaHttpDownloader downloader = captionDownload.getMediaHttpDownloader();
    // Indicate whether direct media download is enabled. A value of
    // "True" indicates that direct media download is enabled and that
    // the entire media content will be downloaded in a single request.
    // A value of "False," which is the default, indicates that the
    // request will use the resumable media download protocol, which
    // supports the ability to resume a download operation after a
    // network interruption or other transmission failure, saving
    // time and bandwidth in the event of network failures.
    downloader.setDirectDownloadEnabled(false);
    // Set the download state for the caption track file.
    MediaHttpDownloaderProgressListener downloadProgressListener = new MediaHttpDownloaderProgressListener() {

        @Override
        public void progressChanged(MediaHttpDownloader downloader) throws IOException {
            switch(downloader.getDownloadState()) {
                case MEDIA_IN_PROGRESS:
                    System.out.println("Download in progress");
                    System.out.println("Download percentage: " + downloader.getProgress());
                    break;
                //  been successfully downloaded.
                case MEDIA_COMPLETE:
                    System.out.println("Download Completed!");
                    break;
                //  not started yet.
                case NOT_STARTED:
                    System.out.println("Download Not Started!");
                    break;
            }
        }
    };
    downloader.setProgressListener(downloadProgressListener);
    OutputStream outputFile = new FileOutputStream("captionFile.srt");
    // Download the caption track.
    captionDownload.executeAndDownloadTo(outputFile);
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) MediaHttpDownloader(com.google.api.client.googleapis.media.MediaHttpDownloader) MediaHttpDownloaderProgressListener(com.google.api.client.googleapis.media.MediaHttpDownloaderProgressListener) Download(com.google.api.services.youtube.YouTube.Captions.Download)

Aggregations

MediaHttpDownloader (com.google.api.client.googleapis.media.MediaHttpDownloader)1 MediaHttpDownloaderProgressListener (com.google.api.client.googleapis.media.MediaHttpDownloaderProgressListener)1 Download (com.google.api.services.youtube.YouTube.Captions.Download)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1