Google Drive URLs can come in different formats:
- Regular sharing URL:
https://drive.google.com/file/d/FILE_ID/view
- Direct download URL:
https://drive.google.com/uc?id=FILE_ID&export=download
For our purposes, we’ll need to extract the FILE_ID from the URL to construct the direct download link.
Setting Up the Python Environment#
First, make sure you have the required libraries installed:
1
|
pip install requests tqdm
|
The requests
library will handle the HTTP requests, and tqdm
will show a progress bar for larger downloads.
The Download Script#
Here’s a complete Python script to download videos from public Google Drive links:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
import requests
import os
from tqdm import tqdm
from urllib.parse import urlparse, parse_qs
def extract_file_id(url):
"""Extract Google Drive file ID from various URL formats"""
# Handle direct download links
if 'uc?id=' in url:
return parse_qs(urlparse(url).query)['id'][0]
# Handle standard sharing links
parsed = urlparse(url)
if 'drive.google.com' in parsed.netloc:
path_parts = parsed.path.split('/')
if 'd' in path_parts:
file_id_index = path_parts.index('d') + 1
if file_id_index < len(path_parts):
return path_parts[file_id_index]
raise ValueError("Could not extract file ID from the provided URL")
def download_google_drive_video(url, output_path=None):
"""Download a video from a public Google Drive URL"""
try:
file_id = extract_file_id(url)
except ValueError as e:
print(f"Error: {e}")
return False
# Construct the direct download URL
download_url = f"https://drive.google.com/uc?id={file_id}&export=download"
# Start a session to handle cookies and confirmations
session = requests.Session()
# Initial request to get confirmation token for large files
response = session.get(download_url, stream=True)
# Check if we need to handle the virus scan warning for large files
if 'confirm=' in response.url:
# Extract confirmation token
confirm_token = parse_qs(urlparse(response.url).query).get('confirm')
if confirm_token:
download_url = f"https://drive.google.com/uc?id={file_id}&export=download&confirm={confirm_token[0]}"
response = session.get(download_url, stream=True)
# Check if the request was successful
if response.status_code != 200:
print(f"Failed to download file: HTTP {response.status_code}")
return False
# Get file size for progress bar
total_size = int(response.headers.get('content-length', 0))
# Determine output filename
if output_path is None:
# Try to get filename from content-disposition header
content_disposition = response.headers.get('content-disposition')
if content_disposition:
filename = content_disposition.split('filename=')[1].strip('"')
else:
filename = f"downloaded_video_{file_id}.mp4"
output_path = filename
# Download with progress bar
block_size = 1024 # 1 Kibibyte
progress_bar = tqdm(total=total_size, unit='iB', unit_scale=True)
with open(output_path, 'wb') as file:
for data in response.iter_content(block_size):
progress_bar.update(len(data))
file.write(data)
progress_bar.close()
# Verify download integrity if total size was known
if total_size != 0 and progress_bar.n != total_size:
print("ERROR: Downloaded file size doesn't match expected size")
return False
print(f"Successfully downloaded: {output_path}")
return True
# Example usage
if __name__ == "__main__":
# Replace with your Google Drive URL
drive_url = "https://drive.google.com/file/d/YOUR_FILE_ID/view"
download_google_drive_video(drive_url)
|
How to Use the Script#
- Get the shareable link: Right-click the file in Google Drive, select “Get link”, and ensure it’s set to “Anyone with the link can view”
- Run the script: Replace
YOUR_FILE_ID
in the example with your actual file ID or URL
- Specify output path: Optionally, provide a custom output filename as the second parameter
Handling Large Files#
For larger files, Google Drive shows a warning page. Our script automatically handles this by:
- Detecting the confirmation token in the redirect URL
- Adding the
confirm
parameter to the download URL
- Resuming the download with the proper authorization
Error Handling#
The script includes basic error handling for:
- Invalid URLs that don’t contain a recognizable file ID
- HTTP errors during the download process
- Size mismatches that may indicate incomplete downloads
Improvements#
- Rate limiting: Add delays between downloads if processing multiple files
- Error logging: Implement proper logging for production use