API¶
YouTube Object¶
- class pytubefix.YouTube(url: str, client: str = 'ANDROID_VR', on_progress_callback: Callable[[Any, bytes, int], None] | None = None, on_complete_callback: Callable[[Any, str | None], None] | None = None, proxies: Dict[str, str] | None = None, use_oauth: bool = False, allow_oauth_cache: bool = True, token_file: str | None = None, oauth_verifier: Callable[[str, str], None] | None = None, use_po_token: bool | None = False, po_token_verifier: Callable[[None], Tuple[str, str]] | None = None)[source]¶
Core developer interface for pytubefix.
- age_check()[source]¶
If the video has any age restrictions, you must confirm that you wish to continue.
Originally the WEB client was used, but with the implementation of PoToken we switched to MWEB.
Interface to query caption tracks.
- Return type:
CaptionQuery
.
- property channel_url: str¶
Construct the channel url for the video’s poster from the channel id.
- Return type:
- check_availability()[source]¶
Check whether the video is available.
Raises different exceptions based on why the video is unavailable, otherwise does nothing.
- property fmt_streams¶
Returns a list of streams if they have been initialized.
If the streams have not been initialized, finds all relevant streams and initializes them.
- property metadata: YouTubeMetadata | None¶
Get the metadata for the video.
- Return type:
YouTubeMetadata
- property pot: str¶
Retrieves the poToken generated by botGuard.
This poToken only works for WEB-based clients.
- property publish_date¶
Get the publish date.
- Return type:
datetime
- register_on_complete_callback(func: Callable[[Any, str | None], None])[source]¶
Register a download complete callback function post initialization.
- Parameters:
func (callable) – A callback function that takes
stream
andfile_path
.- Return type:
None
- register_on_progress_callback(func: Callable[[Any, bytes, int], None])[source]¶
Register a download progress callback function post initialization.
- Parameters:
func (callable) –
- A callback function that takes
stream
,chunk
, and
bytes_remaining
as parameters.
- A callback function that takes
- Return type:
None
- property signature_timestamp: dict¶
WEB clients need to be signed with a signature timestamp.
The signature is found inside the player’s base.js.
- Return type:
Dict
- property streaming_data¶
Return streamingData from video info.
- property streams: StreamQuery¶
Interface to query both adaptive (DASH) and progressive streams.
- Return type:
StreamQuery
.
- property vid_details¶
Parse the raw vid details and return the parsed result.
The official player sends a request to the next endpoint to obtain some details of the video.
- Return type:
Dict[Any, Any]
- property vid_info¶
Parse the raw vid info and return the parsed result.
- Return type:
Dict[Any, Any]
KeyMomentsThumbnail Object¶
KeyMoments Object¶
Playlist Object¶
- class pytubefix.contrib.playlist.Playlist(url: str, client: str = 'ANDROID_VR', proxies: Dict[str, str] | None = None, use_oauth: bool = False, allow_oauth_cache: bool = True, token_file: str | None = None, oauth_verifier: Callable[[str, str], None] | None = None, use_po_token: bool | None = False, po_token_verifier: Callable[[None], Tuple[str, str]] | None = None)[source]¶
Load a YouTube playlist with URL
- count(value) integer -- return number of occurrences of value ¶
- index(value[, start[, stop]]) integer -- return first index of value. ¶
Raises ValueError if the value is not present.
Supporting start and stop arguments is optional, but recommended.
- property last_updated: date | None¶
Extract the date that the playlist was last updated.
For some playlists, this will be a specific date, which is returned as a datetime object. For other playlists, this is an estimate such as “1 week ago”. Due to the fact that this value is returned as a string, pytube does a best-effort parsing where possible, and returns the raw string where it is not possible.
- Returns:
Date of last playlist update where possible, else the string provided
- Return type:
- property length¶
Extract the number of videos in the playlist.
- Returns:
Playlist video count
- Return type:
- property owner_id¶
Extract the channel_id of the owner of the playlist.
- Returns:
Playlist owner’s channel ID.
- Return type:
- property owner_url¶
Create the channel url of the owner of the playlist.
- Returns:
Playlist owner’s channel url.
- Return type:
- property title: str | None¶
Extract playlist title
- Returns:
playlist title (name)
- Return type:
Optional[str]
- trimmed(video_id: str) Iterable[str] [source]¶
Retrieve a list of YouTube video URLs trimmed at the given video ID
i.e. if the playlist has video IDs 1,2,3,4 calling trimmed(3) returns [1,2] :type video_id: str
video ID to trim the returned list of playlist URLs at
- Return type:
List[str]
- Returns:
List of video URLs from the playlist trimmed at the given ID
- property video_urls: DeferredGeneratorList¶
Complete links of all the videos in playlist
- Return type:
List[str]
- Returns:
List of video URLs
Channel Object¶
- class pytubefix.contrib.channel.Channel(url: str, client: str = 'ANDROID_VR', proxies: Dict[str, str] | None = None, use_oauth: bool = False, allow_oauth_cache: bool = True, token_file: str | None = None, oauth_verifier: Callable[[str, str], None] | None = None, use_po_token: bool | None = False, po_token_verifier: Callable[[None], Tuple[str, str]] | None = None)[source]¶
- property about_html¶
Get the html for the /about page.
Currently unused for any functionality.
- Return type:
- property channel_id¶
Get the ID of the YouTube channel.
This will return the underlying ID, not the vanity URL.
- Return type:
- property community_html¶
Get the html for the /community page.
Currently unused for any functionality.
- Return type:
- count(value) integer -- return number of occurrences of value ¶
- property description: str¶
Extract the channel description.
- Returns:
Channel description
- Return type:
- property featured_channels_html¶
Get the html for the /channels page.
Currently unused for any functionality.
- Return type:
- property home: list¶
Yields YouTube, Playlist and Channel objects from the channel home page.
- Returns:
List of YouTube, Playlist and Channel objects.
- index(value[, start[, stop]]) integer -- return first index of value. ¶
Raises ValueError if the value is not present.
Supporting start and stop arguments is optional, but recommended.
- property last_updated: str¶
Extract the date of the last uploaded video.
- Returns:
Last video uploaded
- Return type:
- property length¶
Extracts the approximate amount of videos from the channel.
- property live: Iterable[YouTube]¶
Yields YouTube objects of live in this channel
- Return type:
List[YouTube]
- Returns:
List of YouTube
- property owner_id¶
Extract the channel_id of the owner of the playlist.
- Returns:
Playlist owner’s channel ID.
- Return type:
- property owner_url¶
Create the channel url of the owner of the playlist.
- Returns:
Playlist owner’s channel url.
- Return type:
- property playlists: Iterable[Playlist]¶
Yields Playlist objects in this channel
- Return type:
List[Playlist]
- Returns:
List of Playlist
- property playlists_html¶
Get the html for the /playlists page.
Currently unused for any functionality.
- Return type:
- property releases: Iterable[Playlist]¶
Yields Playlist objects in this channel
- Return type:
List[Playlist]
- Returns:
List of YouTube
- property shorts: Iterable[YouTube]¶
Yields YouTube objects of short videos in this channel
- Return type:
List[YouTube]
- Returns:
List of YouTube
- property thumbnail_url: str¶
extract the profile image from the json of the channel home page
- Return type:
- Returns:
a string with the url of the channel’s profile image
- property title: str | None¶
Extract playlist title
- Returns:
playlist title (name)
- Return type:
Optional[str]
- trimmed(video_id: str) Iterable[str] ¶
Retrieve a list of YouTube video URLs trimmed at the given video ID
i.e. if the playlist has video IDs 1,2,3,4 calling trimmed(3) returns [1,2] :type video_id: str
video ID to trim the returned list of playlist URLs at
- Return type:
List[str]
- Returns:
List of video URLs from the playlist trimmed at the given ID
- property vanity_url¶
Get the vanity URL of the YouTube channel.
Returns None if it doesn’t exist.
- Return type:
- property video_urls: DeferredGeneratorList¶
Complete links of all the videos in playlist
- Return type:
List[str]
- Returns:
List of video URLs
Stream Object¶
- class pytubefix.Stream(stream: Dict, monostate: Monostate)[source]¶
Container for stream manifest data.
- property default_filename: str¶
Generate filename based on the video title.
- Return type:
- Returns:
An os file system compatible filename.
- download(output_path: str | None = None, filename: str | None = None, filename_prefix: str | None = None, skip_existing: bool = True, timeout: int | None = None, max_retries: int = 0, interrupt_checker: Callable[[], bool] | None = None) str | None [source]¶
Downloads a file from the URL provided by self.url and saves it locally with optional configurations.
- Args:
output_path (Optional[str]): Directory path where the downloaded file will be saved. Defaults to the current directory if not specified. filename (Optional[str]): Custom name for the downloaded file. If not provided, a default name is used. filename_prefix (Optional[str]): Prefix to be added to the filename (if provided). skip_existing (bool): Whether to skip the download if the file already exists at the target location. Defaults to True. timeout (Optional[int]): Maximum time, in seconds, to wait for the download request. Defaults to None for no timeout. max_retries (int): The number of times to retry the download if it fails. Defaults to 0 (no retries). interrupt_checker (Optional[Callable[[], bool]]): A callable function that is checked periodically during the download. If it returns True, the download will stop without errors.
- Returns:
Optional[str]: The full file path of the downloaded file, or None if the download was skipped or failed.
- Raises:
HTTPError: Raised if there is an error with the HTTP request during the download process.
- Note:
The skip_existing flag avoids redownloading if the file already exists in the target location.
The interrupt_checker allows for the download to be halted cleanly if certain conditions are met during the download process.
Download progress can be monitored using the on_progress callback, and the on_complete callback is triggered once the download is finished.
- property filesize: int¶
File size of the media stream in bytes.
- Return type:
- Returns:
Filesize (in bytes) of the stream.
- property filesize_approx: int¶
Get approximate filesize of the video
Falls back to HTTP call if there is not sufficient information to approximate
- Return type:
- Returns:
size of video in bytes
- property filesize_gb: float¶
File size of the media stream in gigabytes.
- Return type:
- Returns:
Rounded filesize (in gigabytes) of the stream.
- property filesize_kb: float¶
File size of the media stream in kilobytes.
- Return type:
- Returns:
Rounded filesize (in kilobytes) of the stream.
- property filesize_mb: float¶
File size of the media stream in megabytes.
- Return type:
- Returns:
Rounded filesize (in megabytes) of the stream.
- property height: int¶
Video height. Returns None if it does not have the value.
- Return type:
- Returns:
Returns an int of the video height
- iter_chunks(chunk_size: int | None = None) Iterator[bytes] [source]¶
Get the chunks directly
Example: # Write the chunk by yourself with open(“somefile.mp4”) as out_file:
out_file.writelines(stream.iter_chunks(512))
# Another way # for chunk in stream.iter_chunks(512): # out_file.write(chunk)
# Or give it external api external_api.write_media(stream.iter_chunks(512))
- Parameters:
size (int chunk)
The size in the bytes :rtype: Iterator[bytes]
- on_complete(file_path: str | None)[source]¶
On download complete handler function.
- Parameters:
file_path (str) – The file handle where the media is being written to.
- Return type:
None
- on_progress(chunk: bytes, file_handler: BinaryIO, bytes_remaining: int)[source]¶
On progress callback function.
This function writes the binary data to the file, then checks if an additional callback is defined in the monostate. This is exposed to allow things like displaying a progress bar.
- Parameters:
chunk (bytes) – Segment of media file binary data, not yet written to disk.
file_handler (
io.BufferedWriter
) – The file handle where the media is being written to.bytes_remaining (int) – The delta between the total file size in bytes and amount already downloaded.
- Return type:
None
- on_progress_for_chunks(chunk: bytes, bytes_remaining: int)[source]¶
On progress callback function.
This function checks if an additional callback is defined in the monostate. This is exposed to allow things like displaying a progress bar.
- Parameters:
chunk (bytes)
Segment of media file binary data, not yet written to disk.
io.BufferedWriter
:param int bytes_remaining: The delta between the total file size in bytes and amount already downloaded.- Return type:
None
- parse_codecs() Tuple[str | None, str | None] [source]¶
Get the video/audio codecs from list of codecs.
Parse a variable length sized list of codecs and returns a constant two element tuple, with the video codec as the first element and audio as the second. Returns None if one is not available (adaptive only).
- Return type:
- Returns:
A two element tuple with audio and video codecs.
StreamQuery Object¶
- class pytubefix.query.StreamQuery(fmt_streams)[source]¶
Interface for querying the available media streams.
- asc() StreamQuery [source]¶
Sort streams in ascending order.
- Return type:
- desc() StreamQuery [source]¶
Sort streams in descending order.
- Return type:
- filter(fps=None, res=None, resolution=None, mime_type=None, type=None, subtype=None, file_extension=None, abr=None, bitrate=None, video_codec=None, audio_codec=None, only_audio=None, only_video=None, progressive=None, adaptive=None, is_dash=None, audio_track_name=None, custom_filter_functions=None)[source]¶
Apply the given filtering criterion.
- Parameters:
fps (int or None) – (optional) The frames per second.
resolution (str or None) – (optional) Alias to
res
.res (str or None) – (optional) The video resolution.
mime_type (str or None) – (optional) Two-part identifier for file formats and format contents composed of a “type”, a “subtype”.
type (str or None) – (optional) Type part of the
mime_type
(e.g.: audio, video).subtype (str or None) – (optional) Sub-type part of the
mime_type
(e.g.: mp4, mov).file_extension (str or None) – (optional) Alias to
sub_type
.abr (str or None) – (optional) Average bitrate (ABR) refers to the average amount of data transferred per unit of time (e.g.: 64kbps, 192kbps).
bitrate (str or None) – (optional) Alias to
abr
.video_codec (str or None) – (optional) Video compression format.
audio_codec (str or None) – (optional) Audio compression format.
progressive (bool) – Excludes adaptive streams (one file contains both audio and video tracks).
adaptive (bool) – Excludes progressive streams (audio and video are on separate tracks).
is_dash (bool) – Include/exclude dash streams.
only_audio (bool) – Excludes streams with video tracks.
only_video (bool) – Excludes streams with audio tracks.
audio_track_name – Name of the dubbed audio track
custom_filter_functions (list or None) – (optional) Interface for defining complex filters without subclassing.
- first() Stream | None [source]¶
Get the first
Stream
in the results.- Return type:
Stream
or None- Returns:
the first result of this query or None if the result doesn’t contain any streams.
- get_audio_only(subtype: str = 'mp4') Stream | None [source]¶
Get highest bitrate audio stream for given codec (defaults to mp4)
- Parameters:
subtype (str) – Audio subtype, defaults to mp4
- Return type:
Stream
or None- Returns:
The
Stream
matching the given itag or None if not found.
- get_by_itag(itag: int | str) Stream | None [source]¶
Get the corresponding
Stream
for a given itag.- Parameters:
itag (int) – YouTube format identifier code.
- Return type:
Stream
or None- Returns:
The
Stream
matching the given itag or None if not found.
- get_by_resolution(resolution: str) Stream | None [source]¶
Get the corresponding
Stream
for a given resolution.Stream must be a progressive mp4.
- Parameters:
resolution (str) – Video resolution i.e. “720p”, “480p”, “360p”, “240p”, “144p”
- Return type:
Stream
or None- Returns:
The
Stream
matching the given itag or None if not found.
- get_default_audio_track() StreamQuery [source]¶
Takes the standard audio tracks, will return all audio tracks if there is no dubbing.
- Return type:
- Returns:
A StreamQuery object with filtered default dubbing streams.
- get_extra_audio_track() StreamQuery | None [source]¶
Get only dubbed audio tracks.
- Return type:
StreamQuery
or None- Returns:
A StreamQuery object with filtering only the dubbing streams.
- get_extra_audio_track_by_name(name) StreamQuery | None [source]¶
Filter dubbed audio streams by name
- Return type:
StreamQuery
or None- Returns:
A StreamQuery object filtering dubbed audio streams by name.
- get_highest_resolution(progressive=True, mime_type=None) Stream | None [source]¶
Get highest resolution stream that is a progressive video.
- Parameters:
- Return type:
Stream
or None- Returns:
The
Stream
matching the given itag or None if not found.
- get_lowest_resolution(progressive=True) Stream | None [source]¶
Get lowest resolution stream that is a progressive mp4.
- Parameters:
progressive (bool) – Filter only progressive streams (video and audio in the same file), default is True. Set False to get the adaptive stream (separate video and audio) at the lowest resolution
- Return type:
Stream
or None- Returns:
The
Stream
matching the given itag or None if not found.
- index(value[, start[, stop]]) integer -- return first index of value. ¶
Raises ValueError if the value is not present.
Supporting start and stop arguments is optional, but recommended.
- last()[source]¶
Get the last
Stream
in the results.- Return type:
Stream
or None- Returns:
Return the last result of this query or None if the result doesn’t contain any streams.
- order_by(attribute_name: str) StreamQuery [source]¶
Apply a sort order. Filters out stream the do not have the attribute.
- Parameters:
attribute_name (str) – The name of the attribute to sort by.
- otf(is_otf: bool = False) StreamQuery [source]¶
Filter stream by OTF, useful if some streams have 404 URLs
- Parameters:
is_otf (bool) – Set to False to retrieve only non-OTF streams
- Return type:
- Returns:
A StreamQuery object with otf filtered streams
Search Object¶
- class pytubefix.contrib.search.Search(query: str, client: str = 'ANDROID_VR', proxies: Dict[str, str] | None = None, use_oauth: bool = False, allow_oauth_cache: bool = True, token_file: str | None = None, oauth_verifier: Callable[[str, str], None] | None = None, use_po_token: bool | None = False, po_token_verifier: Callable[[None], Tuple[str, str]] | None = None, filters: dict | None = None)[source]¶
-
- property channel: List[Channel]¶
Returns the search result channel.
On first call, will generate and return the first set of results. Additional results can be generated using
.get_next_results()
.
- property completion_suggestions¶
Return query autocompletion suggestions for the query.
- Return type:
- Returns:
A list of autocomplete suggestions provided by YouTube for the query.
- fetch_query(continuation: str = None, filters: dict = None)[source]¶
Fetch raw results from the innertube API.
- get_next_results()[source]¶
Use the stored continuation string to fetch the next set of results.
This method does not return the results, but instead updates the results property.
- property playlist: List[Playlist]¶
Returns the search result playlist.
On first call, will generate and return the first set of results. Additional results can be generated using
.get_next_results()
.
- property results: list¶
returns a list with videos, shorts, playlist and channels.
On first call, will generate and return the first set of results. Additional results can be generated using
.get_next_results()
.- Return type:
- Returns:
A list of YouTube, Playlist and Channel objects.
ChapterThumbnail Object¶
Chapter Object¶
Extract¶
This module contains all non-cipher related data extraction logic.
- pytubefix.extract.apply_descrambler(stream_data: Dict) List[Dict] | None [source]¶
Apply various in-place transforms to YouTube’s media stream data.
Creates a
list
of dictionaries by string splitting on commas, then taking each list item, parsing it as a query string, converting it to adict
and unquoting the value.- Parameters:
stream_data (dict) – Dictionary containing query string encoded values.
Example:
>>> d = {'foo': 'bar=1&var=test,em=5&t=url%20encoded'} >>> apply_descrambler(d, 'foo') >>> print(d) {'foo': [{'bar': '1', 'var': 'test'}, {'em': '5', 't': 'url encoded'}]}
- pytubefix.extract.apply_po_token(stream_manifest: Dict, vid_info: Dict, po_token: str) None [source]¶
Apply the proof of origin token to the stream manifest
- pytubefix.extract.apply_signature(stream_manifest: Dict, vid_info: Dict, js: str, url_js: str) None [source]¶
Apply the decrypted signature to the stream manifest.
- pytubefix.extract.channel_name(url: str) str [source]¶
Extract the
channel_name
orchannel_id
from a YouTube url.This function supports the following patterns:
https://youtube.com/c/channel_name/*
:samp:`https://youtube.com/channel/{channel_id}/*
https://youtube.com/u/channel_name/*
:samp:`https://youtube.com/user/{channel_id}/*
- pytubefix.extract.get_ytcfg(html: str) str [source]¶
Get the entirety of the ytcfg object.
This is built over multiple pieces, so we have to find all matches and combine the dicts together.
- pytubefix.extract.get_ytplayer_config(html: str) Any [source]¶
Get the YouTube player configuration data from the watch html.
Extract the
ytplayer_config
, which is json data embedded within the watch html and serves as the primary source of obtaining the stream manifest data.
- pytubefix.extract.get_ytplayer_js(html: str) Any [source]¶
Get the YouTube player base JavaScript path.
- :param str html
The html contents of the watch page.
- Return type:
- Returns:
Path to YouTube’s base.js file.
- pytubefix.extract.initial_data(watch_html: str) dict [source]¶
Extract the ytInitialData json from the watch_html page.
This mostly contains metadata necessary for rendering the page on-load, such as video information, copyright notices, etc.
@param watch_html: Html of the watch page @return:
- pytubefix.extract.initial_player_response(watch_html: str) str [source]¶
Extract the ytInitialPlayerResponse json from the watch_html page.
This mostly contains metadata necessary for rendering the page on-load, such as video information, copyright notices, etc.
@param watch_html: Html of the watch page @return:
- pytubefix.extract.is_age_restricted(watch_html: str) bool [source]¶
Check if content is age restricted.
- pytubefix.extract.js_url(html: str) str [source]¶
Get the base JavaScript url.
Construct the base JavaScript url, which contains the decipher “transforms”.
- Parameters:
html (str) – The html contents of the watch page.
- pytubefix.extract.metadata(initial_data) YouTubeMetadata | None [source]¶
Get the informational metadata for the video.
e.g.: [
- {
‘Song’: ‘강남스타일(Gangnam Style)’, ‘Artist’: ‘PSY’, ‘Album’: ‘PSY SIX RULES Pt.1’, ‘Licensed to YouTube by’: ‘YG Entertainment Inc. […]’
}
]
- Return type:
YouTubeMetadata
- pytubefix.extract.mime_type_codec(mime_type_codec: str) Tuple[str, List[str]] [source]¶
Parse the type data.
Breaks up the data in the
type
key of the manifest, which contains the mime type and codecs serialized together, and splits them into separate elements.Example:
mime_type_codec(‘audio/webm; codecs=”opus”’) -> (‘audio/webm’, [‘opus’])
- pytubefix.extract.playability_status(player_response: dict) Tuple[Any, Any] [source]¶
Return the playability status and status explanation of a video.
For example, a video may have a status of LOGIN_REQUIRED, and an explanation of “This is a private video. Please sign in to verify that you may see it.”
This explanation is what gets incorporated into the media player overlay.
- pytubefix.extract.playlist_id(url: str) str [source]¶
Extract the
playlist_id
from a YouTube url.This function supports the following patterns:
https://youtube.com/playlist?list=playlist_id
https://youtube.com/watch?v=video_id&list=playlist_id
- pytubefix.extract.publish_date(watch_html: str)[source]¶
Extract publish date and return it as a datetime object :param str watch_html:
The html contents of the watch page.
- Return type:
datetime
- Returns:
Publish date of the video as a datetime object with timezone.
- pytubefix.extract.recording_available(watch_html)[source]¶
Check if live stream recording is available.
- pytubefix.extract.video_id(url: str) str [source]¶
Extract the
video_id
from a YouTube url.This function supports the following patterns:
https://youtube.com/watch?v=video_id
https://youtube.com/embed/video_id
https://youtu.be/video_id
- pytubefix.extract.video_info_url(video_id: str, watch_url: str) str [source]¶
Construct the video_info url.
Cipher¶
This module contains all the logic needed to find the signature functions.
YouTube’s strategy to restrict downloading videos is to send a ciphered version of the signature to the client, along with the decryption algorithm obfuscated in JavaScript. For the clients to play the videos, JavaScript must take the ciphered version, cycle it through a series of “transform functions,” and then signs the media URL with the output.
This module is responsible for (1) finding these “transformations functions” (2) sends them to be interpreted by jsinterp.py
- pytubefix.cipher.get_initial_function_name(js: str, js_url: str) str [source]¶
Extract the name of the function responsible for computing the signature. :param str js:
The contents of the base.js asset file.
Exceptions¶
Library specific exception definitions.
- exception pytubefix.exceptions.AgeRestrictedError(video_id: str)[source]¶
Video is age restricted, and cannot be accessed without OAuth.
- exception pytubefix.exceptions.LiveStreamOffline(video_id: str, reason: str)[source]¶
The live will start soon
- exception pytubefix.exceptions.MembersOnly(video_id: str)[source]¶
Video is members-only.
YouTube has special videos that are only viewable to users who have subscribed to a content creator. ref: https://support.google.com/youtube/answer/7544492?hl=en
- exception pytubefix.exceptions.PytubeFixError[source]¶
Base pytubefix exception that all others inherit.
This is done to not pollute the built-in exceptions, which could result in unintended errors being unexpectedly and incorrectly handled within implementers code.
- exception pytubefix.exceptions.RegexMatchError(caller: str, pattern: str | Pattern)[source]¶
Regex pattern did not return any matches.
- exception pytubefix.exceptions.UnknownVideoError(video_id: str, status: str = None, reason: str = None, developer_message: str = None)[source]¶
Unknown video error.
Base video error.
This is the base error type for all video errors.
Call this if you can’t group the error by known error type and it is not important to the developer.
Helpers¶
Various helper functions implemented by pytube.
- class pytubefix.helpers.DeferredGeneratorList(generator)[source]¶
A wrapper class for deferring list generation.
Pytubefix has some continuation generators that create web calls, which means that any time a full list is requested, all of those web calls must be made at once, which could lead to slowdowns. This will allow individual elements to be queried, so that slowdowns only happen as necessary. For example, you can iterate over elements in the list without accessing them all simultaneously. This should allow for speed improvements for playlist and channel interactions.
- pytubefix.helpers.cache(func: Callable[[...], GenericType]) GenericType [source]¶
mypy compatible annotation wrapper for lru_cache
- pytubefix.helpers.create_mock_html_json(vid_id) Dict[str, Any] [source]¶
Generate a json.gz file with sample html responses.
- :param str vid_id
YouTube video id
- :return dict data
Dict used to generate the json.gz file
- pytubefix.helpers.deprecated(reason: str) Callable [source]¶
This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emitted when the function is used.
- pytubefix.helpers.generate_all_html_json_mocks()[source]¶
Regenerate the video mock json files for all current test videos.
This should automatically output to the test/mocks directory.
- pytubefix.helpers.regex_search(pattern: str, string: str, group: int) str [source]¶
Shortcut method to search a string for a given pattern.
- pytubefix.helpers.reset_cache(verbose: bool = False)[source]¶
Deletes the __cache__ directory to reset the cache.
This function checks if the __cache__ directory exists in the same directory as the script. If it exists and is a directory, it deletes it along with its contents. If the directory does not exist, it logs a message indicating that the cache directory is not present.
- Parameters:
verbose (bool): If True, sets up logging at the DEBUG level. Default is False.
- Behavior:
When verbose is True, debug messages are logged, providing information about the existence and status of the cache directory.
If verbose is False, no logging configuration is modified, and debug messages may not be shown unless logging is already configured externally.
- Example:
>>> reset_cache(verbose=True) # Logs detailed information about the cache reset process.
- Raises:
None
- pytubefix.helpers.safe_filename(s: str, max_length: int = 255) str [source]¶
Sanitize a string making it safe to use as a filename.
This function was based off the limitations outlined here: https://en.wikipedia.org/wiki/Filename.
- pytubefix.helpers.setup_logger(level: int = 40, log_filename: str | None = None) None [source]¶
Create a configured instance of logger.
- Parameters:
level (int) – Describe the severity level of the logs to handle.
- pytubefix.helpers.strip_color_codes(input_str)[source]¶
Remove ANSI color codes from a colored string
- pytubefix.helpers.target_directory(output_path: str | None = None) str [source]¶
Function for determining target directory of a download. Returns an absolute path (if relative one given) or the current path (if none given). Makes directory if it does not exist.
- Returns:
An absolute directory path as a string.
Request¶
Implements a simple wrapper around urlopen.
- pytubefix.request.filesize(url)[source]¶
Fetch size in bytes of file at given URL
- Parameters:
url (str) – The URL to get the size of
- Returns:
int: size in bytes of remote file
- pytubefix.request.get(url, extra_headers=None, timeout=<object object>)[source]¶
Send an http GET request.
- pytubefix.request.post(url, extra_headers=None, data=None, timeout=<object object>)[source]¶
Send an http POST request.
- pytubefix.request.seq_filesize(url)[source]¶
Fetch size in bytes of file at given URL from sequential requests
- Parameters:
url (str) – The URL to get the size of
- Returns:
int: size in bytes of remote file