Last active
July 24, 2024 14:31
-
-
Save thomastraum/e182ef6ffc70b81d5fb1c3407ed6e7da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install PyVimeo | |
import vimeo | |
import os | |
import requests | |
# Replace these with your Vimeo API credentials | |
ACCESS_TOKEN = 'your_vimeo_access_token' | |
# Initialize the Vimeo client | |
v = vimeo.VimeoClient( | |
token=ACCESS_TOKEN | |
) | |
def get_folder_videos(folder_id): | |
""" | |
Get all videos in a Vimeo folder. | |
""" | |
response = v.get(f'/me/projects/{folder_id}/videos') | |
if response.status_code != 200: | |
raise Exception(f"Failed to get videos: {response.json()}") | |
return response.json()['data'] | |
def download_video(video_uri, download_path): | |
""" | |
Download a Vimeo video in the highest quality. | |
""" | |
video_id = video_uri.split('/')[-1] | |
response = v.get(f'/videos/{video_id}') | |
if response.status_code != 200: | |
raise Exception(f"Failed to get video details: {response.json()}") | |
video_data = response.json() | |
# Find the highest quality download link | |
download_links = video_data['download'] | |
highest_quality_link = max(download_links, key=lambda x: x['size']) | |
# Download the video | |
video_url = highest_quality_link['link'] | |
video_title = video_data['name'] | |
video_filename = os.path.join(download_path, f"{video_title}.mp4") | |
print(f"Downloading {video_title}...") | |
response = requests.get(video_url, stream=True) | |
with open(video_filename, 'wb') as f: | |
for chunk in response.iter_content(chunk_size=1024): | |
if chunk: | |
f.write(chunk) | |
print(f"Downloaded {video_title} to {video_filename}") | |
def download_vimeo_folder(folder_url, download_path): | |
""" | |
Download all videos from a Vimeo folder. | |
""" | |
folder_id = folder_url.split('/')[-1].split('?')[0] | |
videos = get_folder_videos(folder_id) | |
if not os.path.exists(download_path): | |
os.makedirs(download_path) | |
for video in videos: | |
download_video(video['uri'], download_path) | |
if __name__ == "__main__": | |
folder_url = 'https://vimeo.com/user/1232181/folder/10476309?isPrivate=false' | |
download_path = './vimeo_downloads' | |
download_vimeo_folder(folder_url, download_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment