Skip to main content

pytubefix_logo

Python3 Library for Downloading YouTube Videos

Installation

pip install pytubefix

Quickstart

Download MP4 Video in Highest Resolution:

from pytubefix import YouTube
from pytubefix.cli import on_progress

url = "url"

yt = YouTube(url, on_progress_callback=on_progress)
print(yt.title)

ys = yt.streams.get_highest_resolution()
ys.download()

Download Audio-Only (.m4a):

from pytubefix import YouTube
from pytubefix.cli import on_progress

url = "url"

yt = YouTube(url, on_progress_callback=on_progress)
print(yt.title)

ys = yt.streams.get_audio_only()
ys.download()

Download a Complete Playlist:

from pytubefix import Playlist
from pytubefix.cli import on_progress

url = "url"

pl = Playlist(url)
for video in pl.videos:
    ys = video.streams.get_audio_only()
    ys.download()

Use OAuth Authentication:

from pytubefix import YouTube
from pytubefix.cli import on_progress

url = "url"

yt = YouTube(url, use_oauth=True, allow_oauth_cache=True, on_progress_callback=on_progress)
ys = yt.streams.get_highest_resolution()
ys.download()  # Authenticate once for subsequent downloads

Specify Output Directory for Downloads:

from pytubefix import YouTube
from pytubefix.cli import on_progress

url = "url"

yt = YouTube(url, on_progress_callback=on_progress)
ys = yt.streams.get_highest_resolution()
ys.download(output_path="path/to/directory")

Working with Subtitles/Caption Tracks

View Available Subtitles:

from pytubefix import YouTube

yt = YouTube('http://youtube.com/watch?v=2lAe1cqCOXo')
print(yt.captions)

Print Subtitle Tracks:

from pytubefix import YouTube

yt = YouTube('http://youtube.com/watch?v=2lAe1cqCOXo')
caption = yt.captions['a.en']
print(caption.generate_srt_captions())

Save Subtitles to a Text File:

from pytubefix import YouTube

yt = YouTube('http://youtube.com/watch?v=2lAe1cqCOXo')
caption = yt.captions['a.en']
caption.save_captions("captions.txt")

Using Channels

Get Channel Name:

from pytubefix import Channel

c = Channel("https://www.youtube.com/@ProgrammingKnowledge/featured")
print(f'Channel name: {c.channel_name}')

Download All Videos from a Channel:

from pytubefix import Channel

c = Channel("https://www.youtube.com/@ProgrammingKnowledge")
print(f'Downloading videos by: {c.channel_name}')

for video in c.videos:
    video.streams.get_highest_resolution().download()

Search for Videos

Basic Search:

from pytubefix import Search

results = Search('GitHub Issue Best Practices')
for video in results.videos:
    print(f'Title: {video.title}')
    print(f'URL: {video.watch_url}')
    print(f'Duration: {video.length} sec')
    print('---')

Use Filters:

from pytubefix.contrib.search import Search, Filter

filters = (
    Filter.create()
        .upload_date(Filter.UploadDate.TODAY)
        .type(Filter.Type.VIDEO)
        .duration(Filter.Duration.UNDER_4_MINUTES)
        .feature([Filter.Features.CREATIVE_COMMONS, Filter.Features._4K])
        .sort_by(Filter.SortBy.UPLOAD_DATE)
     )

s = Search('music', filters=filters)
for video in s.videos:
    print(video.watch_url)

AsyncYouTube — Advanced Guide with Complete Examples

AsyncYouTube is a fully asynchronous Python interface built on PyTubeFix, intended for developers who require complete control over YouTube video data. It provides access to video streams, metadata, chapters, key moments, and more — all without blocking your event loop.


Quick Start Example

A full program demonstrating basic usage:

import asyncio
from pytubefix import AsyncYouTube

URL = "YOUR_VIDEO_URL"

async def main():
    # Initialize AsyncYouTube with OAuth to handle age-restricted content
    yt = AsyncYouTube(URL, use_oauth=True, allow_oauth_cache=True)
    
    # Fetch all available streams asynchronously
    streams = await yt.streams()
    print("Available Streams:")
    for stream in streams:
        print(stream)

