yt-dlp-utils¶
Utilities for programmatic use of yt-dlp. To use the async API, install with the extra asyncio.
-
yt_dlp_utils.get_configured_yt_dlp(sleep_time: int =
3, *, debug: bool =False, **kwargs: Unpack[yt_dlp._Params]) yt_dlp.YoutubeDL¶ Get a configured
YoutubeDLinstance.This function sets up a
yt_dlp.YoutubeDLinstance with the user’s configuration (e.g. located at~/.config/yt-dlp/config). It overrides the default logger (loggeroption), disables colours (coloroption), and sets the sleep time between requests (sleep_interval_requestsoption). It also sets theverboseflag based on thedebugparameter.All other keyword arguments are passed directly to the
yt_dlp.YoutubeDLconstructor.
-
yt_dlp_utils.setup_session(browser: str, profile: str, add_headers: Mapping[str, str] | None =
None, backoff_factor: float =2.5, domains: Iterable[str] | None =None, headers: Mapping[str, str] | None =None, session: requests.Session | None =None, status_forcelist: Collection[int] =(429, 500, 502, 503, 504), *, setup_retry: bool =False) requests.Session¶ Create or modify a Requests
requests.Sessioninstance with cookies from the browser.- Parameters:¶
- browser : str¶
The browser to extract cookies from.
- profile : str¶
The profile to extract cookies from.
- add_headers : Mapping[str, str]¶
Additional headers to add to the requests session.
- backoff_factor : float¶
The backoff factor to use for the retry mechanism.
- domains : Iterable[str]¶
Filter the cookies to only those that match the specified domains.
- headers : Mapping[str, str]¶
The headers to use for the requests session. If not specified, a default set will be used.
- session : requests.Session | None¶
An existing session to modify. If not specified, a new session will be created.
- status_forcelist : Collection[int]¶
The status codes to retry on.
- setup_retry : bool¶
Whether to set up a retry mechanism for the Requests session.
- Returns:¶
A Requests session.
- Return type:¶
Async utilities.
- class yt_dlp_utils.aio.AsyncYoutubeDL(ydl: YoutubeDL)¶
Async wrapper around
yt_dlp.YoutubeDL.This class wraps a synchronous
YoutubeDLinstance and provides async methods that run blocking operations in a thread executor.Only
downloadandextract_infoare implemented in this wrapper.-
async extract_info(url: str, ie_key: str | None =
None, extra_info: Mapping[str, Any] | None =None, *, download: bool =True, process: bool =True, force_generic_extractor: bool =False) dict[str, Any] | None¶ Extract info asynchronously.
- Parameters:¶
- url : str¶
The URL to extract info from.
- download : bool¶
Whether to download the video. Default is True.
- ie_key : str | None¶
The extractor key to use.
- extra_info : Mapping[str, Any] | None¶
Extra info to pass to the extractor.
- process : bool¶
Whether to process the info. Default is True.
- force_generic_extractor : bool¶
Whether to force the generic extractor. Default is False.
- Returns:¶
The extracted info, or None if extraction failed.
- Return type:¶
- ydl¶
The wrapped
YoutubeDLinstance.
-
async extract_info(url: str, ie_key: str | None =
-
yt_dlp_utils.aio.get_configured_yt_dlp(sleep_time: int =
3, *, debug: bool =False, **kwargs: Unpack[yt_dlp._Params]) AsyncYoutubeDL¶ Get an async-wrapped configured
YoutubeDLinstance.This function sets up a
yt_dlp.YoutubeDLinstance with the user’s configuration (e.g. located at~/.config/yt-dlp/config). It overrides the default logger (loggeroption), disables colours (coloroption), and sets the sleep time between requests (sleep_interval_requestsoption). It also sets theverboseflag based on thedebugparameter.All other keyword arguments are passed directly to the
yt_dlp.YoutubeDLconstructor.
-
async yt_dlp_utils.aio.setup_session(browser: str, profile: str, add_headers: Mapping[str, str] | None =
None, backoff_factor: float =2.5, domains: Iterable[str] | None =None, headers: Mapping[str, str] | None =None, session: AsyncSession | None =None, status_forcelist: Collection[int] =(429, 500, 502, 503, 504), *, setup_retry: bool =False) AsyncSession¶ Create or modify a niquests
AsyncSessionwith cookies from the browser.- Parameters:¶
- browser : str¶
The browser to extract cookies from.
- profile : str¶
The profile to extract cookies from.
- add_headers : Mapping[str, str]¶
Additional headers to add to the session.
- backoff_factor : float¶
The backoff factor to use for the retry mechanism.
- domains : Iterable[str]¶
Filter the cookies to only those that match the specified domains.
- headers : Mapping[str, str]¶
The headers to use for the session. If not specified, a default set will be used.
- session : AsyncSession | None¶
An existing session to modify. If not specified, a new session will be created.
- status_forcelist : Collection[int]¶
The status codes to retry on.
- setup_retry : bool¶
Whether to configure automatic retries (urllib3_future
Retry).
- Returns:¶
A niquests async session.
- Return type:¶
AsyncSession
Notes
The session should be closed with
await session.close()when done.