Skip to main content

ytextract

ytextract is a genuine, lightweight Python library (and command-line utility) for downloading YouTube videos.

Description

YouTube is the most popular video-sharing platform in the world and as a hacker, you may encounter a situation where you want to script something to download videos. For this, I present to you: ytextract.

ytextract is a lightweight library written in Python. It has minimal dependencies and aims to be highly reliable.

ytextract also makes pipelining easy, allowing you to specify callback functions for different download events, such as on progress or on complete.

Furthermore, ytextract includes a command-line utility, allowing you to download videos right from the terminal.

Features

  • Support for both progressive & DASH streams
  • Easily register on_download_progress & on_download_complete callbacks
  • Command-line interface included
  • Caption track support
  • Outputs caption tracks to .srt format (SubRip Subtitle)
  • Ability to capture thumbnail URL
  • Extensively documented source code

Quickstart

Installation

ytextract requires an installation of Python 3.7 or greater, as well as pip. (Pip is typically bundled with Python installations.) If installing from source, git is also required.

To install from PyPI with pip:

pip install ytextract

Or install from source:

git clone https://github.com/Josh-XT/ytextract
pip install -e .

Using the command-line interface

Use the ytextract command in a terminal to download videos, captions, or multiple videos from a list or channel.

Download a single video

To download a video at the highest progressive quality, you can use the following command:

ytextract https://youtube.com/watch?v=2lAe1cqCOXo

Download captions for a video

To download only captions for a video, use the --captions flag:

ytextract --captions https://youtube.com/watch?v=2lAe1cqCOXo

Download Videos from a list in a text file

To download multiple videos from a text file containing YouTube video URLs (one URL per line), use the --file flag:

ytextract --file videos.txt

Download Videos from a Channel, or multiple Channels

To download all videos from one or more YouTube channels, use the --channels flag followed by the channel usernames:

ytextract --channels officialalphablocks Numberblocks

YouTube Proxy Server

ytextract includes a built-in proxy server that can cache and stream YouTube videos over your local network. This is perfect for:

  • Home NAS setups - Run the server on your NAS and stream videos to devices with restricted internet access
  • Limited bandwidth connections - Download videos once and stream locally for repeat watches, saving bandwidth
  • Offline viewing - Pre-download videos and serve them to devices without internet access
  • Parental controls - Restrict direct YouTube access while still allowing curated content

Starting the server

Start the server in the foreground:

ytextract --server start

Start the server in the background (daemon mode):

ytextract --server start --daemon

You can also specify a custom host and port:

ytextract --server start --host 0.0.0.0 --port 8765 --daemon

Stopping the server

ytextract --server stop

Checking server status

ytextract --server status

API Endpoints

Once the server is running, you can access the following endpoints:

Endpoint Method Description
/ GET API information and available endpoints
/health GET Health check
/watch?v={video_id} GET Stream a video (auto-downloads if not cached)
/download/video POST Download a video by URL
/download/captions POST Download captions only
/download/file POST Download videos from a file
/download/channels POST Download videos from channels

Streaming videos

To stream a video, simply navigate to:

http://your-server:8765/watch?v=VIDEO_ID

If the video is already cached locally, it streams immediately. If not, it downloads the video first and then streams it. You can also specify a language for captions:

http://your-server:8765/watch?v=VIDEO_ID&language=en-US

Example: Download and stream a video via API

# Download a video
curl -X POST http://localhost:8765/download/video \
  -H "Content-Type: application/json" \
  -d '{"url": "https://youtube.com/watch?v=VIDEO_ID", "language": "en-US"}'

# Stream the video in a browser
# Open: http://localhost:8765/watch?v=VIDEO_ID

Using ytextract in a Python script

To download a video using the library in a script, simply import ytextract and call the helper functions directly.

Download a single video

Set the url parameter to the YouTube video URL you wish to download.

import ytextract

ytextract.download(url="https://www.youtube.com/watch?v=VIDEO_ID")

Download captions for a video

Set the url parameter to the YouTube video URL you wish to download captions for.

import ytextract

ytextract.download_captions(url="https://www.youtube.com/watch?v=VIDEO_ID")

Download Videos from a list from videos.txt

You can change the filename argument to any text file containing YouTube video URLs (one URL per line).

import ytextract

ytextract.download_videos_from_list(filename="videos.txt")

Download Videos from a Channel, or multiple Channels

You can specify one or more channel usernames in the channels parameter to download all videos from those channels.

import ytextract

ytextract.download_videos_from_channels(channels=["officialalphablocks", "Numberblocks"])

License

This project is licensed under The Unlicense - see the LICENSE file for details.

Contributing

Feel free to open an issue or a pull request at https://github.com/Josh-XT/ytextract

Download files

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

Source Distribution

ytextract-0.0.4.tar.gz (52.5 kB view details)

Uploaded Source

Built Distribution

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

ytextract-0.0.4-py3-none-any.whl (55.0 kB view details)

Uploaded Python 3

File details

Details for the file ytextract-0.0.4.tar.gz.

File metadata

  • Download URL: ytextract-0.0.4.tar.gz
  • Upload date:
  • Size: 52.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for ytextract-0.0.4.tar.gz
Algorithm Hash digest
SHA256 c1950470d5defc7c42a9b1c64e4f031aa1aaeeaa9268ec5836b17afd4a5fa761
MD5 c0a8dbb63b64dce4ad3ff11e030a2161
BLAKE2b-256 a2b518b5bf25aff5adb2b1ad436aa1075ee6e012c4c2db96051d9a95fb1a2659

See more details on using hashes here.

File details

Details for the file ytextract-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: ytextract-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 55.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for ytextract-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 26af9f79d51951b8552f5275e597cf90a1b308b72bcfde7cafe32ee1d3db6d93
MD5 541fa6b5418fac7aafd35c5460cdf829
BLAKE2b-256 e71485cb6cfc0585034acde162724075ece32987188cedd88b5fd2de7274194b

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

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