if __name__ == '__main__':
    asyncio.run(main())

Download a Specific Stream

Complete example showing download with progress and completion callbacks:

import asyncio
from pytubefix import AsyncYouTube

URL = "YOUR_VIDEO_URL"

async def main():
    def on_progress(stream, chunk, bytes_remaining):
        total = stream.filesize
        percent = (1 - bytes_remaining / total) * 100
        print(f"\rProgress: {percent:.2f}%", end="")

    def on_complete(stream, file_path):
        print(f"\n√ Done downloading: {file_path}")

    yt = AsyncYouTube(URL, use_oauth=True, allow_oauth_cache=True)

    yt.register_on_progress_callback(on_progress)
    yt.register_on_complete_callback(on_complete)

    stream = await yt.get_stream_by_itag(18) # 360p MP4 progressive stream

    print(f"Downloading: {await yt.title()}")

    stream.download(filename="my_video.mp4") # Blocking call by design

if __name__ == '__main__':
    asyncio.run(main())

Note: Always use callbacks to track progress; download() is synchronous.


Fetch Video Metadata

import asyncio
from pytubefix import AsyncYouTube

URL = "YOUR_VIDEO_URL"

async def main():
    yt = AsyncYouTube(URL, use_oauth=True, allow_oauth_cache=True)

    title = await yt.title()
    views = await yt.views()
    likes = await yt.likes()
    author = await yt.author()
    thumbnail = await yt.thumbnail_url()

    print(f"Title: {title}")
    print(f"Views: {views}")
    print(f"Likes: {likes}")
    print(f"Author: {author}")
    print(f"Thumbnail URL: {thumbnail}")

if __name__ == '__main__':
    asyncio.run(main())

Retrieve Chapters and Key Moments

import asyncio
from pytubefix import AsyncYouTube

URL = "YOUR_VIDEO_URL"

async def main():
    yt = AsyncYouTube(URL, use_oauth=True, allow_oauth_cache=True)

    chapters = await yt.chapters()
    key_moments = await yt.key_moments()

    print("Chapters:", chapters)
    print("Key Moments:", key_moments)

if __name__ == '__main__':
    asyncio.run(main())

Create AsyncYouTube from Video ID

import asyncio
from pytubefix import AsyncYouTube

VIDEO_ID = "YOUR_VIDEO_ID"

async def main():
    yt = AsyncYouTube.from_id(VIDEO_ID, use_oauth=True, allow_oauth_cache=True)
    streams = await yt.streams()
    print("Streams fetched from Video ID:")
    for s in streams:
        print(s)

if __name__ == '__main__':
    asyncio.run(main())

Best Practices

  • Always await asynchronous methods: streams(), title(), views(), likes(), chapters(), key_moments().
  • Use use_oauth=True to handle age-restricted content; cache tokens to minimize repeated logins.
  • Wrap network calls in try/except to handle errors gracefully.
  • Combine callbacks with asyncio for efficient non-blocking downloads.
  • Maintain consistent program structure with main() and asyncio.run() for readability and maintainability.

Download files

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

Source Distribution

pytubefix-10.11.0.tar.gz (1.5 MB view details)

Uploaded Source

Built Distribution

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

pytubefix-10.11.0-py3-none-any.whl (1.5 MB view details)

Uploaded Python 3

File details

Details for the file pytubefix-10.11.0.tar.gz.

File metadata

  • Download URL: pytubefix-10.11.0.tar.gz
  • Upload date:
  • Size: 1.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.4

File hashes

Hashes for pytubefix-10.11.0.tar.gz
Algorithm Hash digest
SHA256 7fdba0a641aaf72b33df4983ddf26f5798f7e81e5fa72ae00d39bce99c821a99
MD5 a0b14f504a0eba16b71f78fb313eb523
BLAKE2b-256 33127c8bcedf3cb60a310420c8eabfbdeec81ba7fa4c11fa71e9c5a330539732

