Download Videos from Pixabay using Python

šŸŽ„ Download Videos from Pixabay using Python

Want to download high-quality videos automatically from Pixabay using Python? This project helps you fetch and save videos based on any keyword, using the official Pixabay Video API. Perfect for creating content libraries, B-roll collections, or training datasets easily!

🧩 Step 1: Install Required Modules

This script uses two Python libraries:

  • requests — to make API calls and download videos.
  • tqdm — to show a progress bar while downloading.

Install them using pip:

pip install requests tqdm

šŸš€ Step 2: Get Your Pixabay API Key

You’ll need a free API key to use the Pixabay Video API. Go to Pixabay API Docs and log in or sign up to get your key. Once you have it, replace 'YOUR_PIXABAY_API_KEY' in the code below with your actual key.

šŸ’» Step 3: Write the Python Script

Create a new file named pixabay_video_downloader.py and paste this code:


# Code By : Atul Kushwaha || Chat GPT
import os
import requests
from tqdm import tqdm

# Pixabay API Key (replace with your valid key)
API_KEY = 'YOUR_PIXABAY_API_KEY'

# Configuration
SEARCH_QUERY = "Nature"     # Topic
TOTAL_VIDEOS = 3            # Number of videos to fetch
VIDEO_QUALITY = 'medium'    # 'large', 'medium', 'small'

# Folder to save videos
SAVE_FOLDER = "videos"
os.makedirs(SAVE_FOLDER, exist_ok=True)

# Pixabay Video API URL (orientation not supported for videos)
url = (
    f'https://pixabay.com/api/videos/?key={API_KEY}'
    f'&q={SEARCH_QUERY}'
    f'&per_page={TOTAL_VIDEOS}'
    f'&safesearch=true'
)

# Fetch video metadata
response = requests.get(url)
if response.status_code == 200:
    data = response.json()

    if not data.get('hits'):
        print(f"⚠️ No videos found for '{SEARCH_QUERY}'.")
    else:
        print(f"\\nšŸŽ¬ Found {len(data['hits'])} videos for '{SEARCH_QUERY}'. Downloading...\\n")

        for i, hit in enumerate(tqdm(data['hits'], desc=f'Downloading {SEARCH_QUERY}')):
            videos = hit.get('videos', {})
            video_url = videos.get(VIDEO_QUALITY, {}).get('url')

            if not video_url:
                print(f"⚠️ No '{VIDEO_QUALITY}' quality video available for item {i+1}. Skipping.")
                continue

            file_name = os.path.join(SAVE_FOLDER, f"{SEARCH_QUERY.replace(' ', '_').lower()}_{i+1}.mp4")

            try:
                with requests.get(video_url, stream=True) as r:
                    r.raise_for_status()
                    total_size = int(r.headers.get('content-length', 0))

                    with open(file_name, 'wb') as f:
                        for chunk in tqdm(r.iter_content(1024), total=total_size // 1024, unit='KB', leave=False):
                            f.write(chunk)

                print(f"✅ Downloaded: {file_name}")
            except Exception as e:
                print(f"❌ Failed to download video {i+1}: {e}")

        print(f"\\n✅ Successfully downloaded {len(data['hits'])} videos for '{SEARCH_QUERY}'.")
else:
    print(f"❌ Error fetching data: {response.status_code}")

print("\\nšŸŽ‰ Download complete.")

⚙️ Step 4: Run the Script

Save the file and run it in your terminal:

python pixabay_video_downloader.py

The script will automatically create a videos folder and save all downloaded videos there. You can easily change the topic, number of videos, or video quality.

🧠 How It Works

  • The script sends a request to the Pixabay Video API with your search query.
  • It retrieves metadata for the top video results.
  • Each video file URL is extracted and downloaded with requests.
  • The tqdm library shows live download progress.

šŸ’” Use Cases

  • Creating a stock video library for editing or AI models.
  • Downloading background videos for projects or websites.
  • Building automation tools that collect media assets by topic.

šŸŽÆ Project Summary

  • Language: Python
  • Libraries: requests, tqdm
  • API: Pixabay Video API
  • Output: Folder containing downloaded .mp4 videos

šŸ“¢ Stay Connected

  • Join our WhatsApp community for more Python tutorials, automation projects, and coding hacks.

❤️ If you enjoyed this project, share it with your friends and help more developers explore Python automation!

Previous Post Next Post

Contact Form