asyncyt.core module

core.py

AsyncYT core downloader. Contains the main AsyncYT class with methods for video info retrieval, single-video download, playlist download, and search. Also includes internal helper functions and the _FfmpegProgressParser class for parsing FFmpeg progress output. Exceptions are defined in exceptions.py and data models in basemodels.py. Binary management is handled by binaries.py.

class asyncyt.core.AsyncYT(bin_dir=None)[source]

Bases: BinaryManager

AsyncYT: Asynchronous media downloader powered by yt-dlp.

Supports single-video downloads, audio extraction, YouTube search, and native playlist downloads — all with real-time progress callbacks.

FFmpeg encoding/download progress is captured via --external-downloader ffmpeg -progress pipe:1 so you receive accurate frame, fps, speed, bitrate, size, and percentage updates.

Parameters:

bin_dir – Directory containing yt-dlp (and optionally ffmpeg) binaries.

async cancel(download_id: str) None[source]

Cancel a running single-video download.

Kills the yt-dlp process and any child FFmpeg process.

Parameters:

download_id – The ID returned by get_id().

Raises:

DownloadNotFoundError – If no download with that ID is running.

async cancel_playlist(playlist_id: str) None[source]

Request cancellation of a running playlist download.

The playlist loop checks the cancel event between each video. The currently-downloading video is also cancelled immediately.

Parameters:

playlist_id – The playlist ID returned by get_id().

Raises:

DownloadNotFoundError – If no playlist with that ID is running.

async download(url: str, config: DownloadConfig | None = None, progress_callback: Callable[[DownloadProgress], None | Awaitable[None]] | None = None, finalize: bool = True) Path[source]
async download(request: DownloadRequest, progress_callback: Callable[[DownloadProgress], None | Awaitable[None]] | None = None, finalize: bool = True) Path

Download a single video (or audio) from url.

Parameters:
  • url – Video URL or a DownloadRequest.

  • config – Optional DownloadConfig.

  • progress_callback – Async or sync callable receiving DownloadProgress.

  • finalize – Move output from temp dir to config.output_path.

Returns:

Path to the downloaded file.

Raises:
async download_playlist(url: str | None = None, playlist_config: PlaylistConfig | None = None, progress_callback: Callable[[PlaylistDownloadProgress], None | Awaitable[None]] | None = None, *, request: PlaylistRequest | None = None) PlaylistResponse[source]

Download all (or a subset of) videos from a playlist.

Supports concurrent downloads via PlaylistConfig.concurrency. Progress is reported through progress_callback with a PlaylistDownloadProgress that includes both the overall playlist state and the current video’s DownloadProgress.

Parameters:
  • url – Playlist URL (required when request is not given).

  • playlist_config – Playlist-level configuration.

  • progress_callback – Async or sync callable receiving PlaylistDownloadProgress updates.

  • request – Optional PlaylistRequest.

Returns:

PlaylistResponse with per-item results and aggregated stats.

Raises:

TypeError – If conflicting arguments are supplied.

async download_with_response(*args, **kwargs) DownloadResponse[source]

Download with an API-friendly DownloadResponse return value.

Accepts the same arguments as download().

Returns:

DownloadResponse with metadata and optional error.

async finalize_download(temp_dir: TemporaryDirectory | Path, output_dir: Path, config: DownloadConfig) List[Path][source]

Move processed files from temp_dir to output_dir.

Parameters:
  • temp_dir – Temporary directory (cleaned up afterwards).

  • output_dir – Final destination directory.

  • config – Download config (used for overwrite setting).

Returns:

List of moved Path objects.

async get_playlist(url: str, max_videos: int | None = None) PlaylistInfo[source]

Fetch playlist metadata without downloading any videos.

Parameters:
  • url – Playlist URL.

  • max_videos – Limit entries returned (None = all).

Returns:

PlaylistInfo with per-video PlaylistVideoInfo entries (each containing thumbnail URLs).

async get_playlist_info(url: str, max_videos: int | None = None) PlaylistInfo[source]

Fetch full playlist metadata, including per-video thumbnails.

Uses yt-dlp’s --flat-playlist so it is fast — no individual video pages are fetched. Thumbnails come from the flat data that YouTube/yt-dlp provides in the playlist manifest.

Parameters:
  • url – Playlist URL.

  • max_videos – Limit to this many entries (None = all).

Returns:

PlaylistInfo with all PlaylistVideoInfo entries.

Raises:

YtdlpPlaylistGetInfoError – If yt-dlp fails.

async get_video_info(url: str) VideoInfo[source]

Retrieve video metadata from url using yt-dlp.

Parameters:

url – Video URL.

Returns:

VideoInfo with title, duration, thumbnail, etc.

Raises:

YtdlpGetInfoError – If yt-dlp returns a non-zero exit code.

async search(query: str | None = None, max_results: int | None = None, *, request: SearchRequest | None = None) SearchResponse[source]

Search YouTube for videos.

Parameters:
  • query – Search query (required when request is not given).

  • max_results – Maximum results (default 10, max 50).

  • request – Optional SearchRequest (mutually exclusive with query).

Returns:

SearchResponse with a list of VideoInfo objects.