See more details on using hashes here.

File details

Details for the file pytubefix-10.11.0-py3-none-any.whl.

File metadata

  • Download URL: pytubefix-10.11.0-py3-none-any.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.4

File hashes

Hashes for pytubefix-10.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d1b0a01f38853e9cdf3e047c91e82d022c9054650340e19f65fe4cb4af65d7f1
MD5 9e502ff88ea664b3d2d1acef1a907677
BLAKE2b-256 62c6b202c03110cf35b129483042692a7471da435d14d9d2b508963afaad9f90

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

10.11.0 This release

2 files

10.10.1

2 files

10.10.0

2 files

10.9.0

2 files

10.8.1

2 files

10.7.3

2 files

10.7.2

2 files

10.7.1

2 files

10.6.1

2 files

10.5.0

2 files

10.4.0

2 files

10.3.8

2 files

10.3.6

2 files

10.3.5

2 files

10.3.4

2 files

10.3.3

2 files

10.2.1

2 files

10.1.1

2 files

10.0.1

2 files

10.0.0

2 files

9.5.1

2 files

9.5.0

2 files

9.4.1

2 files

9.3.0

2 files

9.2.2

2 files

9.2.1

2 files

9.2.0

2 files

9.1.2

2 files

9.1.1

2 files

9.0.1

2 files

9.0.0

2 files

8.13.1

2 files

8.12.3

2 files

8.12.2

2 files

8.12.1

2 files

8.12.0

2 files

8.11.0

2 files

8.10.2

2 files

8.10.1

2 files

8.10.0

2 files

8.9.0

2 files

8.8.5

2 files

8.8.4

2 files

8.8.3

2 files

8.8.2

2 files

8.8.1

2 files

8.8.0

2 files

8.7.0

2 files

8.6.0

2 files

8.5.3

2 files

8.5.2

2 files

8.5.1

2 files

8.4.1

2 files

8.4.0

2 files

8.3.2

2 files

8.3.0

2 files

8.2.0

2 files

8.1.1

2 files

8.0.0

2 files

7.4.0

2 files

7.3.1

2 files

7.3.0

2 files

7.2.2

2 files

7.1.3

2 files

7.0.0

2 files

6.17.0

2 files

6.16.3

2 files

6.16.2

2 files

6.16.1

2 files

6.15.4

2 files

6.15.2

2 files

6.15.1

2 files

6.15.0

2 files

6.14.0

2 files

6.13.1

2 files

6.13.0

2 files

6.12.0

2 files

6.11.0

2 files

6.10.2

2 files

6.9.2

2 files

6.9.1

2 files

6.8.1

2 files

6.7.0

2 files

6.6.3

2 files

6.6.2

2 files

6.5.3

2 files

6.5.2

2 files

6.5.1

2 files

6.4.2

2 files

6.3.4

2 files

6.3.3

2 files

6.2.2

2 files

6.1.2

2 files

6.1.1

2 files

6.0.0

2 files

5.8.0

2 files

5.7.0

2 files

5.6.3

2 files

5.5.0

2 files

5.4.2

2 files

5.3.0

2 files

5.2.0

2 files

5.1.2

2 files

5.1.1

2 files

5.0.0

2 files

4.0.0

2 files

3.1.0

2 files

3.0.0

2 files

2.3.2

2 files

2.3.1

2 files

2.3.0

2 files

2.2.1

2 files

2.1.1

2 files

2.0.0

2 files

1.13.3

2 files

1.13.2

2 files

1.13.1

2 files

1.12.0

2 files

1.11.0

2 files

1.10.0

2 files

1.9.0

2 files

1.8.3

2 files

1.8.2

2 files

1.8.1

2 files

1.8.0

2 files

1.7.1

1 file

1.7.0

2 files

1.6.0

2 files

1.5.0

1 file

1.4.1

1 file

1.3.0

1 file

1.2.0

1 file

1.1.2

1 file

1.1.1

1 file

1.0.0

1 file

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