Downloading Video Files from Google Drive Using Java

Welcome back! As we continue building a robust video transcribing system, it's important to handle not just local files but also videos stored remotely. In this lesson, we'll focus on how to efficiently download video files from Google Drive using Java and the popular HTTP client library OkHttp. We'll explore how to recognize Google Drive URLs, extract the necessary file identifiers, and use OkHttp to download files from Google Drive to your local system. Mastering these skills will allow you to process videos from a variety of sources as you develop your transcribing application.

What You'll Learn

In this lesson, you'll learn how to:

  • Recognize and handle Google Drive URLs in Java.
  • Extract the file ID from a Google Drive link using Java string manipulation and URL parsing.
  • Use the OkHttp library to download videos from a publicly accessible Google Drive link.
  • Understand the limitations and considerations when downloading files from Google Drive.
Understanding the Google Drive Download Process

The main goal is to download video files from Google Drive using Java and OkHttp. Google Drive URLs contain unique identifiers for each file. The process involves confirming that a URL is from Google Drive, extracting the file ID, and then constructing a direct download URL to fetch the file using OkHttp.

Google Drive URLs typically follow one of these patterns:

  1. Direct file path: https://drive.google.com/file/d/{fileid}/view
  2. Open ID parameter: https://drive.google.com/open?id={fileid}

By recognizing these patterns, we can extract the file ID, which is essential for constructing the download URL.

Recognizing Google Drive URLs

To start, we need to verify that a given URL is a Google Drive link. In Java, we can use the java.net.URI class to parse the URL and check its host:

import java.net.URI;

public class GoogleDriveService {
    public static boolean isGoogleDriveUrl(String urlString) {
        try {
            URI uri = new URI(urlString);
            return uri.getHost().contains("drive.google.com");
        } catch (Exception e) {
            return false;
        }
    }
}

Here, we parse the URL and check if the host contains "drive.google.com". This helps us filter out unsupported URLs.

Extracting the File ID

Next, we need to extract the file ID from the Google Drive URL. We can use Java's string manipulation methods to handle both common URL patterns:

import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class GoogleDriveService {
    public static String getFileId(String url) {
        try {
            URI uri = new URI(url);
            String path = uri.getPath();
            String query = uri.getQuery();

            // Pattern 1: /file/d/{fileid}/
            int fileIdIndex = path.indexOf("/file/d/");
            if (fileIdIndex != -1) {
                int start = fileIdIndex + "/file/d/".length();
                int end = path.indexOf('/', start);
                if (end == -1) {
                    end = path.length();
                }
                return path.substring(start, end);
            }

            // Pattern 2: id={fileid}
            if (query != null) {
                Map<String, String> queryParams = parseQueryParams(query);
                if (queryParams.containsKey("id")) {
                    return queryParams.get("id");
                }
            }
        } catch (Exception e) {
            // Handle exception
        }
        return null;
    }

    private static Map<String, String> parseQueryParams(String query) {
        Map<String, String> params = new HashMap<>();
        String[] pairs = query.split("&");
        for (String pair : pairs) {
            int idx = pair.indexOf("=");
            if (idx > 0) {
                String key = URLDecoder.decode(pair.substring(0, idx), StandardCharsets.UTF_8);
                String value = URLDecoder.decode(pair.substring(idx + 1), StandardCharsets.UTF_8);
                params.put(key, value);
            }
        }
        return params;
    }
}

Explanation:

  • For URLs containing /file/d/, the method locates the start of the file ID by finding the index of /file/d/ and adding its length. It then looks for the next / after the file ID to determine where the ID ends. If there is no trailing /, it assumes the file ID goes to the end of the URL.
  • For URLs containing id=, the method finds the index of id=, adds its length to get the start of the file ID, and then looks for the next & character (in case there are additional URL parameters). If there is no &, it assumes the file ID goes to the end of the URL.
  • If neither pattern is found, the method returns null, indicating that the URL does not match a recognized Google Drive file pattern.

This approach ensures that the method can handle the most common Google Drive URL formats and reliably extract the file ID needed for downloading.

Downloading the File with OkHttp

With the file ID in hand, we can use the OkHttp library to download the file. For publicly shared files, Google Drive allows direct download via a special URL format:

https://drive.google.com/uc?export=download&id={fileid}

Here's how you can implement the download logic using OkHttp:

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.file.Files;

public class GoogleDriveService {
    private final OkHttpClient httpClient = new OkHttpClient();

    public String downloadFile(String url) throws Exception {
        if (!isGoogleDriveUrl(url)) {
            throw new IllegalArgumentException("URL is not a valid Google Drive link");
        }
        String fileId = getFileId(url);
        if (fileId == null) {
            throw new IllegalArgumentException("Invalid Google Drive URL");
        }

        // Construct the direct download URL
        String downloadUrl = "https://drive.google.com/uc?export=download&id=" + fileId;

        Request request = new Request.Builder()
                .url(downloadUrl)
                .addHeader("User-Agent", "Mozilla/5.0 (compatible; VideoTranscriber/1.0)")
                .build();

        File tempFile = File.createTempFile("gdrive_", ".mp4");
        try (Response response = httpClient.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new Exception("Failed to download: HTTP " + response.code());
            }
            try (InputStream inputStream = response.body().byteStream();
                 FileOutputStream outputStream = new FileOutputStream(tempFile)) {
                byte[] buffer = new byte[8192];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
            }
        }

        if (Files.size(tempFile.toPath()) == 0) {
            throw new Exception("Downloaded file is empty");
        }

        return tempFile.getAbsolutePath();
    }
}

Explanation:

  • We extract the file ID using the previously defined method.
  • We construct the download URL using the file ID.
  • We use OkHttp to send a GET request to the download URL.
  • The response body is streamed directly to a temporary file.
  • We check that the downloaded file is not empty and return the path for further processing.
Limitations and Considerations
  • This method only works for files that are shared with "Anyone with the link" (publicly accessible).
  • For large files or files requiring confirmation (such as those that trigger a Google Drive virus scan warning), you may need to handle additional redirects or cookies. The above code works for most standard cases.
  • For private files, this approach will not work; the file must be shared publicly.
Why It Matters

Being able to download videos from Google Drive using OkHttp is a valuable skill for many applications, such as organizing educational content, performing media analysis, or preparing videos for further processing. However, always ensure you have the legal rights to download and use the content, especially if it is protected or licensed.

With this foundation, you're now ready to apply these concepts in practice and integrate Google Drive downloads into your video transcribing workflow using Java and OkHttp.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal