Skip to main content

YouTube Video Information Extractor

CI PyPI version Python Support License: MIT Code Coverage Code Quality

A robust Python library for extracting YouTube video metadata including title, description, channel name, publication date, and view count with multiple extraction strategies.

Features

  • Multiple Extraction Strategies: YouTube Data API v3 (official), yt-dlp, pytubefix
  • Automatic Fallback: Seamlessly switches between methods if one fails
  • Simple Input: Accepts YouTube video IDs only (11 characters)
  • Batch Processing: Extract information from multiple videos efficiently
  • Multiple Output Formats: Text, JSON, CSV
  • Command Line Interface: Easy-to-use CLI for quick extractions
  • Python Library: Full programmatic access for integration
  • Robust Error Handling: Graceful handling of failures with retry logic
  • Rate Limiting: Built-in delays to respect YouTube's servers

Installation

pip install yt-info-extract

Optional Dependencies

For the best experience, install all extraction backends:

# For YouTube Data API v3 support (recommended)
pip install google-api-python-client

# For yt-dlp support (most robust fallback)
pip install yt-dlp

# For pytubefix support (lightweight fallback)
pip install pytubefix

Quick Start

Python Library Usage

from yt_info_extract import get_video_info

# Extract video information
info = get_video_info("jNQXAC9IVRw")

print(f"Title: {info['title']}")
print(f"Channel: {info['channel_name']}")
print(f"Views: {info['views']:,}")
print(f"Published: {info['publication_date']}")

Command Line Usage

# Extract video information
yt-info jNQXAC9IVRw

# Export to JSON
yt-info -f json -o video.json jNQXAC9IVRw

# Process multiple videos
yt-info --batch video_ids.txt --output-dir results/

# Use specific extraction strategy
yt-info -s api --api-key YOUR_KEY jNQXAC9IVRw

The YouTube Data API v3 is the official, most reliable method. It requires a free API key:

  1. Go to Google Cloud Console
  2. Create a new project or select an existing one
  3. Enable the YouTube Data API v3
  4. Create credentials (API Key)
  5. Restrict the API key to YouTube Data API v3
# Set your API key as environment variable
export YOUTUBE_API_KEY="your_api_key_here"

# Now use the library
yt-info jNQXAC9IVRw

Usage Examples

Basic Usage

from yt_info_extract import YouTubeVideoInfoExtractor

# Initialize extractor
extractor = YouTubeVideoInfoExtractor(api_key="your_key")

# Extract video info
info = extractor.get_video_info("jNQXAC9IVRw")

if info:
    print(f"Title: {info['title']}")
    print(f"Channel: {info['channel_name']}")
    print(f"Views: {info['views']:,}")
    print(f"Published: {info['publication_date']}")
    print(f"Description: {info['description'][:100]}...")

Batch Processing

from yt_info_extract import get_video_info_batch

video_ids = ["jNQXAC9IVRw", "dQw4w9WgXcQ", "_OBlgSz8sSM"]
results = get_video_info_batch(video_ids)

for result in results:
    if not result.get('error'):
        print(f"{result['title']} - {result['views']:,} views")

Export Data

from yt_info_extract import get_video_info, export_video_info

# Get video info
info = get_video_info("jNQXAC9IVRw")

# Export to JSON
export_video_info(info, "video.json")

# Export batch results to CSV
batch_results = get_video_info_batch(["jNQXAC9IVRw", "dQw4w9WgXcQ"])
export_video_info(batch_results, "videos.csv", format_type="csv")

Video ID Format

from yt_info_extract import get_video_info

# Only video IDs are accepted (11 characters: A-Z, a-z, 0-9, _, -):
video_id = "jNQXAC9IVRw"
info = get_video_info(video_id)
print(f"✓ {video_id} -> {info['title']}")

# URLs are NOT supported - extract the video ID manually if needed:
# https://www.youtube.com/watch?v=jNQXAC9IVRw -> jNQXAC9IVRw

Command Line Interface

Basic Commands

# Extract video information
yt-info jNQXAC9IVRw

# Use different output formats
yt-info -f compact jNQXAC9IVRw
yt-info -f stats jNQXAC9IVRw
yt-info -f json jNQXAC9IVRw

# Export to file
yt-info -f json -o video.json jNQXAC9IVRw
yt-info -f csv -o video.csv jNQXAC9IVRw

Batch Processing

# Create a file with video IDs only (one per line)
echo "jNQXAC9IVRw" > video_ids.txt
echo "dQw4w9WgXcQ" >> video_ids.txt

# Process all videos
yt-info --batch video_ids.txt --output-dir results/

