Skip to main content

Python 3 library for downloading YouTube Videos.

Project description

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

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

ytextract-0.0.2.tar.gz (51.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.2-py3-none-any.whl (54.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ytextract-0.0.2.tar.gz
Algorithm Hash digest
SHA256 0e7b412d400314a4fb07dafbefe8b6ad8daa60bd75408eef6d2d1968de10d2a5
MD5 b1356e7c0b5976a9deac423828504183
BLAKE2b-256 0683b4f7efda08a5ce7d2cc93c6e99d16b6fb54b510e63837d9649e065c1aed0

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for ytextract-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0b1d16d5add2d9b40494b75232bb7256182c224f20b9538422d1581b5522cfa2
MD5 f4213ccbf6eda72fd16f218d6c5589bb
BLAKE2b-256 11d64a160d8b4505218ce7ad852a473785272bba5201d236e75400e4183b83ff

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