How to Download YouTube Shorts with Pyutube: CLI Guide and Source Code Deep Dive

Yes, Pyutube fully supports downloading YouTube Shorts by automatically detecting Shorts URLs via regex pattern matching in URLHandler.validate() and processing them through the same video pipeline as standard YouTube videos.

Pyutube is an open-source Python CLI tool for downloading YouTube content. If you need to download YouTube Shorts programmatically, Pyutube provides dedicated URL detection and processing logic that handles Shorts identically to regular videos, including adaptive stream selection and automatic audio merging.

How Pyutube Detects YouTube Shorts URLs

When you pass a URL to the Pyutube CLI, the URLHandler class validates and classifies the link before any download begins. The handler uses a regular expression to identify the distinctive Shorts URL pattern (youtube.com/shorts/...).

The URL Validation Logic

In pyutube/handlers/URLHandler.py (lines 34-53), the validate() method checks the URL structure:


# From URLHandler.py - pattern matching for Shorts

if re.match(r'https?://(www\.)?youtube\.com/shorts/[a-zA-Z0-9_-]+', url):
    return "short"

If the pattern matches, URLHandler.validate() returns link_type = "short", which triggers the Shorts-specific download branch in the main CLI logic.

The Download Pipeline for Shorts Videos

Once classified as a Short, the URL flows through Pyutube's standard video download pipeline. The DownloadService orchestrates the process, treating Shorts exactly like regular videos but ensuring the correct initialization parameters are passed to the pytube backend.

Stream Preparation and Quality Selection

The DownloadService.download_preparing() method (found in pyutube/services/DownloadService.py, lines 45-51) creates the YouTube object and retrieves available streams:

def download_preparing(self):
    video = YouTube(self.url)
    video_id = video.video_id
    streams = video.streams
    # Quality selection logic follows...

    return video, video_id, streams, video_audio, quality

For resolution selection, VideoService.get_video_streams() (lines 95-108 in pyutube/services/VideoService.py) filters the adaptive streams to match the requested quality, falling back to the nearest available resolution if the exact match doesn't exist.

Video and Audio Merging

Shorts videos download as separate video and audio streams that require merging. The DownloadService.download_video() method (lines 71-99) handles this:

def download_video(self, video, video_id, video_file, video_audio):
    # Downloads video stream

    video_file.download()
    # Downloads corresponding audio

    audio_path = video_audio.download()
    # Merges with ffmpeg

    self.merge_with_ffmpeg(video_path, audio_path)

This process ensures that downloaded Shorts maintain their original audio quality while being saved in the user's preferred format.

Practical CLI Examples for Downloading Shorts

Pyutube provides a straightforward command-line interface for Shorts downloads. Here are the most common usage patterns:

1. Download a Shorts Video with Default Settings

pyutube https://www.youtube.com/shorts/dQw4w9WgXcQ

This command detects the Shorts URL, displays the video title, prompts for quality selection, then downloads and merges the video and audio streams automatically.

2. Extract Audio Only from a Shorts Video

pyutube https://www.youtube.com/shorts/dQw4w9WgXcQ -a

The -a (or --audio) flag triggers the audio-only download path via AudioService, bypassing video stream selection and ffmpeg merging.

3. Force Specific Resolution Quality

pyutube https://www.youtube.com/shorts/dQw4w9WgXcQ --quality 720p

The --quality parameter passes the resolution preference to VideoService.get_video_streams(), which selects the nearest available adaptive stream if the exact resolution isn't available.

Key Source Files in the Shorts Download Architecture

Understanding the codebase structure helps when extending or debugging Shorts functionality:

File Role Key Functionality
pyutube/cli.py Entry point Parses CLI arguments, instantiates URLHandler, routes to download flow based on link_type
pyutube/handlers/URLHandler.py URL classification Validates URLs with regex, returns "short" type for Shorts URLs (lines 34-53)
pyutube/services/DownloadService.py Download orchestration download_preparing() initializes YouTube object; download_video() handles ffmpeg merging (lines 45-51, 71-99)
pyutube/services/VideoService.py Stream selection get_video_streams() filters adaptive streams by quality (lines 95-108)
pyutube/services/AudioService.py Audio extraction Handles audio-only downloads when -a flag is used

These components work together to provide seamless Shorts support, treating Shorts URLs with the same robustness as standard YouTube videos.

Summary

  • Pyutube detects YouTube Shorts automatically via regex pattern matching in URLHandler.validate(), which identifies youtube.com/shorts/ URLs and sets link_type = "short".
  • Shorts download through the same pipeline as regular videos, using DownloadService to prepare streams, VideoService to select quality, and ffmpeg to merge separate video and audio tracks.
  • CLI usage is identical for Shorts and standard videos, supporting flags like -a for audio-only and --quality for resolution control.
  • Source code locations: URL validation occurs in pyutube/handlers/URLHandler.py (lines 34-53), while the download logic resides in pyutube/services/DownloadService.py.

Frequently Asked Questions

Can Pyutube download YouTube Shorts in 1080p or 4K resolution?

Yes, Pyutube supports high-resolution Shorts downloads up to the maximum quality available from YouTube's adaptive streams. The VideoService.get_video_streams() method filters available resolutions and selects the nearest match to your requested quality. If 1080p or 4K streams exist for the Short, Pyutube will download and merge them using ffmpeg, though higher resolutions may require separate video and audio stream downloads that are automatically combined.

Does downloading YouTube Shorts require ffmpeg installation?

Yes, ffmpeg is required for downloading Shorts videos when you want combined audio and video output. The DownloadService.download_video() method (lines 71-99) explicitly calls ffmpeg to merge the separately downloaded video and audio streams into a single file. However, if you use the -a (audio-only) flag, ffmpeg is not required since AudioService handles single-stream downloads directly without merging.

How does Pyutube distinguish between a regular YouTube video and a Short?

Pyutube uses regex pattern matching in the URLHandler.validate() method located in pyutube/handlers/URLHandler.py (lines 34-53). The code checks if the URL matches the pattern youtube.com/shorts/[video_id]. If matched, it returns link_type = "short", which triggers the Shorts-specific branch in the CLI logic. Regular videos return link_type = "video", while playlists return link_type = "playlist", allowing Pyutube to route each URL type to its appropriate download handler.

Can I download multiple YouTube Shorts at once using Pyutube?

Currently, Pyutube processes one URL per command invocation for Shorts downloads. While the architecture supports different link types through URLHandler, the CLI in pyutube/cli.py is designed to handle single URL processing. To download multiple Shorts, you would need to run the pyutube command sequentially for each Shorts URL or create a shell script that iterates through a list of URLs, passing each one to the Pyutube CLI individually.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →