Skip to main content

Python client for yt-download-api — download YouTube videos via HTTP from any script or bot.

Project description

yt-download-client

Python client for yt-download-api.

Download YouTube videos from any Python script, Discord bot, or Telegram bot — without dealing with HTTP, polling, or error handling yourself.

pip install yt-download-client

Requirements

A running instance of yt-download-api. Deploy your own in one click or run it locally.


Quick start

from yt_download_client import YTDownloadClient

client = YTDownloadClient(
    base_url="https://your-api.onrender.com",
    api_key="YOUR_KEY",
)

path = client.download("https://youtu.be/dQw4w9WgXcQ")
print(f"Saved to {path}")

Usage

One-liner download

path = client.download(
    url="https://youtu.be/dQw4w9WgXcQ",
    format="mp4",
    quality="720p",
    output="video.mp4",
)

Supported formats: mp4, mp3, webm Supported qualities: best, 1080p, 720p, 480p, 360p, audio_only

With progress tracking

from yt_download_client import Job

def on_progress(job: Job):
    if job.status == "progress" and job.progress:
        downloaded = job.progress.get("downloaded_bytes", 0)
        total = job.progress.get("total_bytes", 0)
        if total:
            pct = round(downloaded / total * 100)
            print(f"{pct}%", end="\r")

path = client.download(
    "https://youtu.be/dQw4w9WgXcQ",
    on_progress=on_progress,
)

Step by step

For more control — submit, wait, and fetch separately:

# Submit and get a job ID immediately
job = client.submit("https://youtu.be/dQw4w9WgXcQ", format="mp3", quality="audio_only")
print(f"Job submitted: {job.id}")

# Wait until done (blocks, polls every 5s)
job = client.wait(job.id, poll_interval=5, timeout=300)

# Download the file
path = client.fetch(job.id, output="song.mp3")
print(f"Saved to {path}")

Get video info before downloading

info = client.info("https://youtu.be/dQw4w9WgXcQ")
print(info.title)
print(info.duration_formatted)  # "03:33"
print(info.uploader)
for fmt in info.formats:
    print(fmt["resolution"], fmt["ext"])

Discord bot example

import discord
from yt_download_client import YTDownloadClient, JobFailedError

client = YTDownloadClient(base_url="https://your-api.onrender.com", api_key="YOUR_KEY")
bot = discord.Client(intents=discord.Intents.default())

@bot.event
async def on_message(message):
    if message.content.startswith("!download "):
        url = message.content.split(" ", 1)[1]
        await message.channel.send("Downloading...")
        try:
            path = client.download(url, format="mp4", quality="720p")
            await message.channel.send(file=discord.File(str(path)))
        except JobFailedError as e:
            await message.channel.send(f"Failed: {e}")

Error handling

from yt_download_client import (
    YTDownloadClient,
    AuthenticationError,
    RateLimitError,
    JobFailedError,
    JobTimeoutError,
    ServerBusyError,
    YTDownloadError,
)

try:
    client.download("https://youtu.be/dQw4w9WgXcQ")
except AuthenticationError:
    print("Bad API key")
except RateLimitError as e:
    print(f"Slow down: {e}")
except JobFailedError as e:
    print(f"Download failed: {e}")
except JobTimeoutError:
    print("Took too long")
except ServerBusyError:
    print("Server is busy, retry later")
except YTDownloadError as e:
    print(f"Something went wrong: {e}")

API reference

YTDownloadClient(base_url, api_key, timeout=30)

Method Description
download(url, format, quality, output, poll_interval, timeout, on_progress) Full flow in one call
submit(url, format, quality, webhook_url)Job Submit a job
wait(job_id, poll_interval, timeout, on_progress)Job Wait for completion
fetch(job_id, output)Path Download the output file
status(job_id)Job Get current job status
info(url)VideoInfo Fetch video metadata
health()dict Check API health
delete(job_id)dict Delete a file from the server

Job

Attribute Type Description
id str Job ID
status str pending, started, progress, post_processing, success, failure
progress dict Progress info (speed, ETA, bytes)
download_url str Relative download path when done
error str Error message on failure
done bool True if success or failure
successful bool True if success

VideoInfo

Attribute Type Description
title str Video title
duration int Duration in seconds
duration_formatted str Human-readable duration (03:33)
uploader str Channel name
thumbnail str Thumbnail URL
formats list Available formats
filesize_approx int Approximate file size in bytes

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yt_download_client-1.0.0.tar.gz (27.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yt_download_client-1.0.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file yt_download_client-1.0.0.tar.gz.

File metadata

  • Download URL: yt_download_client-1.0.0.tar.gz
  • Upload date:
  • Size: 27.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.17.0 {"ci":null,"cpu":"x86_64","distro":{"id":"noble","libc":{"lib":"glibc","version":"2.39"},"name":"Ubuntu","version":"24.04"},"implementation":{"name":"CPython","version":"3.12.1"},"installer":{"name":"hatch","version":"1.17.0"},"openssl_version":"OpenSSL 3.0.13 30 Jan 2024","python":"3.12.1","system":{"name":"Linux","release":"6.8.0-1052-azure"}} HTTPX2/2.4.0

File hashes

Hashes for yt_download_client-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b03dfbfd784b3a340df0637ebcc1e0533c654e9f4103f4bdb1b10e818ff20543
MD5 db30b2e5595fbe26b3b1ec9a8e13aedb
BLAKE2b-256 5a54c3eb117f545f922d8d491b7cf9e87a970a8b7d33f21930f59e09fc18ebcc

See more details on using hashes here.

File details

Details for the file yt_download_client-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: yt_download_client-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.17.0 {"ci":null,"cpu":"x86_64","distro":{"id":"noble","libc":{"lib":"glibc","version":"2.39"},"name":"Ubuntu","version":"24.04"},"implementation":{"name":"CPython","version":"3.12.1"},"installer":{"name":"hatch","version":"1.17.0"},"openssl_version":"OpenSSL 3.0.13 30 Jan 2024","python":"3.12.1","system":{"name":"Linux","release":"6.8.0-1052-azure"}} HTTPX2/2.4.0

File hashes

Hashes for yt_download_client-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4215ab1b8303c0a6c4af6abed977b6c3d0a6417fc610e624fa4a7fb1793737f2
MD5 61ce496bdb2f7bc1d5bd5669d44dd286
BLAKE2b-256 cad5f73162651c020560eb113af1f099d960707e51bb130067d239626f319099

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page