# With summary report
yt-info --batch video_ids.txt --summary --output-dir results/

API Key Usage

# Method 1: Environment variable
export YOUTUBE_API_KEY="your_api_key_here"
yt-info jNQXAC9IVRw

# Method 2: Command line argument
yt-info --api-key "your_api_key_here" jNQXAC9IVRw

# Force specific strategy
yt-info -s api --api-key "your_key" jNQXAC9IVRw
yt-info -s yt_dlp jNQXAC9IVRw

Utility Commands

# Test API key
yt-info --test-api

# List available strategies
yt-info --list-strategies

# Verbose output
yt-info -v jNQXAC9IVRw

Extraction Strategies

  • Pros: Official, reliable, comprehensive data, compliant with ToS
  • Cons: Requires API key, has quota limits (10,000 units/day free)
  • Best for: Production applications, commercial use, reliable automation
extractor = YouTubeVideoInfoExtractor(api_key="your_key", strategy="api")

2. yt-dlp (Most Robust Fallback)

  • Pros: No API key needed, very robust, actively maintained
  • Cons: Violates YouTube ToS, can break with YouTube updates
  • Best for: Personal projects, research, when API quotas are insufficient
extractor = YouTubeVideoInfoExtractor(strategy="yt_dlp")

3. pytubefix (Lightweight Fallback)

  • Pros: No API key needed, simple, lightweight
  • Cons: Violates YouTube ToS, less robust than yt-dlp
  • Best for: Simple scripts, minimal dependencies
extractor = YouTubeVideoInfoExtractor(strategy="pytubefix")

4. Auto Strategy (Default)

Automatically tries strategies in order of preference:

  1. YouTube Data API v3 (if API key available)
  2. yt-dlp (if installed)
  3. pytubefix (if installed)
extractor = YouTubeVideoInfoExtractor(strategy="auto")  # Default

Data Structure

Each video information dictionary contains:

{
    "title": "Video title",
    "description": "Full video description",
    "channel_name": "Channel name",
    "publication_date": "2005-04-23T00:00:00Z",  # ISO format
    "views": 123456789,  # Integer view count
    "extraction_method": "youtube_api"  # Method used
}

Configuration Options

extractor = YouTubeVideoInfoExtractor(
    api_key="your_key",           # YouTube Data API key
    strategy="auto",              # Extraction strategy
    timeout=30,                   # Request timeout (seconds)
    max_retries=3,                # Maximum retry attempts
    backoff_factor=0.75,          # Exponential backoff factor
    rate_limit_delay=0.1,         # Delay between requests
)

Error Handling

The library handles errors gracefully:

info = get_video_info("invalid_video_id")
if info:
    print(f"Success: {info['title']}")
else:
    print("Failed to extract video information")

# For batch processing, check individual results
results = get_video_info_batch(["valid_id", "invalid_id"])
for result in results:
    if result.get('error'):
        print(f"Error: {result['error']}")
    else:
        print(f"Success: {result['title']}")
  • YouTube Data API v3: Fully compliant with YouTube's Terms of Service
  • yt-dlp and pytubefix: Violate YouTube's Terms of Service by scraping data

For commercial applications or production use, always use the YouTube Data API v3.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Submit a pull request

License

MIT License - see LICENSE file for details.

Support

  • yt-ts-extract - YouTube transcript extraction
  • yt-dlp - YouTube downloader (used as fallback)
  • pytubefix - YouTube library (used as fallback)

Metadata

Release files for yt-info-extract 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for yt-info-extract 1.1.0
File Size Uploaded Provenance
yt_info_extract-1.1.0.tar.gz 38.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for yt-info-extract 1.1.0
File Interpreter ABI Platform
yt_info_extract-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 56.6 kB

Release files / yt_info_extract-1.1.0.tar.gz

Download URL yt_info_extract-1.1.0.tar.gz
Size 38.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8a507dbf4717a77abf4bd07e143ef147dda28722b8f2f149775346d684d8683d
BLAKE2b-256 checksum
How to use checksums
e6596927a9dde3b79a5758399b940dcaffd47bea693917aee7a306d2c3afc707
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / yt_info_extract-1.1.0-py3-none-any.whl

Download URL yt_info_extract-1.1.0-py3-none-any.whl
Size 18.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a1b9ad12db0aa242a2523b6a5204a382fe5361f004f9f56c873efc5c166e2c8c
BLAKE2b-256 checksum
How to use checksums
a791daf20e0432fac4f8f950df67e080f0f9f73588f2d4da2839d3c7cd63b290
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Security for yt-info-extract 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page