Skip to main content

Python3 library for downloading YouTube Videos.

Project description

Pytubefix

PyPI - Downloads GitHub Sponsors PyPI - License Read the Docs GitHub Tag PyPI - Version

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 = {
    'upload_date': Filter.get_upload_date('Today'),
    'type': Filter.get_type("Video"),
    'duration': Filter.get_duration("Under 4 minutes"),
    'features': [Filter.get_features("4K"), Filter.get_features("Creative Commons")],
    'sort_by': Filter.get_sort_by("Upload date")
}

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

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

pytubefix-9.4.1.tar.gz (758.0 kB view details)

Uploaded Source

Built Distribution

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

pytubefix-9.4.1-py3-none-any.whl (768.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytubefix-9.4.1.tar.gz
  • Upload date:
  • Size: 758.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for pytubefix-9.4.1.tar.gz
Algorithm Hash digest
SHA256 a3466cbbe2659f866c2dd91bf8ebdc8256e7494c9c3733292813b313daafcaad
MD5 db5b108fdd5027962cf5fa380a069b7a
BLAKE2b-256 899f389cff9de23298bef0b12d922d8ae40cf7c35ab8ca5f1436a6b7d5f538d1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytubefix-9.4.1-py3-none-any.whl
  • Upload date:
  • Size: 768.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for pytubefix-9.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e8625c1f1041b1cf91578843b32a3575b81e3eb49a0ad757b3fc7b9772d85680
MD5 dadaf7af5d6b6a6ab1aa19a067516179
BLAKE2b-256 023900a23743e47e62ac254f29ec30c8f6d340847c3b69e5f22f8f28493ecf37

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